blob: 324a479c4c6eba6715cd492beb51c7577468e7c5 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
Bjorn Bringert85f418d2013-09-06 12:50:05 +010048import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070054import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070072import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -070090import android.view.accessibility.AccessibilityManager;
Adam Cohencff6af82011-09-13 14:51:53 -070091import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700109import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700115import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100120public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Daniel Sandlerf061f822013-06-27 13:59:36 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400129 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700130 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 private static final int REQUEST_PICK_APPLICATION = 6;
135 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700137 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurka8b805b12012-04-18 14:23:14 -0700139 private static final int REQUEST_BIND_APPWIDGET = 11;
140
Mathew Inwood876a8462013-06-14 14:12:41 +0100141 /**
142 * IntentStarter uses request codes starting with this. This must be greater than all activity
143 * request codes used internally.
144 */
145 protected static final int REQUEST_LAST = 100;
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
148
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800149 static final int SCREEN_COUNT = 5;
150 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Romain Guy98d01652009-06-30 16:21:04 -0700152 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800153 // To turn on these properties, type
154 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
155 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
156 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Winson Chung2672ff92012-05-04 16:22:30 -0700158 // The Intent extra that defines whether to ignore the launch animation
159 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400160 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: int
163 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700164 // Type: int
165 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700167 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
168 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
170 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700171 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700173 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: boolean
175 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
176 // Type: long
177 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700178 // Type: int
179 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
180 // Type: int
181 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
182 // Type: parcelable
183 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100185 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700186 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100187 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700188 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100189 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700190
Adam Cohen39a06042013-07-19 14:30:12 -0700191 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700192 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700193
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700195 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700196 private State mState = State.WORKSPACE;
197 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700200 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
201 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chungaf40f202013-09-18 18:26:31 -0700202 private static final int SHOW_CLING_DURATION = 250;
203 private static final int DISMISS_CLING_DURATION = 200;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800206 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chunga2413752012-04-03 14:22:34 -0700208 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700209 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
210 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700211 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700212
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800213 private final BroadcastReceiver mCloseSystemDialogsReceiver
214 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800215 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private LayoutInflater mInflater;
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700220 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800221 private DragLayer mDragLayer;
222 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700223 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private AppWidgetManager mAppWidgetManager;
226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Michael Jurkac9d95c52011-08-29 14:03:34 -0700228 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700229 private AppWidgetProviderInfo mPendingAddWidgetInfo;
230
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231 private int[] mTmpAddItemCellCoordinates = new int[2];
232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private FolderInfo mFolderInfo;
234
Winson Chung3d503fb2011-07-13 17:25:49 -0700235 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700236 private View mOverviewPanel;
237
Michael Jurka838a4ca2011-02-07 13:33:06 -0800238 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700239
Winson Chungc51db6a2011-10-05 11:44:49 -0700240 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700241 private AppsCustomizeTabHost mAppsCustomizeTabHost;
242 private AppsCustomizePagedView mAppsCustomizeContent;
243 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100244 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
248 // scroll issues (because the workspace may not have been measured yet) and extra work.
249 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
250 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
252 private SpannableStringBuilder mDefaultKeySsb = null;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private boolean mWorkspaceLoading = true;
255
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800256 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private boolean mRestoring;
258 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700259 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurka1e2f4652013-07-08 18:03:46 -0700261 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
263
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // Keep track of whether the user has left launcher
265 private static boolean sPausedFromUserAction = false;
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedInstanceState;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800271 private boolean mUserPresent = true;
272 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700273 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800274 private boolean mAttached = false;
Winson Chungfa545132013-09-26 17:45:32 -0700275 private static final boolean DISABLE_CLINGS = false;
Winson Chunge6eabff2013-09-24 11:01:23 -0700276 private static final boolean DISABLE_CUSTOM_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700278 private static LocaleConfiguration sLocaleConfiguration = null;
279
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700280 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700281
Adam Cohen61f560d2013-09-30 15:58:20 -0700282 private View.OnTouchListener mHapticFeedbackTouchListener;
283
Adam Cohended9f8d2010-11-03 13:25:16 -0700284 // Related to the auto-advancing of widgets
285 private final int ADVANCE_MSG = 1;
286 private final int mAdvanceInterval = 20000;
287 private final int mAdvanceStagger = 250;
288 private long mAutoAdvanceSentTime;
289 private long mAutoAdvanceTimeLeft = -1;
290 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
291 new HashMap<View, AppWidgetProviderInfo>();
292
Winson Chung400438b2011-01-16 17:53:48 -0800293 // Determines how long to wait after a rotation before restoring the screen orientation to
294 // match the sensor state.
295 private final int mRestoreScreenOrientationDelay = 500;
296
Michael Jurka4ef207b2010-11-29 17:05:45 -0800297 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700298 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
299 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
300 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800301
Winson Chungd64a6662013-09-30 11:06:59 -0700302 private Intent mAppMarketIntent = null;
303 private static final boolean DISABLE_MARKET_BUTTON = true;
304
Craig Mautner360310b2012-10-26 15:13:08 -0700305 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700306
Adam Cohen1462de32012-07-24 22:34:36 -0700307 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
308
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700309 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700310 static Date sDateStamp = new Date();
311 static DateFormat sDateFormat =
312 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
313 static long sRunStart = System.currentTimeMillis();
314 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700315
Winson Chung46353de2012-02-16 14:05:10 -0800316 // We only want to get the SharedPreferences once since it does an FS stat each time we get
317 // it from the context.
318 private SharedPreferences mSharedPrefs;
319
Adam Cohene25af792013-06-06 23:08:25 -0700320 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
321
Winson Chungf0c6ae02012-03-21 16:10:31 -0700322 // Holds the page that we need to animate to, and the icon views that we need to animate up
323 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700324 private ImageView mFolderIconImageView;
325 private Bitmap mFolderIconBitmap;
326 private Canvas mFolderIconCanvas;
327 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700328
Michael Jurkaddd62e92011-02-16 17:49:14 -0800329 private BubbleTextView mWaitingForResume;
330
Michael Jurka22143132012-10-04 17:42:38 +0200331 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
332 = new HideFromAccessibilityHelper();
333
Michael Jurkac1f5d262011-09-30 19:32:27 -0700334 private Runnable mBuildLayersRunnable = new Runnable() {
335 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700336 if (mWorkspace != null) {
337 mWorkspace.buildPageHardwareLayers();
338 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700339 }
340 };
341
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800342 private static ArrayList<PendingAddArguments> sPendingAddList
343 = new ArrayList<PendingAddArguments>();
344
Michael Jurka7267fa52013-09-26 15:29:57 -0700345 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800346
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800347 private static class PendingAddArguments {
348 int requestCode;
349 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700350 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700351 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800352 int cellX;
353 int cellY;
354 }
355
Daniel Sandlerff02d492013-08-05 02:12:05 -0400356 private Stats mStats;
357
Michael Jurka0a457bf2012-11-19 14:05:05 -0800358 private static boolean isPropertyEnabled(String propertyName) {
359 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700360 }
361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 @Override
363 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700364 if (DEBUG_STRICT_MODE) {
365 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
366 .detectDiskReads()
367 .detectDiskWrites()
368 .detectNetwork() // or .detectAll() for all detectable problems
369 .penaltyLog()
370 .build());
371 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
372 .detectLeakedSqlLiteObjects()
373 .detectLeakedClosableObjects()
374 .penaltyLog()
375 .penaltyDeath()
376 .build());
377 }
378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400380
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400381 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400382 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700383
384 // Determine the dynamic grid properties
385 Point smallestSize = new Point();
386 Point largestSize = new Point();
387 Point realSize = new Point();
388 Display display = getWindowManager().getDefaultDisplay();
389 display.getCurrentSizeRange(smallestSize, largestSize);
390 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700391 DisplayMetrics dm = new DisplayMetrics();
392 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700393 // Lazy-initialize the dynamic grid
394 DeviceProfile grid = app.initDynamicGrid(this,
395 Math.min(smallestSize.x, smallestSize.y),
396 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700397 realSize.x, realSize.y,
398 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700399
400 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400401 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700402 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800403 mModel = app.setLauncher(this);
404 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400405 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700407
Daniel Sandlerff02d492013-08-05 02:12:05 -0400408 mStats = new Stats(this);
409
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700410 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700411
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700412 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
413 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700414
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700415 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
416 // this also ensures that any synchronous binding below doesn't re-trigger another
417 // LauncherModel load.
418 mPaused = false;
419
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200421 android.os.Debug.startMethodTracing(
422 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
424
Adam Cohen53805212013-10-01 10:39:23 -0700425
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700428
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100430 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800432 registerContentObservers();
433
Joe Onorato7c312c12009-08-13 21:36:53 -0700434 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 mSavedState = savedInstanceState;
437 restoreState(mSavedState);
438
Winson Chunga12a2502010-12-20 14:41:35 -0800439 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700440 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200441 mAppsCustomizeContent.onPackagesUpdated(
442 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700443 }
Winson Chunga12a2502010-12-20 14:41:35 -0800444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 if (PROFILE_STARTUP) {
446 android.os.Debug.stopMethodTracing();
447 }
448
449 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700450 if (sPausedFromUserAction) {
451 // If the user leaves launcher, then we should just load items asynchronously when
452 // they return.
453 mModel.startLoader(true, -1);
454 } else {
455 // We only load the page synchronously if the user rotates (or triggers a
456 // configuration change) while launcher is in the foreground
457 mModel.startLoader(true, mWorkspace.getCurrentPage());
458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 }
460
461 // For handling default keys
462 mDefaultKeySsb = new SpannableStringBuilder();
463 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800464
465 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
466 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800467
Adam Cohenf9426d52012-06-04 17:26:21 -0700468 updateGlobalIcons();
469
470 // On large interfaces, we want the screen to auto-rotate based on the current orientation
471 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700472
473 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700474 }
475
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 protected void onUserLeaveHint() {
477 super.onUserLeaveHint();
478 sPausedFromUserAction = true;
479 }
480
Winson Chung98ca0f72013-07-29 12:58:51 -0700481 /** To be overriden by subclasses to hint to Launcher that we have custom content */
482 protected boolean hasCustomContentToLeft() {
483 return false;
484 }
485
Dave Hawkeya8881582013-09-17 15:55:33 -0600486 /**
487 * To be overridden by subclasses to create the custom content and call
488 * {@link #addToCustomContentPage}. This will only be invoked if
489 * {@link #hasCustomContentToLeft()} is {@code true}.
490 */
491 protected void addCustomContentToLeft() {
492 }
493
494 /**
495 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
496 * ensure the custom content page is added or removed if necessary.
497 */
498 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600499 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600500 // Not bound yet, wait for bindScreens to be called.
501 return;
502 }
503
504 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
505 // Create the custom content page and call the subclass to populate it.
506 mWorkspace.createCustomContentPage();
507 addCustomContentToLeft();
508 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
509 mWorkspace.removeCustomContentPage();
510 }
511 }
512
Adam Cohenf9426d52012-06-04 17:26:21 -0700513 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700514 boolean searchVisible = false;
515 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800516 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700517 int coi = getCurrentOrientationIndexForGlobalIcons();
518 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
519 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700520 if (!DISABLE_MARKET_BUTTON) {
521 updateAppMarketIcon();
522 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700523 searchVisible = updateGlobalSearchIcon();
524 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700525 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700526 if (sGlobalSearchIcon[coi] != null) {
527 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700528 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700529 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700530 if (sVoiceSearchIcon[coi] != null) {
531 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700532 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700533 }
Winson Chungd64a6662013-09-30 11:06:59 -0700534 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700535 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800536 }
Winson Chungadf0c182012-08-23 14:59:07 -0700537 if (mSearchDropTargetBar != null) {
538 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 }
Romain Guycbb89e42009-06-08 15:52:54 -0700541
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700543 if (sLocaleConfiguration == null) {
544 new AsyncTask<Void, Void, LocaleConfiguration>() {
545 @Override
546 protected LocaleConfiguration doInBackground(Void... unused) {
547 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
548 readConfiguration(Launcher.this, localeConfiguration);
549 return localeConfiguration;
550 }
551
552 @Override
553 protected void onPostExecute(LocaleConfiguration result) {
554 sLocaleConfiguration = result;
555 checkForLocaleChange(); // recursive, but now with a locale configuration
556 }
557 }.execute();
558 return;
559 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 final Configuration configuration = getResources().getConfiguration();
562
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700563 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 final String locale = configuration.locale.toString();
565
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700566 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 final int mcc = configuration.mcc;
568
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700569 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 final int mnc = configuration.mnc;
571
Romain Guy84f296c2009-11-04 15:00:44 -0800572 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573
Romain Guy84f296c2009-11-04 15:00:44 -0800574 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700575 sLocaleConfiguration.locale = locale;
576 sLocaleConfiguration.mcc = mcc;
577 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700578
Joe Onorato0589f0f2010-02-08 13:44:00 -0800579 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700580
581 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
582 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700583 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 public void run() {
585 writeConfiguration(Launcher.this, localeConfiguration);
586 }
587 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700588 }
589 }
590
591 private static class LocaleConfiguration {
592 public String locale;
593 public int mcc = -1;
594 public int mnc = -1;
595 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700596
Romain Guy98d01652009-06-30 16:21:04 -0700597 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
598 DataInputStream in = null;
599 try {
600 in = new DataInputStream(context.openFileInput(PREFERENCES));
601 configuration.locale = in.readUTF();
602 configuration.mcc = in.readInt();
603 configuration.mnc = in.readInt();
604 } catch (FileNotFoundException e) {
605 // Ignore
606 } catch (IOException e) {
607 // Ignore
608 } finally {
609 if (in != null) {
610 try {
611 in.close();
612 } catch (IOException e) {
613 // Ignore
614 }
615 }
616 }
617 }
618
619 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
620 DataOutputStream out = null;
621 try {
622 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
623 out.writeUTF(configuration.locale);
624 out.writeInt(configuration.mcc);
625 out.writeInt(configuration.mnc);
626 out.flush();
627 } catch (FileNotFoundException e) {
628 // Ignore
629 } catch (IOException e) {
630 //noinspection ResultOfMethodCallIgnored
631 context.getFileStreamPath(PREFERENCES).delete();
632 } finally {
633 if (out != null) {
634 try {
635 out.close();
636 } catch (IOException e) {
637 // Ignore
638 }
639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
641 }
642
Anton Hanssona2f665f2013-09-26 12:18:32 +0100643 public Stats getStats() {
644 return mStats;
645 }
646
Bjorn Bringertc459e522013-06-07 19:36:01 +0100647 public LayoutInflater getInflater() {
648 return mInflater;
649 }
650
Adam Cohen716b51e2011-06-30 12:09:54 -0700651 public DragLayer getDragLayer() {
652 return mDragLayer;
653 }
654
Winson Chung36a62fe2012-05-06 18:04:42 -0700655 boolean isDraggingEnabled() {
656 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
657 // that is subsequently removed from the workspace in startBinding().
658 return !mModel.isLoadingWorkspace();
659 }
660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 static int getScreen() {
662 synchronized (sLock) {
663 return sScreen;
664 }
665 }
666
667 static void setScreen(int screen) {
668 synchronized (sLock) {
669 sScreen = screen;
670 }
671 }
672
Winson Chung557d6ed2011-07-08 15:34:52 -0700673 /**
674 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
675 * a configuration step, this allows the proper animations to run after other transitions.
676 */
677 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700678 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 switch (args.requestCode) {
680 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700682 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 break;
684 case REQUEST_PICK_SHORTCUT:
685 processShortcut(args.intent);
686 break;
687 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700688 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700689 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700690 result = true;
691 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800692 case REQUEST_CREATE_APPWIDGET:
693 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700694 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700695 result = true;
696 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 }
Michael Jurka27614d22012-04-02 06:40:22 -0700698 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
699 // if you turned the screen off and then back while in All Apps, Launcher would not
700 // return to the workspace. Clearing mAddInfo.container here fixes this issue
701 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700702 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 }
704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 protected void onActivityResult(
707 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700708 // Reset the startActivity waiting flag
709 mWaitingForResult = false;
710
Michael Jurka8b805b12012-04-18 14:23:14 -0700711 if (requestCode == REQUEST_BIND_APPWIDGET) {
712 int appWidgetId = data != null ?
713 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
714 if (resultCode == RESULT_CANCELED) {
715 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
716 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700717 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 }
719 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200720 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
721 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
722 mWorkspace.exitOverviewMode(false);
723 }
724 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200726
Winson Chung557d6ed2011-07-08 15:34:52 -0700727 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
729 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700730
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 // We have special handling for widgets
732 if (isWidgetDrop) {
733 int appWidgetId = data != null ?
734 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700735 if (appWidgetId < 0) {
736 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
737 "widget configuration activity.");
738 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
739 } else {
740 completeTwoStageWidgetDrop(resultCode, appWidgetId);
741 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 return;
743 }
744
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 // The pattern used here is that a user PICKs a specific application,
746 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
749 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700750 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800751 final PendingAddArguments args = new PendingAddArguments();
752 args.requestCode = requestCode;
753 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700754 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700755 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700756 args.cellX = mPendingAddInfo.cellX;
757 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800758 if (isWorkspaceLocked()) {
759 sPendingAddList.add(args);
760 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700761 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700765 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800766 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
767 null);
768 }
769
770 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700772 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800773 Runnable onCompleteRunnable = null;
774 int animationType = 0;
775
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700776 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800777 if (resultCode == RESULT_OK) {
778 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
779 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700780 mPendingAddWidgetInfo);
781 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800782 onCompleteRunnable = new Runnable() {
783 @Override
784 public void run() {
785 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800787 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
788 null);
789 }
790 };
791 } else if (resultCode == RESULT_CANCELED) {
792 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
793 onCompleteRunnable = new Runnable() {
794 @Override
795 public void run() {
796 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
797 null);
798 }
799 };
800 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700801 if (mDragLayer.getAnimatedView() != null) {
802 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
803 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
804 animationType, boundWidget, true);
805 } else {
806 // The animated view may be null in the case of a rotation during widget configuration
807 onCompleteRunnable.run();
808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 }
810
811 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700812 protected void onStop() {
813 super.onStop();
814 FirstFrameAnimatorHelper.setIsVisible(false);
815 }
816
817 @Override
818 protected void onStart() {
819 super.onStart();
820 FirstFrameAnimatorHelper.setIsVisible(true);
821 }
822
823 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200825 long startTime = 0;
826 if (DEBUG_RESUME_TIME) {
827 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400828 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700831
Winson Chung4a2afa32012-07-19 14:53:05 -0700832 // Restore the previous launcher state
833 if (mOnResumeState == State.WORKSPACE) {
834 showWorkspace(false);
835 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung82963d52013-10-09 11:20:57 -0700836 showAllApps(false, AppsCustomizePagedView.ContentType.Applications, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700837 }
838 mOnResumeState = State.NONE;
839
Craig Mautner360310b2012-10-26 15:13:08 -0700840 // Background was set to gradient in onPause(), restore to black if in all apps.
841 setWorkspaceBackground(mState == State.WORKSPACE);
842
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800843 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700844 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700845 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400846 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700847 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700849 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700851 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200852 // We might have postponed some bind calls until onResume (see waitUntilResume) --
853 // execute them here
854 long startTimeCallbacks = 0;
855 if (DEBUG_RESUME_TIME) {
856 startTimeCallbacks = System.currentTimeMillis();
857 }
858
859 if (mAppsCustomizeContent != null) {
860 mAppsCustomizeContent.setBulkBind(true);
861 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700862 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
863 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200864 }
865 if (mAppsCustomizeContent != null) {
866 mAppsCustomizeContent.setBulkBind(false);
867 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700868 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200869 if (DEBUG_RESUME_TIME) {
870 Log.d(TAG, "Time spent processing callbacks in onResume: " +
871 (System.currentTimeMillis() - startTimeCallbacks));
872 }
Michael Jurka447bf842013-05-15 14:52:15 +0200873 }
Michael Jurka54554252013-08-01 12:52:23 +0200874 if (mOnResumeCallbacks.size() > 0) {
875 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
876 mOnResumeCallbacks.get(i).run();
877 }
878 mOnResumeCallbacks.clear();
879 }
Winson Chunge4e50662012-01-23 14:45:13 -0800880
881 // Reset the pressed state of icons that were locked in the press state while activities
882 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800883 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800884 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800885 mWaitingForResume.setStayPressed(false);
886 }
Winson Chunge4e50662012-01-23 14:45:13 -0800887 if (mAppsCustomizeContent != null) {
888 // Resets the previous all apps icon press state
889 mAppsCustomizeContent.resetDrawableState();
890 }
Winson Chung82963d52013-10-09 11:20:57 -0700891
Adam Cohen06dff352012-06-01 17:17:08 -0700892 // It is possible that widgets can receive updates while launcher is not in the foreground.
893 // Consequently, the widgets will be inflated in the orientation of the foreground activity
894 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
895 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700896 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700897
Winson Chung780fe592013-09-26 14:48:44 -0700898 // Process any items that were added while Launcher was away.
899 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
900
Winson Chung5841efa2013-09-30 18:06:44 -0700901 // Update the voice search button proxy
902 updateVoiceButtonProxyVisible(false);
903
Adam Cohenf9426d52012-06-04 17:26:21 -0700904 // Again, as with the above scenario, it's possible that one or more of the global icons
905 // were updated in the wrong orientation.
906 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200907 if (DEBUG_RESUME_TIME) {
908 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
909 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700910
911 if (mWorkspace.getCustomContentCallbacks() != null) {
912 // If we are resuming and the custom content is the current page, we call onShow().
913 // It is also poassible that onShow will instead be called slightly after first layout
914 // if PagedView#setRestorePage was set to the custom content page in onCreate().
915 if (mWorkspace.isOnOrMovingToCustomContent()) {
916 mWorkspace.getCustomContentCallbacks().onShow();
917 }
918 }
Adam Cohenedaaa302013-10-01 17:33:27 -0700919 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -0700920 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -0700921 }
922
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700924 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700925 // Ensure that items added to Launcher are queued until Launcher returns
926 InstallShortcutReceiver.enableInstallQueue();
927
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700928 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700929 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800930 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700931 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700932
933 // We call onHide() aggressively. The custom content callbacks should be able to
934 // debounce excess onHide calls.
935 if (mWorkspace.getCustomContentCallbacks() != null) {
936 mWorkspace.getCustomContentCallbacks().onHide();
937 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700938 }
Romain Guycbb89e42009-06-08 15:52:54 -0700939
Adam Cohen66a01fd2013-06-11 12:48:00 -0700940 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -0700941 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600942 addCustomContentToLeft();
943 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700944 }
945
Adam Cohenbffe7452013-07-22 18:21:45 -0700946 QSBScroller mQsbScroller = new QSBScroller() {
947 int scrollY = 0;
948
949 @Override
950 public void setScrollY(int scroll) {
951 scrollY = scroll;
952
953 if (mWorkspace.isOnOrMovingToCustomContent()) {
954 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +0100955 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -0700956 }
957 }
958 };
959
960 public void resetQSBScroll() {
961 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +0100962 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -0700963 }
964
965 public interface CustomContentCallbacks {
966 // Custom content is completely shown
967 public void onShow();
968
969 // Custom content is completely hidden
970 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700971
972 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
973 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700974 }
975
Adam Cohen30bacb22013-08-29 14:25:25 -0700976 protected void startSettings() {
977 }
978
Adam Cohenbffe7452013-07-22 18:21:45 -0700979 public interface QSBScroller {
980 public void setScrollY(int scrollY);
981 }
982
Winson Chung98ca0f72013-07-29 12:58:51 -0700983 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -0700984 CustomContentCallbacks callbacks, String description) {
985 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -0700986 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700987 }
988
Adam Cohenedb40762013-07-18 16:45:45 -0700989 // The custom content needs to offset its content to account for the QSB
990 public int getTopOffsetForCustomContent() {
991 return mWorkspace.getPaddingTop();
992 }
993
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700994 @Override
995 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 // Flag the loader to stop early before switching
997 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700998 if (mAppsCustomizeContent != null) {
999 mAppsCustomizeContent.surrender();
1000 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001001 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001002 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001003
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001004 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001005 @Override
1006 public void onWindowFocusChanged(boolean hasFocus) {
1007 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001008 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001009 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 private boolean acceptFilter() {
1012 final InputMethodManager inputManager = (InputMethodManager)
1013 getSystemService(Context.INPUT_METHOD_SERVICE);
1014 return !inputManager.isFullscreenMode();
1015 }
1016
1017 @Override
1018 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001019 final int uniChar = event.getUnicodeChar();
1020 final boolean handled = super.onKeyDown(keyCode, event);
1021 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1022 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1024 keyCode, event);
1025 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001026 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001027 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001028 // showSearchDialog()
1029 // If there are multiple keystrokes before the search dialog takes focus,
1030 // onSearchRequested() will be called for every keystroke,
1031 // but it is idempotent, so it's fine.
1032 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 }
1034 }
1035
Joe Onorato8a9625e2010-01-28 15:55:35 -08001036 // Eat the long press event so the keyboard doesn't come up.
1037 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1038 return true;
1039 }
1040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 return handled;
1042 }
1043
Karl Rosaen138a0412009-04-23 19:00:21 -07001044 private String getTypedText() {
1045 return mDefaultKeySsb.toString();
1046 }
1047
1048 private void clearTypedText() {
1049 mDefaultKeySsb.clear();
1050 mDefaultKeySsb.clearSpans();
1051 Selection.setSelection(mDefaultKeySsb, 0);
1052 }
1053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001055 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1056 * State
1057 */
1058 private static State intToState(int stateOrdinal) {
1059 State state = State.WORKSPACE;
1060 final State[] stateValues = State.values();
1061 for (int i = 0; i < stateValues.length; i++) {
1062 if (stateValues[i].ordinal() == stateOrdinal) {
1063 state = stateValues[i];
1064 break;
1065 }
1066 }
1067 return state;
1068 }
1069
1070 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 * Restores the previous state, if it exists.
1072 *
1073 * @param savedState The previous state.
1074 */
1075 private void restoreState(Bundle savedState) {
1076 if (savedState == null) {
1077 return;
1078 }
1079
Michael Jurka883f55b2010-10-21 15:47:14 -07001080 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001081 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001082 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001083 }
1084
Winson Chungf0c6ae02012-03-21 16:10:31 -07001085 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001087 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 }
1089
Winson Chung3d503fb2011-07-13 17:25:49 -07001090 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001091 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001092
Winson Chung3d503fb2011-07-13 17:25:49 -07001093 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1094 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001095 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001096 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1097 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001098 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1099 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1100 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001101 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 mRestoring = true;
1103 }
1104
1105 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1106 if (renameFolder) {
1107 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001108 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 mRestoring = true;
1110 }
Winson Chunga12a2502010-12-20 14:41:35 -08001111
Winson Chung785d2eb2011-04-14 16:08:02 -07001112 // Restore the AppsCustomize tab
1113 if (mAppsCustomizeTabHost != null) {
1114 String curTab = savedState.getString("apps_customize_currentTab");
1115 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001116 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001117 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001118 mAppsCustomizeContent.loadAssociatedPages(
1119 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001120 }
1121
Winson Chung5afbf7b2011-07-25 11:53:08 -07001122 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1123 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126
1127 /**
1128 * Finds all the views we need and configure them properly.
1129 */
1130 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001131 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001132
Craig Mautner360310b2012-10-26 15:13:08 -07001133 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001134 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1135 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001136
John Spurlock77e1f472013-09-11 10:09:51 -04001137 mLauncherView.setSystemUiVisibility(
1138 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001139 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140
Winson Chung4c98d922011-05-31 16:50:48 -07001141 // Setup the drag layer
1142 mDragLayer.setup(this, dragController);
1143
Winson Chung3d503fb2011-07-13 17:25:49 -07001144 // Setup the hotseat
1145 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1146 if (mHotseat != null) {
1147 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001148 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001149 }
1150
Adam Cohenf358a4b2013-07-23 16:47:31 -07001151 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001152 View widgetButton = findViewById(R.id.widget_button);
1153 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001154 @Override
1155 public void onClick(View arg0) {
Winson Chung82963d52013-10-09 11:20:57 -07001156 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001157 }
1158 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001159 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1160
1161 View wallpaperButton = findViewById(R.id.wallpaper_button);
1162 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001163 @Override
1164 public void onClick(View arg0) {
1165 startWallpaper();
1166 }
1167 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001168 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1169
1170 View settingsButton = findViewById(R.id.settings_button);
1171 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001172 @Override
1173 public void onClick(View arg0) {
1174 startSettings();
1175 }
1176 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001177 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001178 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001179
Winson Chung4c98d922011-05-31 16:50:48 -07001180 // Setup the workspace
1181 mWorkspace.setHapticFeedbackEnabled(false);
1182 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001183 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001184 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001185
Winson Chungf0ea4d32011-06-06 14:27:16 -07001186 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001187 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001188
Winson Chungf0ea4d32011-06-06 14:27:16 -07001189 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001190 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001191 mAppsCustomizeContent = (AppsCustomizePagedView)
1192 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1193 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001194
Winson Chung3d503fb2011-07-13 17:25:49 -07001195 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001196 dragController.setDragScoller(mWorkspace);
1197 dragController.setScrollView(mDragLayer);
1198 dragController.setMoveTarget(mWorkspace);
1199 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001200 if (mSearchDropTargetBar != null) {
1201 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001202 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001203
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001204 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001205 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001206 mWeightWatcher = new WeightWatcher(this);
1207 mWeightWatcher.setAlpha(0.5f);
1208 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001209 new FrameLayout.LayoutParams(
1210 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001211 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001212 Gravity.BOTTOM)
1213 );
Adam Cohen39a06042013-07-19 14:30:12 -07001214
1215 boolean show = shouldShowWeightWatcher();
1216 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 }
1219
1220 /**
1221 * Creates a view representing a shortcut.
1222 *
1223 * @param info The data structure describing the shortcut.
1224 *
1225 * @return A View inflated from R.layout.application.
1226 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001227 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001229 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
1231
1232 /**
1233 * Creates a view representing a shortcut inflated from the specified resource.
1234 *
1235 * @param layoutResId The id of the XML layout used to create the shortcut.
1236 * @param parent The group the shortcut belongs to.
1237 * @param info The data structure describing the shortcut.
1238 *
1239 * @return A View inflated from layoutResId.
1240 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001241 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001242 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1243 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 return favorite;
1246 }
1247
1248 /**
1249 * Add an application shortcut to the workspace.
1250 *
1251 * @param data The intent describing the application.
1252 * @param cellInfo The position on screen where to create the shortcut.
1253 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001254 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001255 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001256 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001257
Adam Cohenfbba09b2011-07-18 21:43:05 -07001258 // First we check if we already know the exact location where we want to add this item.
1259 if (cellX >= 0 && cellY >= 0) {
1260 cellXY[0] = cellX;
1261 cellXY[1] = cellY;
1262 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001263 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001264 return;
1265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001267 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001268
Romain Guy73b979d2009-06-09 12:57:21 -07001269 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001270 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001272 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001273 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001274 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001275 } else {
1276 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
1278 }
Romain Guycbb89e42009-06-08 15:52:54 -07001279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 /**
1281 * Add a shortcut to the workspace.
1282 *
1283 * @param data The intent describing the shortcut.
1284 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001286 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001287 int cellY) {
1288 int[] cellXY = mTmpAddItemCellCoordinates;
1289 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001290 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001291
Michael Jurkad3ef3062010-11-23 16:23:58 -08001292 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001293
Adam Cohen558baaf2011-08-15 15:22:57 -07001294 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001295 if (info == null) {
1296 return;
1297 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001298 final View view = createShortcut(info);
1299
Adam Cohenfbba09b2011-07-18 21:43:05 -07001300 // First we check if we already know the exact location where we want to add this item.
1301 if (cellX >= 0 && cellY >= 0) {
1302 cellXY[0] = cellX;
1303 cellXY[1] = cellY;
1304 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001305
1306 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001307 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001308 true, null,null)) {
1309 return;
1310 }
1311 DragObject dragObject = new DragObject();
1312 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001313 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1314 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001315 return;
1316 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001317 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001318 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001319 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001320 foundCellSpan = (result != null);
1321 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001322 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001323 }
1324
1325 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001326 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001327 return;
1328 }
1329
Adam Cohendcd297f2013-06-18 13:13:40 -07001330 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331
1332 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001333 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001334 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 }
1336 }
1337
Adam Cohen2f093b62012-04-30 18:59:53 -07001338 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1339 int minHeight) {
1340 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001341 // We want to account for the extra amount of padding that we are adding to the widget
1342 // to ensure that it gets the full amount of space that it has requested
1343 int requiredWidth = minWidth + padding.left + padding.right;
1344 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001345 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001346 }
1347
Adam Cohen2f093b62012-04-30 18:59:53 -07001348 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1349 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001350 }
1351
Adam Cohen2f093b62012-04-30 18:59:53 -07001352 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1353 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001354 }
1355
Adam Cohen2f093b62012-04-30 18:59:53 -07001356 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1357 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001358 }
1359
Adam Cohen2f093b62012-04-30 18:59:53 -07001360 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1361 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001362 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001363 }
1364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001366 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001368 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001369 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001371 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001372 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1373 if (appWidgetInfo == null) {
1374 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1375 }
Romain Guycbb89e42009-06-08 15:52:54 -07001376
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001377 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001378 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001379
Adam Cohen2f093b62012-04-30 18:59:53 -07001380 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1381 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001384 // We have saved the position to which the widget was dragged-- this really only matters
1385 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001386 int[] cellXY = mTmpAddItemCellCoordinates;
1387 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001388 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001389 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1391 cellXY[0] = mPendingAddInfo.cellX;
1392 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001393 spanXY[0] = mPendingAddInfo.spanX;
1394 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001395 foundCellSpan = true;
1396 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001397 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001398 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001399 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1400 spanXY[1], cellXY, finalSpan);
1401 spanXY[0] = finalSpan[0];
1402 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001403 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001404 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001405 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001406 }
1407
Michael Jurka0280c3b2010-09-17 15:00:07 -07001408 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001409 if (appWidgetId != -1) {
1410 // Deleting an app widget ID is a void call but writes to disk before returning
1411 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001412 new Thread("deleteAppWidgetId") {
1413 public void run() {
1414 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1415 }
1416 }.start();
1417 }
Winson Chung93eef082012-03-23 15:59:27 -07001418 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001419 return;
1420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001422 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001423 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1424 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001425 launcherInfo.spanX = spanXY[0];
1426 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001427 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1428 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001431 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432
1433 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001434 if (hostView == null) {
1435 // Perform actual inflation because we're live
1436 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1437 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1438 } else {
1439 // The AppWidgetHostView has already been inflated and instantiated
1440 launcherInfo.hostView = hostView;
1441 }
Romain Guycbb89e42009-06-08 15:52:54 -07001442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001444 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001445 launcherInfo.notifyWidgetSizeChanged(this);
1446
Adam Cohendcd297f2013-06-18 13:13:40 -07001447 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001448 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001449
1450 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001452 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
Romain Guycbb89e42009-06-08 15:52:54 -07001454
Adam Cohended9f8d2010-11-03 13:25:16 -07001455 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1456 @Override
1457 public void onReceive(Context context, Intent intent) {
1458 final String action = intent.getAction();
1459 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1460 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001461 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001462 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001463
Winson Chungc100e8e2011-08-09 16:02:43 -07001464 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001465 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001466 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001467 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001468 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001469 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1470 mUserPresent = true;
1471 updateRunning();
1472 }
1473 }
1474 };
1475
1476 @Override
1477 public void onAttachedToWindow() {
1478 super.onAttachedToWindow();
1479
1480 // Listen for broadcasts related to user-presence
1481 final IntentFilter filter = new IntentFilter();
1482 filter.addAction(Intent.ACTION_SCREEN_OFF);
1483 filter.addAction(Intent.ACTION_USER_PRESENT);
1484 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001485 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001486 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001487 mVisible = true;
1488 }
1489
1490 @Override
1491 public void onDetachedFromWindow() {
1492 super.onDetachedFromWindow();
1493 mVisible = false;
1494
Adam Cohend113e0c2010-11-11 10:48:05 -08001495 if (mAttached) {
1496 unregisterReceiver(mReceiver);
1497 mAttached = false;
1498 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001499 updateRunning();
1500 }
1501
1502 public void onWindowVisibilityChanged(int visibility) {
1503 mVisible = visibility == View.VISIBLE;
1504 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001505 // The following code used to be in onResume, but it turns out onResume is called when
1506 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1507 // is a more appropriate event to handle
1508 if (mVisible) {
1509 mAppsCustomizeTabHost.onWindowVisible();
1510 if (!mWorkspaceLoading) {
1511 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001512 // We want to let Launcher draw itself at least once before we force it to build
1513 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001514 // apps is nice and speedy.
1515 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001516 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001517 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001518 if (mStarted) return;
1519 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001520 // We delay the layer building a bit in order to give
1521 // other message processing a time to run. In particular
1522 // this avoids a delay in hiding the IME if it was
1523 // currently shown, because doing that may involve
1524 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001525 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001526 final ViewTreeObserver.OnDrawListener listener = this;
1527 mWorkspace.post(new Runnable() {
1528 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001529 if (mWorkspace != null &&
1530 mWorkspace.getViewTreeObserver() != null) {
1531 mWorkspace.getViewTreeObserver().
1532 removeOnDrawListener(listener);
1533 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001534 }
1535 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001536 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001537 }
1538 });
1539 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001540 // When Launcher comes back to foreground, a different Activity might be responsible for
1541 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001542 if (!DISABLE_MARKET_BUTTON) {
1543 updateAppMarketIcon();
1544 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001545 clearTypedText();
1546 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001547 }
1548
1549 private void sendAdvanceMessage(long delay) {
1550 mHandler.removeMessages(ADVANCE_MSG);
1551 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1552 mHandler.sendMessageDelayed(msg, delay);
1553 mAutoAdvanceSentTime = System.currentTimeMillis();
1554 }
1555
1556 private void updateRunning() {
1557 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1558 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1559 mAutoAdvanceRunning = autoAdvanceRunning;
1560 if (autoAdvanceRunning) {
1561 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1562 sendAdvanceMessage(delay);
1563 } else {
1564 if (!mWidgetsToAdvance.isEmpty()) {
1565 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1566 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1567 }
1568 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001569 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001570 }
1571 }
1572 }
1573
1574 private final Handler mHandler = new Handler() {
1575 @Override
1576 public void handleMessage(Message msg) {
1577 if (msg.what == ADVANCE_MSG) {
1578 int i = 0;
1579 for (View key: mWidgetsToAdvance.keySet()) {
1580 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1581 final int delay = mAdvanceStagger * i;
1582 if (v instanceof Advanceable) {
1583 postDelayed(new Runnable() {
1584 public void run() {
1585 ((Advanceable) v).advance();
1586 }
1587 }, delay);
1588 }
1589 i++;
1590 }
1591 sendAdvanceMessage(mAdvanceInterval);
1592 }
1593 }
1594 };
1595
1596 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001597 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001598 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1599 if (v instanceof Advanceable) {
1600 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001601 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001602 updateRunning();
1603 }
1604 }
1605
1606 void removeWidgetToAutoAdvance(View hostView) {
1607 if (mWidgetsToAdvance.containsKey(hostView)) {
1608 mWidgetsToAdvance.remove(hostView);
1609 updateRunning();
1610 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001611 }
1612
Joe Onorato9c1289c2009-08-17 11:03:03 -04001613 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001615 launcherInfo.hostView = null;
1616 }
1617
Winson Chung93eef082012-03-23 15:59:27 -07001618 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1619 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1620 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001621 }
1622
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001623 public LauncherAppWidgetHost getAppWidgetHost() {
1624 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 }
Romain Guycbb89e42009-06-08 15:52:54 -07001626
Winson Chunga9abd0e2010-10-27 17:18:37 -07001627 public LauncherModel getModel() {
1628 return mModel;
1629 }
1630
Bjorn Bringertc459e522013-06-07 19:36:01 +01001631 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001632 getWindow().closeAllPanels();
1633
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001634 // Whatever we were doing is hereby canceled.
1635 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001636 }
1637
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 @Override
1639 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001640 long startTime = 0;
1641 if (DEBUG_RESUME_TIME) {
1642 startTime = System.currentTimeMillis();
1643 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 super.onNewIntent(intent);
1645
1646 // Close the menu
1647 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001648 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001649 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001650
Adam Cohened307df2013-10-02 09:37:31 -07001651 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1652 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1653 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001654
Adam Cohen6fecd412013-10-02 17:41:50 -07001655 if (mWorkspace == null) {
1656 // Can be cases where mWorkspace is null, this prevents a NPE
1657 return;
1658 }
1659 Folder openFolder = mWorkspace.getOpenFolder();
1660 // In all these cases, only animate if we're already on home
1661 mWorkspace.exitWidgetResizeMode();
1662 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1663 openFolder == null) {
1664 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001665 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001666
Adam Cohen6fecd412013-10-02 17:41:50 -07001667 closeFolder();
1668 exitSpringLoadedDragMode();
1669
1670 // If we are already on home, then just animate back to the workspace,
1671 // otherwise, just wait until onResume to set the state back to Workspace
1672 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001673 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001674 } else {
1675 mOnResumeState = State.WORKSPACE;
1676 }
1677
1678 final View v = getWindow().peekDecorView();
1679 if (v != null && v.getWindowToken() != null) {
1680 InputMethodManager imm = (InputMethodManager)getSystemService(
1681 INPUT_METHOD_SERVICE);
1682 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1683 }
1684
1685 // Reset the apps customize page
1686 if (mAppsCustomizeTabHost != null) {
1687 mAppsCustomizeTabHost.reset();
1688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 }
Adam Cohened307df2013-10-02 09:37:31 -07001690
Michael Jurka447bf842013-05-15 14:52:15 +02001691 if (DEBUG_RESUME_TIME) {
1692 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 }
1695
1696 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001697 public void onRestoreInstanceState(Bundle state) {
1698 super.onRestoreInstanceState(state);
1699 for (int page: mSynchronouslyBoundPages) {
1700 mWorkspace.restoreInstanceStateForChild(page);
1701 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
1703
1704 @Override
1705 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001706 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001707 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708
Michael Jurka883f55b2010-10-21 15:47:14 -07001709 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001710 // We close any open folder since it will not be re-opened, and we need to make sure
1711 // this state is reflected.
1712 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713
Adam Cohendcd297f2013-06-18 13:13:40 -07001714 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001715 mWaitingForResult) {
1716 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001717 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001718 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1719 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001720 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1721 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1722 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 }
1724
1725 if (mFolderInfo != null && mWaitingForResult) {
1726 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1727 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1728 }
Michael Jurka946ad472010-07-09 18:05:18 -07001729
Winson Chung785d2eb2011-04-14 16:08:02 -07001730 // Save the current AppsCustomize tab
1731 if (mAppsCustomizeTabHost != null) {
1732 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1733 if (currentTabTag != null) {
1734 outState.putString("apps_customize_currentTab", currentTabTag);
1735 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001736 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1737 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 }
1740
1741 @Override
1742 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001744
Winson Chunge7a03942011-08-05 15:05:12 -07001745 // Remove all pending runnables
1746 mHandler.removeMessages(ADVANCE_MSG);
1747 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001748 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001749
Winson Chungcd2b0142011-06-08 16:02:26 -07001750 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001751 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001752 mModel.stopLoader();
1753 app.setLauncher(null);
1754
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001756 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001758 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001760 mAppWidgetHost = null;
1761
1762 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763
1764 TextKeyListener.getInstance().release();
1765
Winson Chung81b52252012-08-27 15:34:29 -07001766 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1767 // to prevent leaking Launcher activities on orientation change.
1768 if (mModel != null) {
1769 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1770 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001771
1772 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001773 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001774
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001775 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001776 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1777 mWorkspace.removeAllViews();
1778 mWorkspace = null;
1779 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001780
Michael Jurka2ecf9952012-06-18 12:52:28 -07001781 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
1783
Adam Cohena9cf38f2011-05-02 15:36:58 -07001784 public DragController getDragController() {
1785 return mDragController;
1786 }
1787
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 @Override
1789 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001790 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 super.startActivityForResult(intent, requestCode);
1792 }
1793
Winson Chung70d72102011-08-12 11:24:35 -07001794 /**
1795 * Indicates that we want global search for this activity by setting the globalSearch
1796 * argument for {@link #startSearch} to true.
1797 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001799 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001801
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001802 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001803
Karl Rosaen138a0412009-04-23 19:00:21 -07001804 if (initialQuery == null) {
1805 // Use any text typed in the launcher as the initial query
1806 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001807 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 if (appSearchData == null) {
1809 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001810 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 }
Winson Chungadf0c182012-08-23 14:59:07 -07001812 Rect sourceBounds = new Rect();
1813 if (mSearchDropTargetBar != null) {
1814 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1815 }
Romain Guycbb89e42009-06-08 15:52:54 -07001816
Bjorn Bringertc459e522013-06-07 19:36:01 +01001817 startSearch(initialQuery, selectInitialQuery,
1818 appSearchData, sourceBounds);
1819 }
1820
1821 public void startSearch(String initialQuery,
1822 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001823 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001824 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001825 }
1826
1827 /**
1828 * Starts the global search activity. This code is a copied from SearchManager
1829 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001830 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001831 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001832 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001833 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1834 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1835 if (globalSearchActivity == null) {
1836 Log.w(TAG, "No global search activity found.");
1837 return;
1838 }
1839 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1840 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1841 intent.setComponent(globalSearchActivity);
1842 // Make sure that we have a Bundle to put source in
1843 if (appSearchData == null) {
1844 appSearchData = new Bundle();
1845 } else {
1846 appSearchData = new Bundle(appSearchData);
1847 }
1848 // Set source to package name of app that starts global search, if not set already.
1849 if (!appSearchData.containsKey("source")) {
1850 appSearchData.putString("source", getPackageName());
1851 }
1852 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1853 if (!TextUtils.isEmpty(initialQuery)) {
1854 intent.putExtra(SearchManager.QUERY, initialQuery);
1855 }
1856 if (selectInitialQuery) {
1857 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1858 }
1859 intent.setSourceBounds(sourceBounds);
1860 try {
1861 startActivity(intent);
1862 } catch (ActivityNotFoundException ex) {
1863 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1864 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
1866
Winson Chung70d72102011-08-12 11:24:35 -07001867 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001868 public boolean onPrepareOptionsMenu(Menu menu) {
1869 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001870 if (!mWorkspace.isInOverviewMode()) {
1871 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001872 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001873 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001876 @Override
1877 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001878 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001879 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001880 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001881 }
1882
Joe Onorato9c1289c2009-08-17 11:03:03 -04001883 public boolean isWorkspaceLocked() {
1884 return mWorkspaceLoading || mWaitingForResult;
1885 }
1886
Michael Jurka0280c3b2010-09-17 15:00:07 -07001887 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001888 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001889 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001890 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1891 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001892 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001893 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001894 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001895
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001896 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1897 AppWidgetProviderInfo appWidgetInfo) {
1898 if (appWidgetInfo.configure != null) {
1899 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001900
Bjorn Bringert7984c942009-12-09 15:38:25 +00001901 // Launch over to configure widget, if needed
1902 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001903 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001904 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001905 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001907 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001908 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001909 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001910 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001911 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
1913 }
Romain Guycbb89e42009-06-08 15:52:54 -07001914
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001915 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001916 // Close any folders that may be open.
1917 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001918 mWorkspace.moveToCustomContentScreen(animate);
1919 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001920 /**
1921 * Process a shortcut drop.
1922 *
1923 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001924 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001925 * @param cell The cell it should be added to, optional
1926 * @param position The location on the screen where it was dropped, optional
1927 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001928 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001929 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001930 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001931 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001932 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001933 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001934
1935 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001936 mPendingAddInfo.cellX = cell[0];
1937 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001938 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001939
1940 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1941 createShortcutIntent.setComponent(componentName);
1942 processShortcut(createShortcutIntent);
1943 }
1944
Adam Cohenfbba09b2011-07-18 21:43:05 -07001945 /**
1946 * Process a widget drop.
1947 *
1948 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001950 * @param cell The cell it should be added to, optional
1951 * @param position The location on the screen where it was dropped, optional
1952 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001953 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001954 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001955 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001956 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001957 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001958 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001959 mPendingAddInfo.minSpanX = info.minSpanX;
1960 mPendingAddInfo.minSpanY = info.minSpanY;
1961
Adam Cohenfbba09b2011-07-18 21:43:05 -07001962 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001963 mPendingAddInfo.cellX = cell[0];
1964 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001965 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001966 if (span != null) {
1967 mPendingAddInfo.spanX = span[0];
1968 mPendingAddInfo.spanY = span[1];
1969 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001970
Adam Cohened66b2b2012-01-23 17:28:51 -08001971 AppWidgetHostView hostView = info.boundWidget;
1972 int appWidgetId;
1973 if (hostView != null) {
1974 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001975 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001976 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001977 // In this case, we either need to start an activity to get permission to bind
1978 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001979 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001980 Bundle options = info.bindOptions;
1981
1982 boolean success = false;
1983 if (options != null) {
1984 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1985 info.componentName, options);
1986 } else {
1987 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1988 info.componentName);
1989 }
1990 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001991 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001992 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001993 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001994 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1995 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1996 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001997 // TODO: we need to make sure that this accounts for the options bundle.
1998 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001999 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2000 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002001 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002002 }
2003
Joe Onoratodeb98af2010-02-19 14:59:39 -08002004 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002005 // Handle case where user selected "Applications"
2006 String applicationName = getResources().getString(R.string.group_applications);
2007 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002008
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002009 if (applicationName != null && applicationName.equals(shortcutName)) {
2010 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2011 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002012
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002013 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2014 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002015 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002016 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002017 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002018 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
2021
Winson Chung24ab2f12010-09-16 14:10:47 -07002022 void processWallpaper(Intent intent) {
2023 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2024 }
2025
Adam Cohendcd297f2013-06-18 13:13:40 -07002026 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002027 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002028 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 folderInfo.title = getText(R.string.folder_name);
2030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002032 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002033 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002034 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035
2036 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002037 FolderIcon newFolder =
2038 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002039 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002040 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002041 // Force measure the new folder icon
2042 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2043 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002044 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
Romain Guycbb89e42009-06-08 15:52:54 -07002046
Joe Onorato9c1289c2009-08-17 11:03:03 -04002047 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002048 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002049 }
2050
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002051 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002053 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002054 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 }
2056
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002057 protected ComponentName getWallpaperPickerComponent() {
2058 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2059 }
2060
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002061 /**
2062 * Registers various content observers. The current implementation registers
2063 * only a favorites observer to keep track of the favorites applications.
2064 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002065 private void registerContentObservers() {
2066 ContentResolver resolver = getContentResolver();
2067 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2068 true, mWidgetObserver);
2069 }
2070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 @Override
2072 public boolean dispatchKeyEvent(KeyEvent event) {
2073 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2074 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002076 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002077 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002078 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002079 dumpState();
2080 return true;
2081 }
2082 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002083 }
2084 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2085 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002086 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 return true;
2088 }
2089 }
2090
2091 return super.dispatchKeyEvent(event);
2092 }
2093
Joe Onorato88ec0992009-11-19 13:16:06 -08002094 @Override
2095 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002096 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002097 if (mAppsCustomizeContent.getContentType() ==
2098 AppsCustomizePagedView.ContentType.Applications) {
2099 showWorkspace(true);
2100 } else {
2101 showOverviewMode(true);
2102 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002103 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002104 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002105 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002106 Folder openFolder = mWorkspace.getOpenFolder();
2107 if (openFolder.isEditingName()) {
2108 openFolder.dismissEditingName();
2109 } else {
2110 closeFolder();
2111 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002112 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002113 mWorkspace.exitWidgetResizeMode();
2114
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002115 // Back button is a no-op here, but give at least some feedback for the button press
2116 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002117 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002118 }
2119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002121 * Re-listen when widgets are reset.
2122 */
2123 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002124 if (mAppWidgetHost != null) {
2125 mAppWidgetHost.startListening();
2126 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002127 }
2128
2129 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 * Launches the intent referred by the clicked shortcut.
2131 *
2132 * @param v The view representing the clicked shortcut.
2133 */
2134 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002135 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2136 // view has detached (it's possible for this to happen if the view is removed mid touch).
2137 if (v.getWindowToken() == null) {
2138 return;
2139 }
2140
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002141 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002142 return;
2143 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002144
Adam Cohen1697b792013-09-17 19:08:21 -07002145 if (v instanceof Workspace) {
2146 if (mWorkspace.isInOverviewMode()) {
2147 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002148 }
2149 return;
2150 }
2151
2152 if (v instanceof CellLayout) {
2153 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002154 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002155 }
2156 }
2157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002159 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002161 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2162 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002163
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002164 // Check for special shortcuts
2165 if (intent.getComponent() != null) {
2166 final String shortcutClass = intent.getComponent().getClassName();
2167
2168 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002169 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002170 return;
2171 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2172 MemoryDumpActivity.startDump(this);
2173 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002174 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2175 toggleShowWeightWatcher();
2176 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002177 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002178 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002179
2180 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002181 int[] pos = new int[2];
2182 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002183 intent.setSourceBounds(new Rect(pos[0], pos[1],
2184 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002185
2186 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002187
Daniel Sandlerff02d492013-08-05 02:12:05 -04002188 mStats.recordLaunch(intent, shortcut);
2189
Michael Jurkaddd62e92011-02-16 17:49:14 -08002190 if (success && v instanceof BubbleTextView) {
2191 mWaitingForResume = (BubbleTextView) v;
2192 mWaitingForResume.setStayPressed(true);
2193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002195 if (v instanceof FolderIcon) {
2196 FolderIcon fi = (FolderIcon) v;
2197 handleFolderClick(fi);
2198 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002199 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002200 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002201 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002202 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002203 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002204 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 }
2206 }
2207
Michael Jurka0e260592010-06-30 17:07:39 -07002208 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002209 return false;
2210 }
2211
Michael Jurkaaf442092010-06-10 17:01:57 -07002212 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002213 * Event handler for the search button
2214 *
2215 * @param v The view that was clicked.
2216 */
2217 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002218 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2219
Amith Yamasania135ba82011-08-09 17:42:01 -07002220 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002221 }
2222
2223 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002224 * Event handler for the voice button
2225 *
2226 * @param v The view that was clicked.
2227 */
2228 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002229 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002230
Bjorn Bringertc459e522013-06-07 19:36:01 +01002231 startVoice();
2232 }
2233
2234 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002235 try {
2236 final SearchManager searchManager =
2237 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2238 ComponentName activityName = searchManager.getGlobalSearchActivity();
2239 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002240 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002241 if (activityName != null) {
2242 intent.setPackage(activityName.getPackageName());
2243 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002244 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002245 } catch (ActivityNotFoundException e) {
2246 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002247 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002248 startActivitySafely(null, intent, "onClickVoiceButton");
2249 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002250 }
2251
2252 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002253 * Event handler for the "grid" button that appears on the home screen, which
2254 * enters all apps mode.
2255 *
2256 * @param v The view that was clicked.
2257 */
2258 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002259 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002260 }
2261
2262 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002263 // Provide the same haptic feedback that the system offers for virtual keys.
2264 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002265 }
2266
Adam Cohen61f560d2013-09-30 15:58:20 -07002267 public void performHapticFeedbackOnTouchDown(View v) {
2268 // Provide the same haptic feedback that the system offers for virtual keys.
2269 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2270 }
2271
2272 public View.OnTouchListener getHapticFeedbackTouchListener() {
2273 if (mHapticFeedbackTouchListener == null) {
2274 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2275 @Override
2276 public boolean onTouch(View v, MotionEvent event) {
2277 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2278 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2279 }
2280 return false;
2281 }
2282 };
2283 }
2284 return mHapticFeedbackTouchListener;
2285 }
2286
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002287 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002288 if (!DISABLE_MARKET_BUTTON) {
2289 if (mAppMarketIntent != null) {
2290 startActivitySafely(v, mAppMarketIntent, "app market");
2291 } else {
2292 Log.e(TAG, "Invalid app market intent.");
2293 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002294 }
2295 }
2296
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002297 /**
2298 * Called when the user stops interacting with the launcher.
2299 * This implies that the user is now on the homescreen and is not doing housekeeping.
2300 */
2301 protected void onInteractionEnd() {}
2302
2303 /**
2304 * Called when the user starts interacting with the launcher.
2305 * The possible interactions are:
2306 * - open all apps
2307 * - reorder an app shortcut, or a widget
2308 * - open the overview mode.
2309 * This is a good time to stop doing things that only make sense
2310 * when the user is on the homescreen and not doing housekeeping.
2311 */
2312 protected void onInteractionBegin() {}
2313
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002314 void startApplicationDetailsActivity(ComponentName componentName) {
2315 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002316 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2317 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002318 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002319 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002320 }
2321
Michael Jurka1e2f4652013-07-08 18:03:46 -07002322 // returns true if the activity was started
2323 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002324 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002325 // System applications cannot be installed. For now, show a toast explaining that.
2326 // We may give them the option of disabling apps this way.
2327 int messageId = R.string.uninstall_system_app_text;
2328 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002329 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002330 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002331 String packageName = componentName.getPackageName();
2332 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002333 Intent intent = new Intent(
2334 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002335 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2336 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002337 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002338 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002339 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002340 }
2341
Michael Jurka86a720e2012-05-09 11:23:23 -07002342 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002344
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002346 // Only launch using the new animation if the shortcut has not opted out (this is a
2347 // private contract between launcher and may be ignored in the future).
2348 boolean useLaunchAnimation = (v != null) &&
2349 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2350 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002351 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2352 v.getMeasuredWidth(), v.getMeasuredHeight());
2353
2354 startActivity(intent, opts.toBundle());
2355 } else {
2356 startActivity(intent);
2357 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002358 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 } catch (SecurityException e) {
2360 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002361 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002363 "or use the exported attribute for this activity. "
2364 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002366 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002368
Michael Jurka86a720e2012-05-09 11:23:23 -07002369 boolean startActivitySafely(View v, Intent intent, Object tag) {
2370 boolean success = false;
2371 try {
2372 success = startActivity(v, intent, tag);
2373 } catch (ActivityNotFoundException e) {
2374 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2375 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2376 }
2377 return success;
2378 }
2379
Adam Cohena9cf38f2011-05-02 15:36:58 -07002380 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002381 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002382 Folder openFolder = mWorkspace.getFolderForTag(info);
2383
2384 // If the folder info reports that the associated folder is open, then verify that
2385 // it is actually opened. There have been a few instances where this gets out of sync.
2386 if (info.opened && openFolder == null) {
2387 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002388 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002389 info.opened = false;
2390 }
2391
Adam Cohenfb91f302012-06-11 15:45:18 -07002392 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 // Close any open folder
2394 closeFolder();
2395 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002396 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 } else {
2398 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 int folderScreen;
2400 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002401 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 // .. and close it
2403 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002404 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 // Close any folder open on the current screen
2406 closeFolder();
2407 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002408 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 }
2410 }
2411 }
2412 }
2413
Adam Cohen268c4752012-06-06 17:47:33 -07002414 /**
2415 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2416 * in the DragLayer in the exact absolute location of the original FolderIcon.
2417 */
2418 private void copyFolderIconToImage(FolderIcon fi) {
2419 final int width = fi.getMeasuredWidth();
2420 final int height = fi.getMeasuredHeight();
2421
2422 // Lazy load ImageView, Bitmap and Canvas
2423 if (mFolderIconImageView == null) {
2424 mFolderIconImageView = new ImageView(this);
2425 }
2426 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2427 mFolderIconBitmap.getHeight() != height) {
2428 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2429 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2430 }
2431
2432 DragLayer.LayoutParams lp;
2433 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2434 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2435 } else {
2436 lp = new DragLayer.LayoutParams(width, height);
2437 }
2438
Adam Cohen307fe232012-08-16 17:55:58 -07002439 // The layout from which the folder is being opened may be scaled, adjust the starting
2440 // view size by this scale factor.
2441 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002442 lp.customPosition = true;
2443 lp.x = mRectForFolderAnimation.left;
2444 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002445 lp.width = (int) (scale * width);
2446 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002447
2448 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2449 fi.draw(mFolderIconCanvas);
2450 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002451 if (fi.getFolder() != null) {
2452 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2453 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002454 }
Adam Cohen268c4752012-06-06 17:47:33 -07002455 // Just in case this image view is still in the drag layer from a previous animation,
2456 // we remove it and re-add it.
2457 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2458 mDragLayer.removeView(mFolderIconImageView);
2459 }
2460 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002461 if (fi.getFolder() != null) {
2462 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002463 }
Adam Cohen268c4752012-06-06 17:47:33 -07002464 }
2465
Adam Cohen2801caf2011-05-13 20:57:39 -07002466 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002467 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002468 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2469 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2470 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2471
Adam Cohenc51934b2011-07-26 21:07:43 -07002472 FolderInfo info = (FolderInfo) fi.getTag();
2473 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2474 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002475 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2476 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002477 }
2478
Adam Cohen268c4752012-06-06 17:47:33 -07002479 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2480 copyFolderIconToImage(fi);
2481 fi.setVisibility(View.INVISIBLE);
2482
Michael Jurka2ecf9952012-06-18 12:52:28 -07002483 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002484 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002485 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2486 oa.start();
2487 }
2488
Adam Cohen268c4752012-06-06 17:47:33 -07002489 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002490 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002491 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2492 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2493 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2494
Adam Cohen268c4752012-06-06 17:47:33 -07002495 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002496
Adam Cohen268c4752012-06-06 17:47:33 -07002497 // We remove and re-draw the FolderIcon in-case it has changed
2498 mDragLayer.removeView(mFolderIconImageView);
2499 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002500 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002501 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002502 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002503 oa.addListener(new AnimatorListenerAdapter() {
2504 @Override
2505 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002506 if (cl != null) {
2507 cl.clearFolderLeaveBehind();
2508 // Remove the ImageView copy of the FolderIcon and make the original visible.
2509 mDragLayer.removeView(mFolderIconImageView);
2510 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002511 }
2512 }
2513 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002514 oa.start();
2515 }
2516
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002518 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 * is animated relative to the specified View. If the View is null, no animation
2520 * is played.
2521 *
2522 * @param folderInfo The FolderInfo describing the folder to open.
2523 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002524 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002525 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002526 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527
Adam Cohena9cf38f2011-05-02 15:36:58 -07002528 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529
Adam Cohen4554ee12011-08-03 16:13:21 -07002530 // Just verify that the folder hasn't already been added to the DragLayer.
2531 // There was a one-off crash where the folder had a parent already.
2532 if (folder.getParent() == null) {
2533 mDragLayer.addView(folder);
2534 mDragController.addDropTarget((DropTarget) folder);
2535 } else {
2536 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2537 folder.getParent() + ").");
2538 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002539 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002540 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002541
2542 // Notify the accessibility manager that this folder "window" has appeared and occluded
2543 // the workspace items
2544 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2545 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002546 }
2547
2548 public void closeFolder() {
2549 Folder folder = mWorkspace.getOpenFolder();
2550 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002551 if (folder.isEditingName()) {
2552 folder.dismissEditingName();
2553 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002554 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002555
2556 // Dismiss the folder cling
2557 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002558 }
2559 }
2560
2561 void closeFolder(Folder folder) {
2562 folder.getInfo().opened = false;
2563
2564 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2565 if (parent != null) {
2566 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2567 shrinkAndFadeInFolderIcon(fi);
2568 }
2569 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002570
2571 // Notify the accessibility manager that this folder "window" has disappeard and no
2572 // longer occludeds the workspace items
2573 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 }
2575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002577 if (!isDraggingEnabled()) return false;
2578 if (isWorkspaceLocked()) return false;
2579 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580
Adam Cohen1697b792013-09-17 19:08:21 -07002581 if (v instanceof Workspace) {
2582 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002583 if (mWorkspace.enterOverviewMode()) {
2584 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2585 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2586 return true;
2587 } else {
2588 return false;
2589 }
Adam Cohen1697b792013-09-17 19:08:21 -07002590 }
Adam Cohen1697b792013-09-17 19:08:21 -07002591 }
2592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002594 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 }
2596
Michael Jurka0280c3b2010-09-17 15:00:07 -07002597 resetAddInfo();
2598 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002600 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 return true;
2602 }
2603
Winson Chung3d503fb2011-07-13 17:25:49 -07002604 // The hotseat touch handling does not go through Workspace, and we always allow long press
2605 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002606 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002607 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2608 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002609 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002610 // User long pressed on empty space
2611 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2612 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2613 // Disabling reordering until we sort out some issues.
2614 if (mWorkspace.isInOverviewMode()) {
2615 mWorkspace.startReordering(v);
2616 } else {
2617 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002618 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002619 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002620 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002621 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002622 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002623 }
2624 }
2625 }
2626 return true;
2627 }
2628
Winson Chung3d503fb2011-07-13 17:25:49 -07002629 boolean isHotseatLayout(View layout) {
2630 return mHotseat != null && layout != null &&
2631 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2632 }
2633 Hotseat getHotseat() {
2634 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002635 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002636 View getOverviewPanel() {
2637 return mOverviewPanel;
2638 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002639 SearchDropTargetBar getSearchBar() {
2640 return mSearchDropTargetBar;
2641 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002642
Winson Chung3d503fb2011-07-13 17:25:49 -07002643 /**
2644 * Returns the CellLayout of the specified container at the specified screen.
2645 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002646 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002647 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2648 if (mHotseat != null) {
2649 return mHotseat.getLayout();
2650 } else {
2651 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002652 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002653 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002654 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002655 }
2656 }
2657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 Workspace getWorkspace() {
2659 return mWorkspace;
2660 }
2661
Daniel Sandler843e8602010-06-07 14:59:01 -04002662 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002663 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002664 }
2665
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002666 /**
2667 * Helper method for the cameraZoomIn/cameraZoomOut animations
2668 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002669 * @param scaleFactor The scale factor used for the zoom
2670 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002671 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002673 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002674 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002675
Craig Mautner360310b2012-10-26 15:13:08 -07002676 private void setWorkspaceBackground(boolean workspace) {
2677 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002678 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002679 }
2680
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002681 void updateWallpaperVisibility(boolean visible) {
2682 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2683 int curflags = getWindow().getAttributes().flags
2684 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2685 if (wpflags != curflags) {
2686 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2687 }
Craig Mautner360310b2012-10-26 15:13:08 -07002688 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002689 }
2690
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002691 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2692 if (v instanceof LauncherTransitionable) {
2693 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2694 }
2695 }
2696
Michael Jurkabed61d22012-02-14 22:51:29 -08002697 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2698 if (v instanceof LauncherTransitionable) {
2699 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2700 }
Winson Chung70442722012-02-10 15:43:22 -08002701
2702 // Update the workspace transition step as well
2703 dispatchOnLauncherTransitionStep(v, 0f);
2704 }
2705
2706 private void dispatchOnLauncherTransitionStep(View v, float t) {
2707 if (v instanceof LauncherTransitionable) {
2708 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2709 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002710 }
2711
2712 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2713 if (v instanceof LauncherTransitionable) {
2714 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2715 }
Winson Chung70442722012-02-10 15:43:22 -08002716
2717 // Update the workspace transition step as well
2718 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002719 }
2720
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002721 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002722 * Things to test when changing the following seven functions.
2723 * - Home from workspace
2724 * - from center screen
2725 * - from other screens
2726 * - Home from all apps
2727 * - from center screen
2728 * - from other screens
2729 * - Back from all apps
2730 * - from center screen
2731 * - from other screens
2732 * - Launch app from workspace and quit
2733 * - with back
2734 * - with home
2735 * - Launch app from all apps and quit
2736 * - with back
2737 * - with home
2738 * - Go to a screen that's not the default, then all
2739 * apps, and launch and app, and go back
2740 * - with back
2741 * -with home
2742 * - On workspace, long press power and go back
2743 * - with back
2744 * - with home
2745 * - On all apps, long press power and go back
2746 * - with back
2747 * - with home
2748 * - On workspace, power off
2749 * - On all apps, power off
2750 * - Launch an app and turn off the screen while in that app
2751 * - Go back with home key
2752 * - Go back with back key TODO: make this not go to workspace
2753 * - From all apps
2754 * - From workspace
2755 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2756 * - From all apps
2757 * - From the center workspace
2758 * - From another workspace
2759 */
2760
2761 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002762 * Zoom the camera out from the workspace to reveal 'toView'.
2763 * Assumes that the view to show is anchored at either the very top or very bottom
2764 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002766 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002767 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2768 showAppsCustomizeHelper(animated, springLoaded, contentType);
2769 }
2770 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2771 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002772 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002773 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002774 mStateAnimation.cancel();
2775 mStateAnimation = null;
2776 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002777 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002778
Winson Chungf0ea4d32011-06-06 14:27:16 -07002779 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2780 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2781 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002782 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002783 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002784 final int startDelay =
2785 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002786
Michael Jurkab3e22d92011-10-31 15:58:33 -07002787 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002788
Michael Jurkad74c9842011-07-10 12:44:21 -07002789 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002790 Animator workspaceAnim =
2791 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002792 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2793 // Set the content type for the all apps space
2794 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2795 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002796
2797 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002798 toView.setScaleX(scale);
2799 toView.setScaleY(scale);
2800 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2801 scaleAnim.
2802 scaleX(1f).scaleY(1f).
2803 setDuration(duration).
2804 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002805
Winson Chungf0ea4d32011-06-06 14:27:16 -07002806 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002807 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002808 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002809 .ofFloat(toView, "alpha", 0f, 1f)
2810 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002811 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002812 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2813 @Override
2814 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002815 if (animation == null) {
2816 throw new RuntimeException("animation is null");
2817 }
Winson Chung70442722012-02-10 15:43:22 -08002818 float t = (Float) animation.getAnimatedValue();
2819 dispatchOnLauncherTransitionStep(fromView, t);
2820 dispatchOnLauncherTransitionStep(toView, t);
2821 }
2822 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002823
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002824 // toView should appear right at the end of the workspace shrink
2825 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002826 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002827 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002828 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002829
2830 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002831 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002832 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002833 // Prepare the position
2834 toView.setTranslationX(0.0f);
2835 toView.setTranslationY(0.0f);
2836 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002837 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002838 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002839 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002840 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002841 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2842 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002843
Winson Chungc7d2b602012-05-16 17:02:20 -07002844 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002845 if (mSearchDropTargetBar != null) {
2846 mSearchDropTargetBar.hideSearchBar(false);
2847 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002848 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002849 });
2850
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002851 if (workspaceAnim != null) {
2852 mStateAnimation.play(workspaceAnim);
2853 }
Michael Jurka1899a362011-11-03 13:50:45 -07002854
2855 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002856
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002857 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2858 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002859
2860 // If any of the objects being animated haven't been measured/laid out
2861 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002862 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002863 (mWorkspace.getMeasuredWidth() == 0) ||
2864 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002865 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002866 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002867
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002868 final AnimatorSet stateAnimation = mStateAnimation;
2869 final Runnable startAnimRunnable = new Runnable() {
2870 public void run() {
2871 // Check that mStateAnimation hasn't changed while
2872 // we waited for a layout/draw pass
2873 if (mStateAnimation != stateAnimation)
2874 return;
2875 setPivotsForZoom(toView, scale);
2876 dispatchOnLauncherTransitionStart(fromView, animated, false);
2877 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002878 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002879 }
2880 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002881 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002882 final ViewTreeObserver observer = toView.getViewTreeObserver();
2883 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2884 public void onGlobalLayout() {
2885 startAnimRunnable.run();
2886 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2887 }
2888 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002889 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002890 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002891 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002892 } else {
2893 toView.setTranslationX(0.0f);
2894 toView.setTranslationY(0.0f);
2895 toView.setScaleX(1.0f);
2896 toView.setScaleY(1.0f);
2897 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002898 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002899
Daniel Sandlere4f98912013-06-25 15:13:26 -04002900 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002901 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002902 if (mSearchDropTargetBar != null) {
2903 mSearchDropTargetBar.hideSearchBar(false);
2904 }
Michael Jurkaabded662011-03-04 12:06:57 -08002905 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002906 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002907 dispatchOnLauncherTransitionStart(fromView, animated, false);
2908 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002909 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002910 dispatchOnLauncherTransitionStart(toView, animated, false);
2911 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002912 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002913 }
2914
2915 /**
2916 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002917 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002918 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002919 */
Adam Cohened307df2013-10-02 09:37:31 -07002920 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08002921 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002922
Michael Jurkab3e22d92011-10-31 15:58:33 -07002923 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002924 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002925 mStateAnimation.cancel();
2926 mStateAnimation = null;
2927 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002928 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002929
Winson Chungf0ea4d32011-06-06 14:27:16 -07002930 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002931 final int fadeOutDuration =
2932 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002933 final float scaleFactor = (float)
2934 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2935 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002936 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002937 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07002938 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002939 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2940 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002941 toState, animated, stagger, -1);
2942 } else if (toState == Workspace.State.SPRING_LOADED ||
2943 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002944 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002945 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002946 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002947
Michael Jurkab3e22d92011-10-31 15:58:33 -07002948 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07002949 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002950 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002951 final LauncherViewPropertyAnimator scaleAnim =
2952 new LauncherViewPropertyAnimator(fromView);
2953 scaleAnim.
2954 scaleX(scaleFactor).scaleY(scaleFactor).
2955 setDuration(duration).
2956 setInterpolator(new Workspace.ZoomInInterpolator());
2957
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002958 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002959 .ofFloat(fromView, "alpha", 1f, 0f)
2960 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002961 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002962 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2963 @Override
2964 public void onAnimationUpdate(ValueAnimator animation) {
2965 float t = 1f - (Float) animation.getAnimatedValue();
2966 dispatchOnLauncherTransitionStep(fromView, t);
2967 dispatchOnLauncherTransitionStep(toView, t);
2968 }
2969 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002970
Michael Jurka2ecf9952012-06-18 12:52:28 -07002971 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002972
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002973 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2974 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002975 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002976
2977 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002978 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002979 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002980 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002981 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2982 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002983 if (onCompleteRunnable != null) {
2984 onCompleteRunnable.run();
2985 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002986 mAppsCustomizeContent.updateCurrentPageScroll();
2987 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002988 }
2989 });
2990
Winson Chungf0ea4d32011-06-06 14:27:16 -07002991 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002992 if (workspaceAnim != null) {
2993 mStateAnimation.play(workspaceAnim);
2994 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002995 dispatchOnLauncherTransitionStart(fromView, animated, true);
2996 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002997 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002998 } else {
2999 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003000 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003001 dispatchOnLauncherTransitionStart(fromView, animated, true);
3002 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003003 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003004 dispatchOnLauncherTransitionStart(toView, animated, true);
3005 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003006 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003007 }
3008
Michael Jurkae326f182011-11-21 14:05:46 -08003009 @Override
3010 public void onTrimMemory(int level) {
3011 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003012 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003013 mAppsCustomizeTabHost.onTrimMemory();
3014 }
3015 }
3016
Adam Cohened307df2013-10-02 09:37:31 -07003017 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003018 showWorkspace(animated, null);
3019 }
3020
Adam Cohened307df2013-10-02 09:37:31 -07003021 protected void showWorkspace() {
3022 showWorkspace(true);
3023 }
3024
Adam Cohened66b2b2012-01-23 17:28:51 -08003025 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003026 if (mWorkspace.isInOverviewMode()) {
3027 mWorkspace.exitOverviewMode(animated);
3028 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003029 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003030 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003031 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003032 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003033
Winson Chungc7d2b602012-05-16 17:02:20 -07003034 // Show the search bar (only animate if we were showing the drop target bar in spring
3035 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003036 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003037 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003038 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003039
Michael Jurkab3e22d92011-10-31 15:58:33 -07003040 // Set focus to the AppsCustomize button
3041 if (mAllAppsButton != null) {
3042 mAllAppsButton.requestFocus();
3043 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003044 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003045
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003046 // Change the state *after* we've called all the transition code
3047 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003048
Winson Chung5fb63472011-02-02 17:03:37 -08003049 // Resume the auto-advance of widgets
3050 mUserPresent = true;
3051 updateRunning();
3052
alanv1d4fde62012-10-17 13:15:47 -07003053 // Send an accessibility event to announce the context change
3054 getWindow().getDecorView()
3055 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003056
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003057 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003058 }
3059
Adam Cohened307df2013-10-02 09:37:31 -07003060 void showOverviewMode(boolean animated) {
3061 mWorkspace.setVisibility(View.VISIBLE);
3062 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3063 mState = State.WORKSPACE;
3064 onWorkspaceShown(animated);
3065 }
3066
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003067 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003068 }
3069
Winson Chung82963d52013-10-09 11:20:57 -07003070 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3071 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003072 if (mState != State.WORKSPACE) return;
3073
Winson Chung82963d52013-10-09 11:20:57 -07003074 if (resetPageToZero) {
3075 mAppsCustomizeTabHost.reset();
3076 }
Winson Chungc58497e2013-09-03 17:48:37 -07003077 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003078 mAppsCustomizeTabHost.requestFocus();
3079
Michael Jurkab3e22d92011-10-31 15:58:33 -07003080 // Change the state *after* we've called all the transition code
3081 mState = State.APPS_CUSTOMIZE;
3082
3083 // Pause the auto-advance of widgets until we are out of AllApps
3084 mUserPresent = false;
3085 updateRunning();
3086 closeFolder();
3087
3088 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003089 getWindow().getDecorView()
3090 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003091 }
3092
Adam Cohen7777d962011-08-18 18:58:38 -07003093 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003094 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003095 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003096 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003097 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003098 }
Adam Cohen7777d962011-08-18 18:58:38 -07003099
Adam Cohened66b2b2012-01-23 17:28:51 -08003100 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3101 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003102 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3103
Winson Chunge7a03942011-08-05 15:05:12 -07003104 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003105 @Override
3106 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003107 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003108 // Before we show workspace, hide all apps again because
3109 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3110 // clean up our state transition functions
3111 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003112 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003113 } else {
3114 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003115 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003116 }
3117 }, (extendedDelay ?
3118 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3119 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3120 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003121
Michael Jurkad3ef3062010-11-23 16:23:58 -08003122 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003123 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003124 final boolean animated = true;
3125 final boolean springLoaded = true;
3126 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003127 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003128 }
3129 // Otherwise, we are not in spring loaded mode, so don't do anything.
3130 }
3131
Michael Jurkab3e22d92011-10-31 15:58:33 -07003132 void lockAllApps() {
3133 // TODO
3134 }
3135
3136 void unlockAllApps() {
3137 // TODO
3138 }
3139
Winson Chungf0ea4d32011-06-06 14:27:16 -07003140 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003141 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003142 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003143 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003144 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003145 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003146 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003147 int duration = 0;
3148 if (mSearchDropTargetBar != null) {
3149 duration = mSearchDropTargetBar.getTransitionInDuration();
3150 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003151 mHotseat.animate().alpha(1f).setDuration(duration);
3152 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003153 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003154 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003155 }
3156 }
3157 }
3158
3159 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003161 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003162 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003163 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003164 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003165 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003166 int duration = 0;
3167 if (mSearchDropTargetBar != null) {
3168 duration = mSearchDropTargetBar.getTransitionOutDuration();
3169 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003170 mHotseat.animate().alpha(0f).setDuration(duration);
3171 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003172 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003173 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003174 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003175 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003176 }
3177
Patrick Dubroy5f445422011-02-18 14:35:21 -08003178 /**
3179 * Add an item from all apps or customize onto the given workspace screen.
3180 * If layout is null, add to the current screen.
3181 */
3182 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003183 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003184 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003185 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003186 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003187
Winson Chungdff8ebb2011-09-08 17:25:31 -07003188 /** Maps the current orientation to an index for referencing orientation correct global icons */
3189 private int getCurrentOrientationIndexForGlobalIcons() {
3190 // default - 0, landscape - 1
3191 switch (getResources().getConfiguration().orientation) {
3192 case Configuration.ORIENTATION_LANDSCAPE:
3193 return 1;
3194 default:
3195 return 0;
3196 }
3197 }
3198
Michael Jurkaae65ee32012-04-30 11:45:54 -07003199 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003200 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003201 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003202 // Look for the toolbar icon specified in the activity meta-data
3203 Bundle metaData = packageManager.getActivityInfo(
3204 activityName, PackageManager.GET_META_DATA).metaData;
3205 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003206 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003207 if (iconResId != 0) {
3208 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003209 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003210 }
3211 }
3212 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003213 // This can happen if the activity defines an invalid drawable
3214 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3215 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003216 } catch (Resources.NotFoundException nfe) {
3217 // This can happen if the activity defines an invalid drawable
3218 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3219 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003220 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003221 return null;
3222 }
3223
3224 // if successful in getting icon, return it; otherwise, set button to use default drawable
3225 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003226 int buttonId, ComponentName activityName, int fallbackDrawableId,
3227 String toolbarResourceName) {
3228 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003229 Resources r = getResources();
3230 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3231 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003232
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003233 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003234 // If we were unable to find the icon via the meta-data, use a generic one
3235 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003236 toolbarIcon = r.getDrawable(fallbackDrawableId);
3237 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003238 if (button != null) {
3239 button.setCompoundDrawables(toolbarIcon, null, null, null);
3240 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003241 return null;
3242 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003243 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003244 if (button != null) {
3245 button.setCompoundDrawables(toolbarIcon, null, null, null);
3246 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003247 return toolbarIcon.getConstantState();
3248 }
3249 }
3250
3251 // if successful in getting icon, return it; otherwise, set button to use default drawable
3252 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003253 int buttonId, ComponentName activityName, int fallbackDrawableId,
3254 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003255 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003256 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003257
Michael Jurka19e0fc52011-07-22 18:00:21 -07003258 if (button != null) {
3259 // If we were unable to find the icon via the meta-data, use a
3260 // generic one
3261 if (toolbarIcon == null) {
3262 button.setImageResource(fallbackDrawableId);
3263 } else {
3264 button.setImageDrawable(toolbarIcon);
3265 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003266 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003267
3268 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3269
Michael Jurka0423dcf2010-10-05 14:56:18 -07003270 }
3271
Winson Chung1b884092012-06-08 17:13:02 -07003272 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003273 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003274 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003275 }
3276
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003277 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003278 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003279 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003280 }
3281
Winson Chungbb185bd2011-11-21 12:31:42 -08003282 private void invalidatePressedFocusedStates(View container, View button) {
3283 if (container instanceof HolographicLinearLayout) {
3284 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3285 layout.invalidatePressedFocusedStates();
3286 } else if (button instanceof HolographicImageView) {
3287 HolographicImageView view = (HolographicImageView) button;
3288 view.invalidatePressedFocusedStates();
3289 }
3290 }
3291
Cristina Stancu476493b2013-08-07 17:20:14 +01003292 public View getQsbBar() {
3293 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003294 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3295 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003296 }
3297 return mQsbBar;
3298 }
3299
3300 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003301 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003302 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003303 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003304 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003305
Winson Chung1cad91e2011-05-25 17:41:01 -07003306 final SearchManager searchManager =
3307 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3308 ComponentName activityName = searchManager.getGlobalSearchActivity();
3309 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003310 int coi = getCurrentOrientationIndexForGlobalIcons();
3311 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003312 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3313 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3314 if (sGlobalSearchIcon[coi] == null) {
3315 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3316 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3317 TOOLBAR_ICON_METADATA_NAME);
3318 }
3319
Winson Chungc51db6a2011-10-05 11:44:49 -07003320 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3321 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003322 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003323 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003324 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003325 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003326 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3327 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003328 if (searchButton != null) searchButton.setVisibility(View.GONE);
3329 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003330 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003331 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003332 }
3333 }
3334
Cristina Stancu476493b2013-08-07 17:20:14 +01003335 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003336 final View searchButtonContainer = findViewById(R.id.search_button_container);
3337 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003338 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003339 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003340 }
3341
Cristina Stancu476493b2013-08-07 17:20:14 +01003342 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003343 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003344 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003345
Winson Chungc51db6a2011-10-05 11:44:49 -07003346 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003347 final SearchManager searchManager =
3348 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3349 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3350
3351 ComponentName activityName = null;
3352 if (globalSearchActivity != null) {
3353 // Check if the global search activity handles voice search
3354 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3355 intent.setPackage(globalSearchActivity.getPackageName());
3356 activityName = intent.resolveActivity(getPackageManager());
3357 }
3358
3359 if (activityName == null) {
3360 // Fallback: check if an activity other than the global search activity
3361 // resolves this
3362 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3363 activityName = intent.resolveActivity(getPackageManager());
3364 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003365 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003366 int coi = getCurrentOrientationIndexForGlobalIcons();
3367 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003368 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3369 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3370 if (sVoiceSearchIcon[coi] == null) {
3371 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3372 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3373 TOOLBAR_ICON_METADATA_NAME);
3374 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003375 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003376 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003377 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003378 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003379 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003380 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003381 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003382 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003383 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003384 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003385 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003386 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003387
Cristina Stancu476493b2013-08-07 17:20:14 +01003388 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003389 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3390 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003391 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003392 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003393 }
3394
Winson Chung5841efa2013-09-30 18:06:44 -07003395 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003396 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3397 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003398 boolean visible = !forceDisableVoiceButtonProxy &&
3399 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003400 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003401 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003402 }
3403 }
Winson Chung5841efa2013-09-30 18:06:44 -07003404
3405 /**
3406 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3407 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3408 */
3409 public void disableVoiceButtonProxy(boolean disabled) {
3410 updateVoiceButtonProxyVisible(disabled);
3411 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003412 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003413 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003414 */
3415 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003416 if (!DISABLE_MARKET_BUTTON) {
3417 final View marketButton = findViewById(R.id.market_button);
3418 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3419 // Find the app market activity by resolving an intent.
3420 // (If multiple app markets are installed, it will return the ResolverActivity.)
3421 ComponentName activityName = intent.resolveActivity(getPackageManager());
3422 if (activityName != null) {
3423 int coi = getCurrentOrientationIndexForGlobalIcons();
3424 mAppMarketIntent = intent;
3425 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3426 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3427 TOOLBAR_ICON_METADATA_NAME);
3428 marketButton.setVisibility(View.VISIBLE);
3429 } else {
3430 // We should hide and disable the view so that we don't try and restore the visibility
3431 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3432 marketButton.setVisibility(View.GONE);
3433 marketButton.setEnabled(false);
3434 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003435 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003436 }
3437
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003438 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003439 if (!DISABLE_MARKET_BUTTON) {
3440 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3441 Resources r = getResources();
3442 Drawable marketIconDrawable = d.newDrawable(r);
3443 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3444 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3445 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003446
Winson Chungd64a6662013-09-30 11:06:59 -07003447 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3448 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003449 }
3450
Michael Jurkad7c28052012-04-27 15:43:36 -07003451 @Override
3452 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003453 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003454 final List<CharSequence> text = event.getText();
3455 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003456 // Populate event with a fake title based on the current state.
3457 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003458 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003459 } else {
3460 text.add(getString(R.string.all_apps_home_button_label));
3461 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003462 return result;
3463 }
3464
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003465 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003466 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003467 */
3468 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3469 @Override
3470 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003471 closeSystemDialogs();
3472 }
3473 }
3474
3475 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003476 * Receives notifications whenever the appwidgets are reset.
3477 */
3478 private class AppWidgetResetObserver extends ContentObserver {
3479 public AppWidgetResetObserver() {
3480 super(new Handler());
3481 }
3482
3483 @Override
3484 public void onChange(boolean selfChange) {
3485 onAppWidgetReset();
3486 }
3487 }
3488
3489 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003490 * If the activity is currently paused, signal that we need to run the passed Runnable
3491 * in onResume.
3492 *
3493 * This needs to be called from incoming places where resources might have been loaded
3494 * while we are paused. That is becaues the Configuration might be wrong
3495 * when we're not running, and if it comes back to what it was when we
3496 * were paused, we are not restarted.
3497 *
3498 * Implementation of the method from LauncherModel.Callbacks.
3499 *
3500 * @return true if we are currently paused. The caller might be able to
3501 * skip some work in that case since we will come back again.
3502 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003503 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003504 if (mPaused) {
3505 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003506 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003507 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003508 }
3509 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003510 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003511 return true;
3512 } else {
3513 return false;
3514 }
3515 }
3516
Michael Jurkac402cd92013-05-20 15:49:32 +02003517 private boolean waitUntilResume(Runnable run) {
3518 return waitUntilResume(run, false);
3519 }
3520
Michael Jurka1e2f4652013-07-08 18:03:46 -07003521 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003522 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003523 }
3524
Michael Jurka7607c2f2013-04-03 14:33:19 -07003525 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003526 * If the activity is currently paused, signal that we need to re-run the loader
3527 * in onResume.
3528 *
3529 * This needs to be called from incoming places where resources might have been loaded
3530 * while we are paused. That is becaues the Configuration might be wrong
3531 * when we're not running, and if it comes back to what it was when we
3532 * were paused, we are not restarted.
3533 *
3534 * Implementation of the method from LauncherModel.Callbacks.
3535 *
3536 * @return true if we are currently paused. The caller might be able to
3537 * skip some work in that case since we will come back again.
3538 */
3539 public boolean setLoadOnResume() {
3540 if (mPaused) {
3541 Log.i(TAG, "setLoadOnResume");
3542 mOnResumeNeedsLoad = true;
3543 return true;
3544 } else {
3545 return false;
3546 }
3547 }
3548
3549 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003550 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003551 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003552 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003553 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003554 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003555 } else {
3556 return SCREEN_COUNT / 2;
3557 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003558 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003559
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 /**
3561 * Refreshes the shortcuts shown on the workspace.
3562 *
3563 * Implementation of the method from LauncherModel.Callbacks.
3564 */
3565 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003566 // If we're starting binding all over again, clear any bind calls we'd postponed in
3567 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3568 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003569 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003570
Winson Chungd64d1762013-08-20 14:37:16 -07003571 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003572 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003573 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003574
Michael Jurka05bf644e2011-11-30 20:28:53 -08003575 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003576 if (mHotseat != null) {
3577 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003578 }
3579 }
3580
Adam Cohendcd297f2013-06-18 13:13:40 -07003581 @Override
3582 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003583 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003584
Adam Cohen5084cba2013-09-03 12:01:16 -07003585 // If there are no screens, we need to have an empty screen
3586 if (orderedScreenIds.size() == 0) {
3587 mWorkspace.addExtraEmptyScreen();
3588 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003589
3590 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003591 // setCurrentPage() so ensure that all pages are added before calling this).
3592 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003593 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3594 mWorkspace.createCustomContentPage();
3595 }
Winson Chung64359a52013-07-08 17:17:08 -07003596 }
3597
3598 @Override
3599 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003600 int count = orderedScreenIds.size();
3601 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003602 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003603 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003604 }
3605
Adam Cohen39a06042013-07-19 14:30:12 -07003606 private boolean shouldShowWeightWatcher() {
3607 String spKey = LauncherAppState.getSharedPreferencesKey();
3608 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003609 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003610
3611 return show;
3612 }
3613
3614 private void toggleShowWeightWatcher() {
3615 String spKey = LauncherAppState.getSharedPreferencesKey();
3616 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3617 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3618
3619 show = !show;
3620
3621 SharedPreferences.Editor editor = sp.edit();
3622 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3623 editor.commit();
3624
3625 if (mWeightWatcher != null) {
3626 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3627 }
3628 }
3629
Winson Chungd64d1762013-08-20 14:37:16 -07003630 public void bindAppsAdded(final ArrayList<Long> newScreens,
3631 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003632 final ArrayList<ItemInfo> addAnimated,
3633 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003634 Runnable r = new Runnable() {
3635 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003636 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003637 }
3638 };
3639 if (waitUntilResume(r)) {
3640 return;
3641 }
3642
Winson Chungd64d1762013-08-20 14:37:16 -07003643 // Add the new screens
3644 bindAddScreens(newScreens);
3645
3646 // We add the items without animation on non-visible pages, and with
3647 // animations on the new page (which we will try and snap to).
3648 if (!addNotAnimated.isEmpty()) {
3649 bindItems(addNotAnimated, 0,
3650 addNotAnimated.size(), false);
3651 }
3652 if (!addAnimated.isEmpty()) {
3653 bindItems(addAnimated, 0,
3654 addAnimated.size(), true);
3655 }
Winson Chungc58497e2013-09-03 17:48:37 -07003656
Winson Chung87412982013-10-03 18:34:14 -07003657 // Remove the extra empty screen
3658 mWorkspace.removeExtraEmptyScreen();
3659
Winson Chungc58497e2013-09-03 17:48:37 -07003660 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3661 addedApps != null && mAppsCustomizeContent != null) {
3662 mAppsCustomizeContent.addApps(addedApps);
3663 }
Winson Chungd64d1762013-08-20 14:37:16 -07003664 }
3665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003667 * Bind the items start-end from the list.
3668 *
3669 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003670 */
Winson Chung64359a52013-07-08 17:17:08 -07003671 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3672 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003673 Runnable r = new Runnable() {
3674 public void run() {
3675 bindItems(shortcuts, start, end, forceAnimateIcons);
3676 }
3677 };
3678 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003679 return;
3680 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003681
Winson Chungf0c6ae02012-03-21 16:10:31 -07003682 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003683 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3684 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003685 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003686 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003687 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003688 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003690
3691 // Short circuit if we are loading dock items for a configuration which has no dock
3692 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3693 mHotseat == null) {
3694 continue;
3695 }
3696
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 switch (item.itemType) {
3698 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3699 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003700 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003701 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003702
Winson Chung64359a52013-07-08 17:17:08 -07003703 /*
3704 * TODO: FIX collision case
3705 */
Winson Chungce376632013-07-11 16:12:41 -07003706 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3707 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3708 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3709 throw new RuntimeException("OCCUPIED");
3710 }
Winson Chung64359a52013-07-08 17:17:08 -07003711 }
3712
Adam Cohendcd297f2013-06-18 13:13:40 -07003713 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3714 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003715 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003716 // Animate all the applications up now
3717 shortcut.setAlpha(0f);
3718 shortcut.setScaleX(0f);
3719 shortcut.setScaleY(0f);
3720 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003721 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003722 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003724 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003725 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003726 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003727 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003728 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3729 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003731 default:
3732 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003734 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003735
Winson Chung997a9232013-07-24 15:33:46 -07003736 if (animateIcons) {
3737 // Animate to the correct page
3738 if (newShortcutsScreenId > -1) {
3739 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003740 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003741 final Runnable startBounceAnimRunnable = new Runnable() {
3742 public void run() {
3743 anim.playTogether(bounceAnims);
3744 anim.start();
3745 }
3746 };
Winson Chung997a9232013-07-24 15:33:46 -07003747 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003748 // We post the animation slightly delayed to prevent slowdowns
3749 // when we are loading right after we return to launcher.
3750 mWorkspace.postDelayed(new Runnable() {
3751 public void run() {
3752 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003753 mWorkspace.postDelayed(startBounceAnimRunnable,
3754 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003755 }
3756 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003757 } else {
3758 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003759 }
3760 }
Winson Chung64359a52013-07-08 17:17:08 -07003761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003763 }
3764
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 /**
3766 * Implementation of the method from LauncherModel.Callbacks.
3767 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003768 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003769 Runnable r = new Runnable() {
3770 public void run() {
3771 bindFolders(folders);
3772 }
3773 };
3774 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003775 return;
3776 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003777 sFolders.clear();
3778 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003779 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003780
3781 /**
3782 * Add the views for a widget to the workspace.
3783 *
3784 * Implementation of the method from LauncherModel.Callbacks.
3785 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003786 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003787 Runnable r = new Runnable() {
3788 public void run() {
3789 bindAppWidget(item);
3790 }
3791 };
3792 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003793 return;
3794 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003795
Daniel Sandler843e8602010-06-07 14:59:01 -04003796 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3797 if (DEBUG_WIDGETS) {
3798 Log.d(TAG, "bindAppWidget: " + item);
3799 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 final Workspace workspace = mWorkspace;
3801
3802 final int appWidgetId = item.appWidgetId;
3803 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003804 if (DEBUG_WIDGETS) {
3805 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3806 }
3807
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3809
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003811 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003812
Adam Cohendcd297f2013-06-18 13:13:40 -07003813 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003814 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003815 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3816
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 workspace.requestLayout();
3818
Daniel Sandler843e8602010-06-07 14:59:01 -04003819 if (DEBUG_WIDGETS) {
3820 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3821 + (SystemClock.uptimeMillis()-start) + "ms");
3822 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 }
3824
Adam Cohen1462de32012-07-24 22:34:36 -07003825 public void onPageBoundSynchronously(int page) {
3826 mSynchronouslyBoundPages.add(page);
3827 }
3828
Joe Onorato9c1289c2009-08-17 11:03:03 -04003829 /**
3830 * Callback saying that there aren't any more items to bind.
3831 *
3832 * Implementation of the method from LauncherModel.Callbacks.
3833 */
Adam Cohene25af792013-06-06 23:08:25 -07003834 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003835 Runnable r = new Runnable() {
3836 public void run() {
3837 finishBindingItems(upgradePath);
3838 }
3839 };
3840 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003841 return;
3842 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003843 if (mSavedState != null) {
3844 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003845 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003846 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003847 mSavedState = null;
3848 }
3849
Adam Cohen1462de32012-07-24 22:34:36 -07003850 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003852 // If we received the result of any pending adds while the loader was running (e.g. the
3853 // widget configuration forced an orientation change), process them now.
3854 for (int i = 0; i < sPendingAddList.size(); i++) {
3855 completeAdd(sPendingAddList.get(i));
3856 }
3857 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003858
Winson Chungc51db6a2011-10-05 11:44:49 -07003859 // Update the market app icon as necessary (the other icons will be managed in response to
3860 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003861 if (!DISABLE_MARKET_BUTTON) {
3862 updateAppMarketIcon();
3863 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003864
Winson Chungf0c6ae02012-03-21 16:10:31 -07003865 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003866 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003867 mWorkspace.getUniqueComponents(true, null);
3868 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003869 }
Adam Cohen99894d92013-06-14 11:22:59 -07003870
Adam Cohen66a01fd2013-06-11 12:48:00 -07003871 mWorkspace.post(new Runnable() {
3872 @Override
3873 public void run() {
3874 onFinishBindingItems();
3875 }
3876 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003877 }
3878
Winson Chunga0b7e862013-09-05 16:03:15 -07003879 public boolean isAllAppsButtonRank(int rank) {
3880 if (mHotseat != null) {
3881 return mHotseat.isAllAppsButtonRank(rank);
3882 }
3883 return false;
3884 }
3885
Winson Chunga2413752012-04-03 14:22:34 -07003886 private boolean canRunNewAppsAnimation() {
3887 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3888 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3889 }
3890
Winson Chungc9168342013-06-26 14:54:55 -07003891 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3892 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3893 PropertyValuesHolder.ofFloat("alpha", 1f),
3894 PropertyValuesHolder.ofFloat("scaleX", 1f),
3895 PropertyValuesHolder.ofFloat("scaleY", 1f));
3896 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3897 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3898 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3899 return bounceAnim;
3900 }
3901
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003902 @Override
3903 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003904 boolean searchVisible = updateGlobalSearchIcon();
3905 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003906 if (mSearchDropTargetBar != null) {
3907 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3908 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003909 }
3910
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003911 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003912 * Add the icons for all apps.
3913 *
3914 * Implementation of the method from LauncherModel.Callbacks.
3915 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003916 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003917 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3918 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3919 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3920 getHotseat().addAllAppsFolder(mIconCache, apps,
3921 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3922 }
3923 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003924 }
Winson Chungc58497e2013-09-03 17:48:37 -07003925 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07003926 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07003927 mAppsCustomizeContent.setApps(apps);
3928 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003929 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003930 }
3931
3932 /**
3933 * A package was updated.
3934 *
3935 * Implementation of the method from LauncherModel.Callbacks.
3936 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003937 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003938 Runnable r = new Runnable() {
3939 public void run() {
3940 bindAppsUpdated(apps);
3941 }
3942 };
3943 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003944 return;
3945 }
3946
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003947 if (mWorkspace != null) {
3948 mWorkspace.updateShortcuts(apps);
3949 }
Winson Chungc58497e2013-09-03 17:48:37 -07003950
3951 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3952 mAppsCustomizeContent != null) {
3953 mAppsCustomizeContent.updateApps(apps);
3954 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003955 }
3956
3957 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003958 * A package was uninstalled. We take both the super set of packageNames
3959 * in addition to specific applications to remove, the reason being that
3960 * this can be called when a package is updated as well. In that scenario,
3961 * we only remove specific components from the workspace, where as
3962 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003963 *
3964 * Implementation of the method from LauncherModel.Callbacks.
3965 */
Winson Chung83892cc2013-05-01 16:53:33 -07003966 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003967 final ArrayList<AppInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003968 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003969 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003970 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003971 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003972 }
Winson Chungd64d1762013-08-20 14:37:16 -07003973 };
3974 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003975 return;
3976 }
3977
Winson Chung64359a52013-07-08 17:17:08 -07003978 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003979 mWorkspace.removeItemsByPackageName(packageNames);
3980 } else {
3981 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003982 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003983
Winson Chunga1820962011-10-03 16:31:06 -07003984 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003985 mDragController.onAppsRemoved(appInfos, this);
Winson Chungc58497e2013-09-03 17:48:37 -07003986
3987 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3988 mAppsCustomizeContent != null) {
3989 mAppsCustomizeContent.removeApps(appInfos);
3990 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003991 }
Joe Onoratobe386092009-11-17 17:32:16 -08003992
3993 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003994 * A number of packages were updated.
3995 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003996 private ArrayList<Object> mWidgetsAndShortcuts;
3997 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003998 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003999 bindPackagesUpdated(mWidgetsAndShortcuts);
4000 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004001 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004002 };
4003
4004 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4005 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4006 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004007 return;
4008 }
4009
Winson Chung64359a52013-07-08 17:17:08 -07004010 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004011 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4012 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004013 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004014 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004015 }
4016
Winson Chung400438b2011-01-16 17:53:48 -08004017 private int mapConfigurationOriActivityInfoOri(int configOri) {
4018 final Display d = getWindowManager().getDefaultDisplay();
4019 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4020 switch (d.getRotation()) {
4021 case Surface.ROTATION_0:
4022 case Surface.ROTATION_180:
4023 // We are currently in the same basic orientation as the natural orientation
4024 naturalOri = configOri;
4025 break;
4026 case Surface.ROTATION_90:
4027 case Surface.ROTATION_270:
4028 // We are currently in the other basic orientation to the natural orientation
4029 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4030 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4031 break;
4032 }
4033
4034 int[] oriMap = {
4035 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4036 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4037 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4038 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4039 };
4040 // Since the map starts at portrait, we need to offset if this device's natural orientation
4041 // is landscape.
4042 int indexOffset = 0;
4043 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4044 indexOffset = 1;
4045 }
4046 return oriMap[(d.getRotation() + indexOffset) % 4];
4047 }
Adam Cohen446e9402011-09-15 18:21:21 -07004048
Winson Chung4b919f82012-05-01 10:44:08 -07004049 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004050 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004051 getResources().getBoolean(R.bool.allow_rotation);
4052 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004053 }
Winson Chung4b919f82012-05-01 10:44:08 -07004054 public void lockScreenOrientation() {
4055 if (isRotationEnabled()) {
4056 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4057 .getConfiguration().orientation));
4058 }
4059 }
4060 public void unlockScreenOrientation(boolean immediate) {
4061 if (isRotationEnabled()) {
4062 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004063 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004064 } else {
4065 mHandler.postDelayed(new Runnable() {
4066 public void run() {
4067 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4068 }
4069 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004070 }
Winson Chung4b919f82012-05-01 10:44:08 -07004071 }
Winson Chung400438b2011-01-16 17:53:48 -08004072 }
4073
Winson Chung82f55532011-08-09 14:14:23 -07004074 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004075 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004076 if (DISABLE_CLINGS) {
4077 return false;
4078 }
4079
Brett Chabot2a9e2282011-08-23 15:03:13 -07004080 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004081 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004082
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004083 // Disable clings for accessibility when explore by touch is enabled
4084 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4085 ACCESSIBILITY_SERVICE);
4086 if (a11yManager.isTouchExplorationEnabled()) {
4087 return false;
4088 }
4089
Michael Jurkae233a8b2013-03-19 13:49:20 +01004090 // Restricted secondary users (child mode) will potentially have very few apps
4091 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004092// boolean supportsLimitedUsers =
4093// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4094// Account[] accounts = AccountManager.get(this).getAccounts();
4095// if (supportsLimitedUsers && accounts.length == 0) {
4096// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4097// Bundle restrictions = um.getUserRestrictions();
4098// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4099// return false;
4100// }
4101// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004102 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004103 }
Michael Jurka22143132012-10-04 17:42:38 +02004104
Winson Chungfa545132013-09-26 17:45:32 -07004105 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004106 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004107 Cling cling = (Cling) findViewById(clingId);
4108 View scrim = null;
4109 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004110 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004111 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004112 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004113 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004114 cling.show(animate, SHOW_CLING_DURATION);
4115
4116 if (dimNavBarVisibilty) {
4117 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4118 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004119 }
4120 }
4121 return cling;
4122 }
Michael Jurka22143132012-10-04 17:42:38 +02004123
Winson Chungaf40f202013-09-18 18:26:31 -07004124 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4125 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004126 // To catch cases where siblings of top-level views are made invisible, just check whether
4127 // the cling is directly set to GONE before dismissing it.
4128 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004129 final Runnable cleanUpClingCb = new Runnable() {
4130 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004131 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004132 // We should update the shared preferences on a background thread
4133 new Thread("dismissClingThread") {
4134 public void run() {
4135 SharedPreferences.Editor editor = mSharedPrefs.edit();
4136 editor.putBoolean(flag, true);
4137 editor.commit();
4138 }
4139 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004140 if (postAnimationCb != null) {
4141 postAnimationCb.run();
4142 }
4143 }
4144 };
4145 if (duration <= 0) {
4146 cleanUpClingCb.run();
4147 } else {
4148 cling.hide(duration, cleanUpClingCb);
4149 }
Michael Jurka22143132012-10-04 17:42:38 +02004150 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004151
4152 if (restoreNavBarVisibilty) {
4153 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4154 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4155 }
Winson Chung82f55532011-08-09 14:14:23 -07004156 }
4157 }
Michael Jurka22143132012-10-04 17:42:38 +02004158
Winson Chung9d9d74f2011-09-19 11:49:12 -07004159 private void removeCling(int id) {
4160 final View cling = findViewById(id);
4161 if (cling != null) {
4162 final ViewGroup parent = (ViewGroup) cling.getParent();
4163 parent.post(new Runnable() {
4164 @Override
4165 public void run() {
4166 parent.removeView(cling);
4167 }
4168 });
Michael Jurka22143132012-10-04 17:42:38 +02004169 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004170 }
4171 }
Michael Jurka974c3862012-05-22 22:00:31 -07004172
4173 private boolean skipCustomClingIfNoAccounts() {
4174 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4175 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4176 if (customCling) {
4177 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004178 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004179 Account[] accounts = am.getAccountsByType("com.google");
4180 return accounts.length == 0;
4181 }
4182 return false;
4183 }
4184
Winson Chung82e5c982013-10-09 12:04:50 -07004185 public void updateCustomContentHintVisibility() {
4186 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4187 String ccHintStr = getFirstRunCustomContentHint();
4188
4189 if (mWorkspace.hasCustomContent()) {
4190 // Show the custom content hint if ccHintStr is not empty
4191 if (cling != null) {
4192 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4193 }
4194 } else {
4195 // Hide the custom content hint
4196 if (cling != null) {
4197 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4198 }
4199 }
4200 }
4201
4202 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4203 boolean animate) {
4204 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4205 if (ccHint != null) {
4206 if (visible && !ccHintStr.isEmpty()) {
4207 ccHint.setText(ccHintStr);
4208 ccHint.setVisibility(View.VISIBLE);
4209 if (animate) {
4210 ccHint.setAlpha(0f);
4211 ccHint.animate().alpha(1f)
4212 .setDuration(SHOW_CLING_DURATION)
4213 .start();
4214 } else {
4215 ccHint.setAlpha(1f);
4216 }
4217 } else {
4218 if (animate) {
4219 ccHint.animate().alpha(0f)
4220 .setDuration(SHOW_CLING_DURATION)
4221 .setListener(new AnimatorListenerAdapter() {
4222 @Override
4223 public void onAnimationEnd(Animator animation) {
4224 ccHint.setVisibility(View.GONE);
4225 }
4226 })
4227 .start();
4228 } else {
4229 ccHint.setAlpha(0f);
4230 ccHint.setVisibility(View.GONE);
4231 }
4232 }
4233 }
4234 }
4235
Winson Chungaf40f202013-09-18 18:26:31 -07004236 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004237 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004238 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004239 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004240 // If we're not using the default workspace layout, replace workspace cling
4241 // with a custom workspace cling (usually specified in an overlay)
4242 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004243 if (!DISABLE_CUSTOM_CLINGS) {
4244 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4245 getResources().getBoolean(R.bool.config_useCustomClings)) {
4246 // Use a custom cling
4247 View cling = findViewById(R.id.workspace_cling);
4248 ViewGroup clingParent = (ViewGroup) cling.getParent();
4249 int clingIndex = clingParent.indexOfChild(cling);
4250 clingParent.removeViewAt(clingIndex);
4251 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4252 clingParent.addView(customCling, clingIndex);
4253 customCling.setId(R.id.workspace_cling);
4254 }
4255 }
4256 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4257 if (cling != null) {
4258 String sbHintStr = getFirstRunClingSearchBarHint();
4259 String ccHintStr = getFirstRunCustomContentHint();
4260 if (!sbHintStr.isEmpty()) {
4261 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4262 sbHint.setText(sbHintStr);
4263 sbHint.setVisibility(View.VISIBLE);
4264 }
Winson Chung82e5c982013-10-09 12:04:50 -07004265 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004266 }
Winson Chungfa545132013-09-26 17:45:32 -07004267 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004268 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004269 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004270 }
4271 }
Winson Chungaf40f202013-09-18 18:26:31 -07004272
Winson Chunge6eabff2013-09-24 11:01:23 -07004273 protected String getFirstRunClingSearchBarHint() {
4274 return "";
4275 }
4276 protected String getFirstRunCustomContentHint() {
4277 return "";
4278 }
4279
Winson Chungaf40f202013-09-18 18:26:31 -07004280 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004281 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004282 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004283 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004284 initCling(R.id.workspace_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004285 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004286 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004287 }
4288 }
4289 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004290 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004291 if (isClingsEnabled() &&
4292 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004293 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4294 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004295 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004296 } else {
4297 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004298 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004299 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004300 }
Michael Jurka104c4562013-07-08 18:03:46 -07004301 protected SharedPreferences getSharedPrefs() {
4302 return mSharedPrefs;
4303 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004304 public boolean isFolderClingVisible() {
4305 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004306 if (cling != null) {
4307 return cling.getVisibility() == View.VISIBLE;
4308 }
4309 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004310 }
Winson Chungaf40f202013-09-18 18:26:31 -07004311 public void dismissFirstRunCling(View v) {
4312 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4313 Runnable cb = new Runnable() {
4314 public void run() {
4315 // Show the workspace cling next
4316 showFirstRunWorkspaceCling();
4317 }
4318 };
4319 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4320 DISMISS_CLING_DURATION, false);
4321 }
Winson Chung82f55532011-08-09 14:14:23 -07004322 public void dismissWorkspaceCling(View v) {
4323 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004324 Runnable cb = null;
4325 if (v == null) {
4326 cb = new Runnable() {
4327 public void run() {
4328 mWorkspace.enterOverviewMode();
4329 }
4330 };
4331 }
4332 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4333 DISMISS_CLING_DURATION, true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004334 }
4335 public void dismissFolderCling(View v) {
4336 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004337 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4338 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004339 }
4340
Winson Chung80baf5a2010-08-09 16:03:15 -07004341 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004342 * Prints out out state for debugging.
4343 */
4344 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004345 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004346 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004347 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4348 Log.d(TAG, "mRestoring=" + mRestoring);
4349 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4350 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004351 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004352 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004353
Winson Chung785d2eb2011-04-14 16:08:02 -07004354 if (mAppsCustomizeContent != null) {
4355 mAppsCustomizeContent.dumpState();
4356 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004357 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004358 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004359
4360 @Override
4361 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4362 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004363 synchronized (sDumpLogs) {
4364 writer.println(" ");
4365 writer.println("Debug logs: ");
4366 for (int i = 0; i < sDumpLogs.size(); i++) {
4367 writer.println(" " + sDumpLogs.get(i));
4368 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004369 }
4370 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004371
4372 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004373 if (DEBUG_DUMP_LOG) {
4374 synchronized (sDumpLogs) {
4375 Log.d(TAG, "");
4376 Log.d(TAG, "*********************");
4377 Log.d(TAG, "Launcher debug logs: ");
4378 for (int i = 0; i < sDumpLogs.size(); i++) {
4379 Log.d(TAG, " " + sDumpLogs.get(i));
4380 }
4381 Log.d(TAG, "*********************");
4382 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004383 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004384 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004385 }
4386
4387 public static void addDumpLog(String tag, String log, boolean debugLog) {
4388 if (debugLog) {
4389 Log.d(tag, log);
4390 }
Winson Chungede41292013-09-19 16:27:36 -07004391 if (DEBUG_DUMP_LOG) {
4392 sDateStamp.setTime(System.currentTimeMillis());
4393 synchronized (sDumpLogs) {
4394 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004395 }
Winson Chungede41292013-09-19 16:27:36 -07004396 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004397 }
4398
Winson Chungede41292013-09-19 16:27:36 -07004399 public void dumpLogsToLocalData() {
4400 if (DEBUG_DUMP_LOG) {
4401 new Thread("DumpLogsToLocalData") {
4402 @Override
4403 public void run() {
4404 boolean success = false;
4405 sDateStamp.setTime(sRunStart);
4406 String FILENAME = sDateStamp.getMonth() + "-"
4407 + sDateStamp.getDay() + "_"
4408 + sDateStamp.getHours() + "-"
4409 + sDateStamp.getMinutes() + "_"
4410 + sDateStamp.getSeconds() + ".txt";
4411
4412 FileOutputStream fos = null;
4413 File outFile = null;
4414 try {
4415 outFile = new File(getFilesDir(), FILENAME);
4416 outFile.createNewFile();
4417 fos = new FileOutputStream(outFile);
4418 } catch (Exception e) {
4419 e.printStackTrace();
4420 }
4421 if (fos != null) {
4422 PrintWriter writer = new PrintWriter(fos);
4423
4424 writer.println(" ");
4425 writer.println("Debug logs: ");
4426 synchronized (sDumpLogs) {
4427 for (int i = 0; i < sDumpLogs.size(); i++) {
4428 writer.println(" " + sDumpLogs.get(i));
4429 }
4430 }
4431 writer.close();
4432 }
4433 try {
4434 if (fos != null) {
4435 fos.close();
4436 success = true;
4437 }
4438 } catch (IOException e) {
4439 e.printStackTrace();
4440 }
4441 }
4442 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004443 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004444 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004445}
Michael Jurka2763be32011-02-24 11:19:57 -08004446
Michael Jurkaabded662011-03-04 12:06:57 -08004447interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004448 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004449 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004450 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004451 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004452 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004453}