blob: b76379add274eb8e9a6d3000d393327161c11715 [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";
Adam Cohen4637b5a2013-11-04 18:21:24 -0800184 // Type: parcelable
185 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100187 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700188 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100189 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700190 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100191 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700192
Adam Cohen39a06042013-07-19 14:30:12 -0700193 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700194 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700195
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700196 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700197 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700198 private State mState = State.WORKSPACE;
199 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700200
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700202 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
203 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Winson Chungaf40f202013-09-18 18:26:31 -0700204 private static final int SHOW_CLING_DURATION = 250;
205 private static final int DISMISS_CLING_DURATION = 200;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800208 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Winson Chunga2413752012-04-03 14:22:34 -0700210 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700211 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
212 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700213 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700214
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800215 private final BroadcastReceiver mCloseSystemDialogsReceiver
216 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800217 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private LayoutInflater mInflater;
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700222 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800223 private DragLayer mDragLayer;
224 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700225 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700226
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700227 private AppWidgetManager mAppWidgetManager;
228 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700229
Michael Jurkac9d95c52011-08-29 14:03:34 -0700230 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700231 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800232 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700233
Michael Jurka0280c3b2010-09-17 15:00:07 -0700234 private int[] mTmpAddItemCellCoordinates = new int[2];
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private FolderInfo mFolderInfo;
237
Winson Chung3d503fb2011-07-13 17:25:49 -0700238 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700239 private View mOverviewPanel;
240
Michael Jurka838a4ca2011-02-07 13:33:06 -0800241 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700242
Winson Chungc51db6a2011-10-05 11:44:49 -0700243 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700244 private AppsCustomizeTabHost mAppsCustomizeTabHost;
245 private AppsCustomizePagedView mAppsCustomizeContent;
246 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100247 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700250 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
251 // scroll issues (because the workspace may not have been measured yet) and extra work.
252 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
253 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254
255 private SpannableStringBuilder mDefaultKeySsb = null;
256
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257 private boolean mWorkspaceLoading = true;
258
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800259 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private boolean mRestoring;
261 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700262 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
Michael Jurka1e2f4652013-07-08 18:03:46 -0700264 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700265 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
266
Winson Chung4a2afa32012-07-19 14:53:05 -0700267 // Keep track of whether the user has left launcher
268 private static boolean sPausedFromUserAction = false;
269
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 private Bundle mSavedInstanceState;
271
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800273 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800274 private boolean mUserPresent = true;
275 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700276 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800277 private boolean mAttached = false;
Winson Chungfa545132013-09-26 17:45:32 -0700278 private static final boolean DISABLE_CLINGS = false;
Winson Chunge6eabff2013-09-24 11:01:23 -0700279 private static final boolean DISABLE_CUSTOM_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400280
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700281 private static LocaleConfiguration sLocaleConfiguration = null;
282
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700283 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700284
Adam Cohen61f560d2013-09-30 15:58:20 -0700285 private View.OnTouchListener mHapticFeedbackTouchListener;
286
Adam Cohended9f8d2010-11-03 13:25:16 -0700287 // Related to the auto-advancing of widgets
288 private final int ADVANCE_MSG = 1;
289 private final int mAdvanceInterval = 20000;
290 private final int mAdvanceStagger = 250;
291 private long mAutoAdvanceSentTime;
292 private long mAutoAdvanceTimeLeft = -1;
293 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
294 new HashMap<View, AppWidgetProviderInfo>();
295
Winson Chung400438b2011-01-16 17:53:48 -0800296 // Determines how long to wait after a rotation before restoring the screen orientation to
297 // match the sensor state.
298 private final int mRestoreScreenOrientationDelay = 500;
299
Michael Jurka4ef207b2010-11-29 17:05:45 -0800300 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700301 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
302 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
303 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800304
Winson Chungd64a6662013-09-30 11:06:59 -0700305 private Intent mAppMarketIntent = null;
306 private static final boolean DISABLE_MARKET_BUTTON = true;
307
Craig Mautner360310b2012-10-26 15:13:08 -0700308 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700309
Adam Cohen1462de32012-07-24 22:34:36 -0700310 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700311 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700312
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700313 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700314 static Date sDateStamp = new Date();
315 static DateFormat sDateFormat =
316 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
317 static long sRunStart = System.currentTimeMillis();
318 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700319
Winson Chung46353de2012-02-16 14:05:10 -0800320 // We only want to get the SharedPreferences once since it does an FS stat each time we get
321 // it from the context.
322 private SharedPreferences mSharedPrefs;
323
Adam Cohene25af792013-06-06 23:08:25 -0700324 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
325
Winson Chungf0c6ae02012-03-21 16:10:31 -0700326 // Holds the page that we need to animate to, and the icon views that we need to animate up
327 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700328 private ImageView mFolderIconImageView;
329 private Bitmap mFolderIconBitmap;
330 private Canvas mFolderIconCanvas;
331 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700332
Michael Jurkaddd62e92011-02-16 17:49:14 -0800333 private BubbleTextView mWaitingForResume;
334
Michael Jurka22143132012-10-04 17:42:38 +0200335 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
336 = new HideFromAccessibilityHelper();
337
Michael Jurkac1f5d262011-09-30 19:32:27 -0700338 private Runnable mBuildLayersRunnable = new Runnable() {
339 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700340 if (mWorkspace != null) {
341 mWorkspace.buildPageHardwareLayers();
342 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700343 }
344 };
345
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800346 private static ArrayList<PendingAddArguments> sPendingAddList
347 = new ArrayList<PendingAddArguments>();
348
Michael Jurka7267fa52013-09-26 15:29:57 -0700349 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800350
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800351 private static class PendingAddArguments {
352 int requestCode;
353 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700354 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700355 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 int cellX;
357 int cellY;
358 }
359
Daniel Sandlerff02d492013-08-05 02:12:05 -0400360 private Stats mStats;
361
Michael Jurka0a457bf2012-11-19 14:05:05 -0800362 private static boolean isPropertyEnabled(String propertyName) {
363 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700364 }
365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 @Override
367 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700368 if (DEBUG_STRICT_MODE) {
369 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
370 .detectDiskReads()
371 .detectDiskWrites()
372 .detectNetwork() // or .detectAll() for all detectable problems
373 .penaltyLog()
374 .build());
375 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
376 .detectLeakedSqlLiteObjects()
377 .detectLeakedClosableObjects()
378 .penaltyLog()
379 .penaltyDeath()
380 .build());
381 }
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400384
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400385 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400386 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700387
388 // Determine the dynamic grid properties
389 Point smallestSize = new Point();
390 Point largestSize = new Point();
391 Point realSize = new Point();
392 Display display = getWindowManager().getDefaultDisplay();
393 display.getCurrentSizeRange(smallestSize, largestSize);
394 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700395 DisplayMetrics dm = new DisplayMetrics();
396 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700397
Winson Chung5f8afe62013-08-12 16:19:28 -0700398 // Lazy-initialize the dynamic grid
399 DeviceProfile grid = app.initDynamicGrid(this,
400 Math.min(smallestSize.x, smallestSize.y),
401 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700402 realSize.x, realSize.y,
403 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700404
405 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400406 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700407 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800408 mModel = app.setLauncher(this);
409 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700410 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400411 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700413
Daniel Sandlerff02d492013-08-05 02:12:05 -0400414 mStats = new Stats(this);
415
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700416 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700417
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700418 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
419 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700420
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700421 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
422 // this also ensures that any synchronous binding below doesn't re-trigger another
423 // LauncherModel load.
424 mPaused = false;
425
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200427 android.os.Debug.startMethodTracing(
428 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 }
430
Adam Cohen53805212013-10-01 10:39:23 -0700431
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700434
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100436 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800438 registerContentObservers();
439
Joe Onorato7c312c12009-08-13 21:36:53 -0700440 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 mSavedState = savedInstanceState;
443 restoreState(mSavedState);
444
445 if (PROFILE_STARTUP) {
446 android.os.Debug.stopMethodTracing();
447 }
448
449 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700450 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700451 // 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;
Michael Jurka43467462013-11-14 12:03:58 +0100582 new AsyncTask<Void, Void, Void>() {
583 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100585 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586 }
Michael Jurka43467462013-11-14 12:03:58 +0100587 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
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;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800710 int pendingAddWidgetId = mPendingAddWidgetId;
711 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700712
Adam Cohenad4e15c2013-10-17 16:21:35 -0700713 Runnable exitSpringLoaded = new Runnable() {
714 @Override
715 public void run() {
716 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
717 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
718 }
719 };
720
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700722 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
724 if (resultCode == RESULT_CANCELED) {
725 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700726 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
727 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700729 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
730 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 }
732 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200733 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
734 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
735 mWorkspace.exitOverviewMode(false);
736 }
737 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200739
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
741 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700742
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 // We have special handling for widgets
744 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800745 final int appWidgetId;
746 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
747 : -1;
748 if (widgetId < 0) {
749 appWidgetId = pendingAddWidgetId;
750 } else {
751 appWidgetId = widgetId;
752 }
753
Adam Cohenad4e15c2013-10-17 16:21:35 -0700754 final int result;
755 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800756 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700757 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
758 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 result = RESULT_CANCELED;
760 completeTwoStageWidgetDrop(result, appWidgetId);
761 onComplete = new Runnable() {
762 @Override
763 public void run() {
764 exitSpringLoadedDragModeDelayed(false, 0, null);
765 }
766 };
Winson Chung5aaab772012-04-27 15:24:02 -0700767 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700768 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700769 final CellLayout dropLayout =
770 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
771 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700772 onComplete = new Runnable() {
773 @Override
774 public void run() {
775 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700776 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700777 }
778 };
Winson Chung5aaab772012-04-27 15:24:02 -0700779 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
781 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800782 return;
783 }
784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 // The pattern used here is that a user PICKs a specific application,
786 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700787
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
789 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700790 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800791 final PendingAddArguments args = new PendingAddArguments();
792 args.requestCode = requestCode;
793 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700794 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700796 args.cellX = mPendingAddInfo.cellX;
797 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800798 if (isWorkspaceLocked()) {
799 sPendingAddList.add(args);
800 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700801 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700803 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
804 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700805 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800809 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800810 }
811
812 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700813 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700814 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800815 Runnable onCompleteRunnable = null;
816 int animationType = 0;
817
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700818 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800819 if (resultCode == RESULT_OK) {
820 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
821 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700822 mPendingAddWidgetInfo);
823 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800824 onCompleteRunnable = new Runnable() {
825 @Override
826 public void run() {
827 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700828 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700829 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
830 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800831 }
832 };
833 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800834 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800835 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800836 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700837 if (mDragLayer.getAnimatedView() != null) {
838 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
839 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
840 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700841 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700842 // The animated view may be null in the case of a rotation during widget configuration
843 onCompleteRunnable.run();
844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 }
846
847 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700848 protected void onStop() {
849 super.onStop();
850 FirstFrameAnimatorHelper.setIsVisible(false);
851 }
852
853 @Override
854 protected void onStart() {
855 super.onStart();
856 FirstFrameAnimatorHelper.setIsVisible(true);
857 }
858
859 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200861 long startTime = 0;
862 if (DEBUG_RESUME_TIME) {
863 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400864 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700867
Winson Chung4a2afa32012-07-19 14:53:05 -0700868 // Restore the previous launcher state
869 if (mOnResumeState == State.WORKSPACE) {
870 showWorkspace(false);
871 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800872 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700873 }
874 mOnResumeState = State.NONE;
875
Craig Mautner360310b2012-10-26 15:13:08 -0700876 // Background was set to gradient in onPause(), restore to black if in all apps.
877 setWorkspaceBackground(mState == State.WORKSPACE);
878
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800879 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700880 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700881 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700885 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700887 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200888 // We might have postponed some bind calls until onResume (see waitUntilResume) --
889 // execute them here
890 long startTimeCallbacks = 0;
891 if (DEBUG_RESUME_TIME) {
892 startTimeCallbacks = System.currentTimeMillis();
893 }
894
895 if (mAppsCustomizeContent != null) {
896 mAppsCustomizeContent.setBulkBind(true);
897 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700898 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
899 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200900 }
901 if (mAppsCustomizeContent != null) {
902 mAppsCustomizeContent.setBulkBind(false);
903 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700904 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200905 if (DEBUG_RESUME_TIME) {
906 Log.d(TAG, "Time spent processing callbacks in onResume: " +
907 (System.currentTimeMillis() - startTimeCallbacks));
908 }
Michael Jurka447bf842013-05-15 14:52:15 +0200909 }
Michael Jurka54554252013-08-01 12:52:23 +0200910 if (mOnResumeCallbacks.size() > 0) {
911 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
912 mOnResumeCallbacks.get(i).run();
913 }
914 mOnResumeCallbacks.clear();
915 }
Winson Chunge4e50662012-01-23 14:45:13 -0800916
917 // Reset the pressed state of icons that were locked in the press state while activities
918 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800919 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800920 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800921 mWaitingForResume.setStayPressed(false);
922 }
Winson Chunge4e50662012-01-23 14:45:13 -0800923 if (mAppsCustomizeContent != null) {
924 // Resets the previous all apps icon press state
925 mAppsCustomizeContent.resetDrawableState();
926 }
Winson Chung82963d52013-10-09 11:20:57 -0700927
Adam Cohen06dff352012-06-01 17:17:08 -0700928 // It is possible that widgets can receive updates while launcher is not in the foreground.
929 // Consequently, the widgets will be inflated in the orientation of the foreground activity
930 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
931 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700932 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700933
Winson Chung780fe592013-09-26 14:48:44 -0700934 // Process any items that were added while Launcher was away.
935 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
936
Winson Chung5841efa2013-09-30 18:06:44 -0700937 // Update the voice search button proxy
938 updateVoiceButtonProxyVisible(false);
939
Adam Cohenf9426d52012-06-04 17:26:21 -0700940 // Again, as with the above scenario, it's possible that one or more of the global icons
941 // were updated in the wrong orientation.
942 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200943 if (DEBUG_RESUME_TIME) {
944 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
945 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700946
947 if (mWorkspace.getCustomContentCallbacks() != null) {
948 // If we are resuming and the custom content is the current page, we call onShow().
949 // It is also poassible that onShow will instead be called slightly after first layout
950 // if PagedView#setRestorePage was set to the custom content page in onCreate().
951 if (mWorkspace.isOnOrMovingToCustomContent()) {
952 mWorkspace.getCustomContentCallbacks().onShow();
953 }
954 }
Adam Cohenedaaa302013-10-01 17:33:27 -0700955 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -0700956 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -0700957 }
958
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700960 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700961 // Ensure that items added to Launcher are queued until Launcher returns
962 InstallShortcutReceiver.enableInstallQueue();
963
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700964 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700965 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800966 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700967 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700968
969 // We call onHide() aggressively. The custom content callbacks should be able to
970 // debounce excess onHide calls.
971 if (mWorkspace.getCustomContentCallbacks() != null) {
972 mWorkspace.getCustomContentCallbacks().onHide();
973 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700974 }
Romain Guycbb89e42009-06-08 15:52:54 -0700975
Adam Cohen66a01fd2013-06-11 12:48:00 -0700976 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -0700977 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600978 addCustomContentToLeft();
979 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700980 }
981
Adam Cohenbffe7452013-07-22 18:21:45 -0700982 QSBScroller mQsbScroller = new QSBScroller() {
983 int scrollY = 0;
984
985 @Override
986 public void setScrollY(int scroll) {
987 scrollY = scroll;
988
989 if (mWorkspace.isOnOrMovingToCustomContent()) {
990 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +0100991 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -0700992 }
993 }
994 };
995
996 public void resetQSBScroll() {
997 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +0100998 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -0700999 }
1000
1001 public interface CustomContentCallbacks {
1002 // Custom content is completely shown
1003 public void onShow();
1004
1005 // Custom content is completely hidden
1006 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001007
1008 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1009 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001010 }
1011
Adam Cohen30bacb22013-08-29 14:25:25 -07001012 protected void startSettings() {
1013 }
1014
Adam Cohenbffe7452013-07-22 18:21:45 -07001015 public interface QSBScroller {
1016 public void setScrollY(int scrollY);
1017 }
1018
Winson Chung98ca0f72013-07-29 12:58:51 -07001019 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001020 CustomContentCallbacks callbacks, String description) {
1021 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001022 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001023 }
1024
Adam Cohenedb40762013-07-18 16:45:45 -07001025 // The custom content needs to offset its content to account for the QSB
1026 public int getTopOffsetForCustomContent() {
1027 return mWorkspace.getPaddingTop();
1028 }
1029
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001030 @Override
1031 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 // Flag the loader to stop early before switching
1033 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001034 if (mAppsCustomizeContent != null) {
1035 mAppsCustomizeContent.surrender();
1036 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001037 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001038 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001039
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001040 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001041 @Override
1042 public void onWindowFocusChanged(boolean hasFocus) {
1043 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001044 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001045 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 private boolean acceptFilter() {
1048 final InputMethodManager inputManager = (InputMethodManager)
1049 getSystemService(Context.INPUT_METHOD_SERVICE);
1050 return !inputManager.isFullscreenMode();
1051 }
1052
1053 @Override
1054 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001055 final int uniChar = event.getUnicodeChar();
1056 final boolean handled = super.onKeyDown(keyCode, event);
1057 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1058 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1060 keyCode, event);
1061 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001062 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001063 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001064 // showSearchDialog()
1065 // If there are multiple keystrokes before the search dialog takes focus,
1066 // onSearchRequested() will be called for every keystroke,
1067 // but it is idempotent, so it's fine.
1068 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 }
1070 }
1071
Joe Onorato8a9625e2010-01-28 15:55:35 -08001072 // Eat the long press event so the keyboard doesn't come up.
1073 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1074 return true;
1075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 return handled;
1078 }
1079
Karl Rosaen138a0412009-04-23 19:00:21 -07001080 private String getTypedText() {
1081 return mDefaultKeySsb.toString();
1082 }
1083
1084 private void clearTypedText() {
1085 mDefaultKeySsb.clear();
1086 mDefaultKeySsb.clearSpans();
1087 Selection.setSelection(mDefaultKeySsb, 0);
1088 }
1089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001091 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1092 * State
1093 */
1094 private static State intToState(int stateOrdinal) {
1095 State state = State.WORKSPACE;
1096 final State[] stateValues = State.values();
1097 for (int i = 0; i < stateValues.length; i++) {
1098 if (stateValues[i].ordinal() == stateOrdinal) {
1099 state = stateValues[i];
1100 break;
1101 }
1102 }
1103 return state;
1104 }
1105
1106 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 * Restores the previous state, if it exists.
1108 *
1109 * @param savedState The previous state.
1110 */
1111 private void restoreState(Bundle savedState) {
1112 if (savedState == null) {
1113 return;
1114 }
1115
Michael Jurka883f55b2010-10-21 15:47:14 -07001116 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001117 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001118 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001119 }
1120
Adam Cohen21cd0022013-10-09 18:57:02 -07001121 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1122 PagedView.INVALID_RESTORE_PAGE);
1123 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001124 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126
Winson Chung3d503fb2011-07-13 17:25:49 -07001127 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001128 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001129
Winson Chung3d503fb2011-07-13 17:25:49 -07001130 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1131 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001132 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001133 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1134 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001135 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1136 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1137 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001138 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001139 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 mRestoring = true;
1141 }
1142
1143 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1144 if (renameFolder) {
1145 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001146 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 mRestoring = true;
1148 }
Winson Chunga12a2502010-12-20 14:41:35 -08001149
Winson Chung785d2eb2011-04-14 16:08:02 -07001150 // Restore the AppsCustomize tab
1151 if (mAppsCustomizeTabHost != null) {
1152 String curTab = savedState.getString("apps_customize_currentTab");
1153 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001154 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001155 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001156 mAppsCustomizeContent.loadAssociatedPages(
1157 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001158 }
1159
Winson Chung5afbf7b2011-07-25 11:53:08 -07001160 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1161 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 }
1164
1165 /**
1166 * Finds all the views we need and configure them properly.
1167 */
1168 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001169 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001170
Craig Mautner360310b2012-10-26 15:13:08 -07001171 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001172 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1173 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001174
John Spurlock77e1f472013-09-11 10:09:51 -04001175 mLauncherView.setSystemUiVisibility(
1176 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001177 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
Winson Chung4c98d922011-05-31 16:50:48 -07001179 // Setup the drag layer
1180 mDragLayer.setup(this, dragController);
1181
Winson Chung3d503fb2011-07-13 17:25:49 -07001182 // Setup the hotseat
1183 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1184 if (mHotseat != null) {
1185 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001186 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001187 }
1188
Adam Cohenf358a4b2013-07-23 16:47:31 -07001189 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001190 View widgetButton = findViewById(R.id.widget_button);
1191 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001192 @Override
1193 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001194 if (!mWorkspace.isSwitchingState()) {
1195 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1196 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001197 }
1198 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001199 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1200
1201 View wallpaperButton = findViewById(R.id.wallpaper_button);
1202 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001203 @Override
1204 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001205 if (!mWorkspace.isSwitchingState()) {
1206 startWallpaper();
1207 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001208 }
1209 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001210 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1211
1212 View settingsButton = findViewById(R.id.settings_button);
1213 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001214 @Override
1215 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001216 if (!mWorkspace.isSwitchingState()) {
1217 startSettings();
1218 }
Adam Cohen30bacb22013-08-29 14:25:25 -07001219 }
1220 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001221 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001222 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001223
Winson Chung4c98d922011-05-31 16:50:48 -07001224 // Setup the workspace
1225 mWorkspace.setHapticFeedbackEnabled(false);
1226 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001227 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001228 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001229
Winson Chungf0ea4d32011-06-06 14:27:16 -07001230 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001231 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001232
Winson Chungf0ea4d32011-06-06 14:27:16 -07001233 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001234 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001235 mAppsCustomizeContent = (AppsCustomizePagedView)
1236 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1237 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001238
Winson Chung3d503fb2011-07-13 17:25:49 -07001239 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001240 dragController.setDragScoller(mWorkspace);
1241 dragController.setScrollView(mDragLayer);
1242 dragController.setMoveTarget(mWorkspace);
1243 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001244 if (mSearchDropTargetBar != null) {
1245 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001246 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001247
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001248 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001249 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001250 mWeightWatcher = new WeightWatcher(this);
1251 mWeightWatcher.setAlpha(0.5f);
1252 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001253 new FrameLayout.LayoutParams(
1254 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001255 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001256 Gravity.BOTTOM)
1257 );
Adam Cohen39a06042013-07-19 14:30:12 -07001258
1259 boolean show = shouldShowWeightWatcher();
1260 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
1263
1264 /**
1265 * Creates a view representing a shortcut.
1266 *
1267 * @param info The data structure describing the shortcut.
1268 *
1269 * @return A View inflated from R.layout.application.
1270 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001271 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001273 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
1275
1276 /**
1277 * Creates a view representing a shortcut inflated from the specified resource.
1278 *
1279 * @param layoutResId The id of the XML layout used to create the shortcut.
1280 * @param parent The group the shortcut belongs to.
1281 * @param info The data structure describing the shortcut.
1282 *
1283 * @return A View inflated from layoutResId.
1284 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001285 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001286 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1287 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 return favorite;
1290 }
1291
1292 /**
1293 * Add an application shortcut to the workspace.
1294 *
1295 * @param data The intent describing the application.
1296 * @param cellInfo The position on screen where to create the shortcut.
1297 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001298 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001299 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001300 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001301
Adam Cohenfbba09b2011-07-18 21:43:05 -07001302 // First we check if we already know the exact location where we want to add this item.
1303 if (cellX >= 0 && cellY >= 0) {
1304 cellXY[0] = cellX;
1305 cellXY[1] = cellY;
1306 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001307 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001308 return;
1309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001311 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001312
Romain Guy73b979d2009-06-09 12:57:21 -07001313 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001314 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001316 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001317 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001318 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001319 } else {
1320 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322 }
Romain Guycbb89e42009-06-08 15:52:54 -07001323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 /**
1325 * Add a shortcut to the workspace.
1326 *
1327 * @param data The intent describing the shortcut.
1328 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001330 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001331 int cellY) {
1332 int[] cellXY = mTmpAddItemCellCoordinates;
1333 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001334 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001335
Michael Jurkad3ef3062010-11-23 16:23:58 -08001336 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001337
Adam Cohen558baaf2011-08-15 15:22:57 -07001338 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001339 if (info == null) {
1340 return;
1341 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001342 final View view = createShortcut(info);
1343
Adam Cohenfbba09b2011-07-18 21:43:05 -07001344 // First we check if we already know the exact location where we want to add this item.
1345 if (cellX >= 0 && cellY >= 0) {
1346 cellXY[0] = cellX;
1347 cellXY[1] = cellY;
1348 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001349
1350 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001351 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001352 true, null,null)) {
1353 return;
1354 }
1355 DragObject dragObject = new DragObject();
1356 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001357 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1358 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001359 return;
1360 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001361 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001362 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001364 foundCellSpan = (result != null);
1365 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001366 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001367 }
1368
1369 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001370 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001371 return;
1372 }
1373
Adam Cohendcd297f2013-06-18 13:13:40 -07001374 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375
1376 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001377 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001378 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
1380 }
1381
Adam Cohen2f093b62012-04-30 18:59:53 -07001382 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1383 int minHeight) {
1384 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001385 // We want to account for the extra amount of padding that we are adding to the widget
1386 // to ensure that it gets the full amount of space that it has requested
1387 int requiredWidth = minWidth + padding.left + padding.right;
1388 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001389 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001390 }
1391
Adam Cohen2f093b62012-04-30 18:59:53 -07001392 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1393 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001394 }
1395
Adam Cohen2f093b62012-04-30 18:59:53 -07001396 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1397 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001398 }
1399
Adam Cohen2f093b62012-04-30 18:59:53 -07001400 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1401 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001402 }
1403
Adam Cohen2f093b62012-04-30 18:59:53 -07001404 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1405 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001406 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001407 }
1408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001410 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001412 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001413 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001415 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001416 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1417 if (appWidgetInfo == null) {
1418 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1419 }
Romain Guycbb89e42009-06-08 15:52:54 -07001420
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001421 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001422 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001423
Adam Cohen2f093b62012-04-30 18:59:53 -07001424 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1425 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001428 // We have saved the position to which the widget was dragged-- this really only matters
1429 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001430 int[] cellXY = mTmpAddItemCellCoordinates;
1431 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001432 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001433 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001434 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1435 cellXY[0] = mPendingAddInfo.cellX;
1436 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001437 spanXY[0] = mPendingAddInfo.spanX;
1438 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001439 foundCellSpan = true;
1440 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001441 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001442 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001443 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1444 spanXY[1], cellXY, finalSpan);
1445 spanXY[0] = finalSpan[0];
1446 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001447 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001448 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001449 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001450 }
1451
Michael Jurka0280c3b2010-09-17 15:00:07 -07001452 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001453 if (appWidgetId != -1) {
1454 // Deleting an app widget ID is a void call but writes to disk before returning
1455 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001456 new AsyncTask<Void, Void, Void>() {
1457 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001458 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001459 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001460 }
Michael Jurka43467462013-11-14 12:03:58 +01001461 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001462 }
Winson Chung93eef082012-03-23 15:59:27 -07001463 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001464 return;
1465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001467 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001468 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1469 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001470 launcherInfo.spanX = spanXY[0];
1471 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001472 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1473 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001476 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477
1478 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001479 if (hostView == null) {
1480 // Perform actual inflation because we're live
1481 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1482 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1483 } else {
1484 // The AppWidgetHostView has already been inflated and instantiated
1485 launcherInfo.hostView = hostView;
1486 }
Romain Guycbb89e42009-06-08 15:52:54 -07001487
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001489 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001490 launcherInfo.notifyWidgetSizeChanged(this);
1491
Adam Cohendcd297f2013-06-18 13:13:40 -07001492 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001494
1495 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001497 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
Romain Guycbb89e42009-06-08 15:52:54 -07001499
Adam Cohended9f8d2010-11-03 13:25:16 -07001500 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1501 @Override
1502 public void onReceive(Context context, Intent intent) {
1503 final String action = intent.getAction();
1504 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1505 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001506 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001507 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001508
Winson Chungc100e8e2011-08-09 16:02:43 -07001509 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001510 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001511 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001512 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001513 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001514 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1515 mUserPresent = true;
1516 updateRunning();
1517 }
1518 }
1519 };
1520
1521 @Override
1522 public void onAttachedToWindow() {
1523 super.onAttachedToWindow();
1524
1525 // Listen for broadcasts related to user-presence
1526 final IntentFilter filter = new IntentFilter();
1527 filter.addAction(Intent.ACTION_SCREEN_OFF);
1528 filter.addAction(Intent.ACTION_USER_PRESENT);
1529 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001530 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001531 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001532 mVisible = true;
1533 }
1534
1535 @Override
1536 public void onDetachedFromWindow() {
1537 super.onDetachedFromWindow();
1538 mVisible = false;
1539
Adam Cohend113e0c2010-11-11 10:48:05 -08001540 if (mAttached) {
1541 unregisterReceiver(mReceiver);
1542 mAttached = false;
1543 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001544 updateRunning();
1545 }
1546
1547 public void onWindowVisibilityChanged(int visibility) {
1548 mVisible = visibility == View.VISIBLE;
1549 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001550 // The following code used to be in onResume, but it turns out onResume is called when
1551 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1552 // is a more appropriate event to handle
1553 if (mVisible) {
1554 mAppsCustomizeTabHost.onWindowVisible();
1555 if (!mWorkspaceLoading) {
1556 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001557 // We want to let Launcher draw itself at least once before we force it to build
1558 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001559 // apps is nice and speedy.
1560 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001561 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001562 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001563 if (mStarted) return;
1564 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001565 // We delay the layer building a bit in order to give
1566 // other message processing a time to run. In particular
1567 // this avoids a delay in hiding the IME if it was
1568 // currently shown, because doing that may involve
1569 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001570 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001571 final ViewTreeObserver.OnDrawListener listener = this;
1572 mWorkspace.post(new Runnable() {
1573 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001574 if (mWorkspace != null &&
1575 mWorkspace.getViewTreeObserver() != null) {
1576 mWorkspace.getViewTreeObserver().
1577 removeOnDrawListener(listener);
1578 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001579 }
1580 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001581 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001582 }
1583 });
1584 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001585 // When Launcher comes back to foreground, a different Activity might be responsible for
1586 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001587 if (!DISABLE_MARKET_BUTTON) {
1588 updateAppMarketIcon();
1589 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001590 clearTypedText();
1591 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001592 }
1593
1594 private void sendAdvanceMessage(long delay) {
1595 mHandler.removeMessages(ADVANCE_MSG);
1596 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1597 mHandler.sendMessageDelayed(msg, delay);
1598 mAutoAdvanceSentTime = System.currentTimeMillis();
1599 }
1600
1601 private void updateRunning() {
1602 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1603 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1604 mAutoAdvanceRunning = autoAdvanceRunning;
1605 if (autoAdvanceRunning) {
1606 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1607 sendAdvanceMessage(delay);
1608 } else {
1609 if (!mWidgetsToAdvance.isEmpty()) {
1610 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1611 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1612 }
1613 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001614 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001615 }
1616 }
1617 }
1618
1619 private final Handler mHandler = new Handler() {
1620 @Override
1621 public void handleMessage(Message msg) {
1622 if (msg.what == ADVANCE_MSG) {
1623 int i = 0;
1624 for (View key: mWidgetsToAdvance.keySet()) {
1625 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1626 final int delay = mAdvanceStagger * i;
1627 if (v instanceof Advanceable) {
1628 postDelayed(new Runnable() {
1629 public void run() {
1630 ((Advanceable) v).advance();
1631 }
1632 }, delay);
1633 }
1634 i++;
1635 }
1636 sendAdvanceMessage(mAdvanceInterval);
1637 }
1638 }
1639 };
1640
1641 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001642 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1644 if (v instanceof Advanceable) {
1645 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001646 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 updateRunning();
1648 }
1649 }
1650
1651 void removeWidgetToAutoAdvance(View hostView) {
1652 if (mWidgetsToAdvance.containsKey(hostView)) {
1653 mWidgetsToAdvance.remove(hostView);
1654 updateRunning();
1655 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001656 }
1657
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001659 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660 launcherInfo.hostView = null;
1661 }
1662
Winson Chung93eef082012-03-23 15:59:27 -07001663 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1664 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1665 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 }
1667
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001668 public LauncherAppWidgetHost getAppWidgetHost() {
1669 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 }
Romain Guycbb89e42009-06-08 15:52:54 -07001671
Winson Chunga9abd0e2010-10-27 17:18:37 -07001672 public LauncherModel getModel() {
1673 return mModel;
1674 }
1675
Bjorn Bringertc459e522013-06-07 19:36:01 +01001676 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001677 getWindow().closeAllPanels();
1678
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001679 // Whatever we were doing is hereby canceled.
1680 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001681 }
1682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 @Override
1684 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001685 long startTime = 0;
1686 if (DEBUG_RESUME_TIME) {
1687 startTime = System.currentTimeMillis();
1688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 super.onNewIntent(intent);
1690
1691 // Close the menu
1692 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001693 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001694 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001695
Adam Cohened307df2013-10-02 09:37:31 -07001696 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1697 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1698 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001699
Adam Cohen6fecd412013-10-02 17:41:50 -07001700 if (mWorkspace == null) {
1701 // Can be cases where mWorkspace is null, this prevents a NPE
1702 return;
1703 }
1704 Folder openFolder = mWorkspace.getOpenFolder();
1705 // In all these cases, only animate if we're already on home
1706 mWorkspace.exitWidgetResizeMode();
1707 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1708 openFolder == null) {
1709 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001710 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001711
Adam Cohen6fecd412013-10-02 17:41:50 -07001712 closeFolder();
1713 exitSpringLoadedDragMode();
1714
1715 // If we are already on home, then just animate back to the workspace,
1716 // otherwise, just wait until onResume to set the state back to Workspace
1717 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001718 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001719 } else {
1720 mOnResumeState = State.WORKSPACE;
1721 }
1722
1723 final View v = getWindow().peekDecorView();
1724 if (v != null && v.getWindowToken() != null) {
1725 InputMethodManager imm = (InputMethodManager)getSystemService(
1726 INPUT_METHOD_SERVICE);
1727 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1728 }
1729
1730 // Reset the apps customize page
1731 if (mAppsCustomizeTabHost != null) {
1732 mAppsCustomizeTabHost.reset();
1733 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 }
Adam Cohened307df2013-10-02 09:37:31 -07001735
Michael Jurka447bf842013-05-15 14:52:15 +02001736 if (DEBUG_RESUME_TIME) {
1737 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 }
1740
1741 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001742 public void onRestoreInstanceState(Bundle state) {
1743 super.onRestoreInstanceState(state);
1744 for (int page: mSynchronouslyBoundPages) {
1745 mWorkspace.restoreInstanceStateForChild(page);
1746 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 }
1748
1749 @Override
1750 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001751 if (mWorkspace.getChildCount() > 0) {
1752 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getRestorePage());
1753 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001754 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755
Michael Jurka883f55b2010-10-21 15:47:14 -07001756 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001757 // We close any open folder since it will not be re-opened, and we need to make sure
1758 // this state is reflected.
1759 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760
Adam Cohendcd297f2013-06-18 13:13:40 -07001761 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001762 mWaitingForResult) {
1763 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001764 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001765 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1766 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001767 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1768 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1769 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001770 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 }
1772
1773 if (mFolderInfo != null && mWaitingForResult) {
1774 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1775 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1776 }
Michael Jurka946ad472010-07-09 18:05:18 -07001777
Winson Chung785d2eb2011-04-14 16:08:02 -07001778 // Save the current AppsCustomize tab
1779 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001780 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1781 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001782 if (currentTabTag != null) {
1783 outState.putString("apps_customize_currentTab", currentTabTag);
1784 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001785 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1786 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001787 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 }
1789
1790 @Override
1791 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001793
Winson Chunge7a03942011-08-05 15:05:12 -07001794 // Remove all pending runnables
1795 mHandler.removeMessages(ADVANCE_MSG);
1796 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001797 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001798
Winson Chungcd2b0142011-06-08 16:02:26 -07001799 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001800 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001801 mModel.stopLoader();
1802 app.setLauncher(null);
1803
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001805 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001807 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001809 mAppWidgetHost = null;
1810
1811 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812
1813 TextKeyListener.getInstance().release();
1814
Winson Chung81b52252012-08-27 15:34:29 -07001815 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1816 // to prevent leaking Launcher activities on orientation change.
1817 if (mModel != null) {
1818 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1819 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001820
1821 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001822 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001823
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001824 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001825 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1826 mWorkspace.removeAllViews();
1827 mWorkspace = null;
1828 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001829
Michael Jurka2ecf9952012-06-18 12:52:28 -07001830 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 }
1832
Adam Cohena9cf38f2011-05-02 15:36:58 -07001833 public DragController getDragController() {
1834 return mDragController;
1835 }
1836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 @Override
1838 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001839 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 super.startActivityForResult(intent, requestCode);
1841 }
1842
Winson Chung70d72102011-08-12 11:24:35 -07001843 /**
1844 * Indicates that we want global search for this activity by setting the globalSearch
1845 * argument for {@link #startSearch} to true.
1846 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001848 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001850
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001851 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001852
Karl Rosaen138a0412009-04-23 19:00:21 -07001853 if (initialQuery == null) {
1854 // Use any text typed in the launcher as the initial query
1855 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001856 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 if (appSearchData == null) {
1858 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001859 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 }
Winson Chungadf0c182012-08-23 14:59:07 -07001861 Rect sourceBounds = new Rect();
1862 if (mSearchDropTargetBar != null) {
1863 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1864 }
Romain Guycbb89e42009-06-08 15:52:54 -07001865
Bjorn Bringertc459e522013-06-07 19:36:01 +01001866 startSearch(initialQuery, selectInitialQuery,
1867 appSearchData, sourceBounds);
1868 }
1869
1870 public void startSearch(String initialQuery,
1871 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001872 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001873 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001874 }
1875
1876 /**
1877 * Starts the global search activity. This code is a copied from SearchManager
1878 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001879 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001880 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001881 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001882 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1883 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1884 if (globalSearchActivity == null) {
1885 Log.w(TAG, "No global search activity found.");
1886 return;
1887 }
1888 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1889 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1890 intent.setComponent(globalSearchActivity);
1891 // Make sure that we have a Bundle to put source in
1892 if (appSearchData == null) {
1893 appSearchData = new Bundle();
1894 } else {
1895 appSearchData = new Bundle(appSearchData);
1896 }
1897 // Set source to package name of app that starts global search, if not set already.
1898 if (!appSearchData.containsKey("source")) {
1899 appSearchData.putString("source", getPackageName());
1900 }
1901 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1902 if (!TextUtils.isEmpty(initialQuery)) {
1903 intent.putExtra(SearchManager.QUERY, initialQuery);
1904 }
1905 if (selectInitialQuery) {
1906 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1907 }
1908 intent.setSourceBounds(sourceBounds);
1909 try {
1910 startActivity(intent);
1911 } catch (ActivityNotFoundException ex) {
1912 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 }
1915
Winson Chung70d72102011-08-12 11:24:35 -07001916 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001917 public boolean onPrepareOptionsMenu(Menu menu) {
1918 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001919 if (!mWorkspace.isInOverviewMode()) {
1920 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001921 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001922 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001925 @Override
1926 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001927 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001928 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001929 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001930 }
1931
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 public boolean isWorkspaceLocked() {
1933 return mWorkspaceLoading || mWaitingForResult;
1934 }
1935
Michael Jurka0280c3b2010-09-17 15:00:07 -07001936 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001937 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001938 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001939 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1940 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001941 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001942 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001943 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001944
Adam Cohenad4e15c2013-10-17 16:21:35 -07001945 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1946 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
1947 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1948 }
1949
1950 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1951 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
1952 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001953 if (appWidgetInfo.configure != null) {
1954 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08001955 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07001956
Bjorn Bringert7984c942009-12-09 15:38:25 +00001957 // Launch over to configure widget, if needed
1958 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001959 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001960 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001961 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001963 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001964 Runnable onComplete = new Runnable() {
1965 @Override
1966 public void run() {
1967 // Exit spring loaded mode if necessary after adding the widget
1968 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1969 null);
1970 }
1971 };
Adam Cohendcd297f2013-06-18 13:13:40 -07001972 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001973 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001974 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
1976 }
Romain Guycbb89e42009-06-08 15:52:54 -07001977
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001978 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001979 // Close any folders that may be open.
1980 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001981 mWorkspace.moveToCustomContentScreen(animate);
1982 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001983 /**
1984 * Process a shortcut drop.
1985 *
1986 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001988 * @param cell The cell it should be added to, optional
1989 * @param position The location on the screen where it was dropped, optional
1990 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001991 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001992 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001993 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001994 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001995 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001996 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001997
1998 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001999 mPendingAddInfo.cellX = cell[0];
2000 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002001 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002002
2003 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2004 createShortcutIntent.setComponent(componentName);
2005 processShortcut(createShortcutIntent);
2006 }
2007
Adam Cohenfbba09b2011-07-18 21:43:05 -07002008 /**
2009 * Process a widget drop.
2010 *
2011 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002012 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002013 * @param cell The cell it should be added to, optional
2014 * @param position The location on the screen where it was dropped, optional
2015 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002016 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002017 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002018 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002019 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002020 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002021 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002022 mPendingAddInfo.minSpanX = info.minSpanX;
2023 mPendingAddInfo.minSpanY = info.minSpanY;
2024
Adam Cohenfbba09b2011-07-18 21:43:05 -07002025 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002026 mPendingAddInfo.cellX = cell[0];
2027 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002028 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002029 if (span != null) {
2030 mPendingAddInfo.spanX = span[0];
2031 mPendingAddInfo.spanY = span[1];
2032 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002033
Adam Cohened66b2b2012-01-23 17:28:51 -08002034 AppWidgetHostView hostView = info.boundWidget;
2035 int appWidgetId;
2036 if (hostView != null) {
2037 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002038 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002039 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002040 // In this case, we either need to start an activity to get permission to bind
2041 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002042 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002043 Bundle options = info.bindOptions;
2044
2045 boolean success = false;
2046 if (options != null) {
2047 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2048 info.componentName, options);
2049 } else {
2050 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2051 info.componentName);
2052 }
2053 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002054 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002055 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002056 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002057 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2058 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2059 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002060 // TODO: we need to make sure that this accounts for the options bundle.
2061 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002062 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2063 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002064 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002065 }
2066
Joe Onoratodeb98af2010-02-19 14:59:39 -08002067 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002068 // Handle case where user selected "Applications"
2069 String applicationName = getResources().getString(R.string.group_applications);
2070 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002071
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002072 if (applicationName != null && applicationName.equals(shortcutName)) {
2073 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2074 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002075
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002076 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2077 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002078 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002079 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002080 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002081 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 }
2084
Winson Chung24ab2f12010-09-16 14:10:47 -07002085 void processWallpaper(Intent intent) {
2086 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2087 }
2088
Adam Cohendcd297f2013-06-18 13:13:40 -07002089 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002090 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002091 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 folderInfo.title = getText(R.string.folder_name);
2093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002095 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002096 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002097 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098
2099 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002100 FolderIcon newFolder =
2101 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002102 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002103 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002104 // Force measure the new folder icon
2105 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2106 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002107 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 }
Romain Guycbb89e42009-06-08 15:52:54 -07002109
Joe Onorato9c1289c2009-08-17 11:03:03 -04002110 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002111 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002112 }
2113
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002114 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002116 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002117 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 }
2119
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002120 protected ComponentName getWallpaperPickerComponent() {
2121 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2122 }
2123
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002124 /**
2125 * Registers various content observers. The current implementation registers
2126 * only a favorites observer to keep track of the favorites applications.
2127 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002128 private void registerContentObservers() {
2129 ContentResolver resolver = getContentResolver();
2130 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2131 true, mWidgetObserver);
2132 }
2133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 @Override
2135 public boolean dispatchKeyEvent(KeyEvent event) {
2136 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2137 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002139 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002140 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002141 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002142 dumpState();
2143 return true;
2144 }
2145 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002146 }
2147 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2148 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002149 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 return true;
2151 }
2152 }
2153
2154 return super.dispatchKeyEvent(event);
2155 }
2156
Joe Onorato88ec0992009-11-19 13:16:06 -08002157 @Override
2158 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002159 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002160 if (mAppsCustomizeContent.getContentType() ==
2161 AppsCustomizePagedView.ContentType.Applications) {
2162 showWorkspace(true);
2163 } else {
2164 showOverviewMode(true);
2165 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002166 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002167 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002168 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002169 Folder openFolder = mWorkspace.getOpenFolder();
2170 if (openFolder.isEditingName()) {
2171 openFolder.dismissEditingName();
2172 } else {
2173 closeFolder();
2174 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002175 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002176 mWorkspace.exitWidgetResizeMode();
2177
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002178 // Back button is a no-op here, but give at least some feedback for the button press
2179 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002180 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002181 }
2182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002184 * Re-listen when widgets are reset.
2185 */
2186 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002187 if (mAppWidgetHost != null) {
2188 mAppWidgetHost.startListening();
2189 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002190 }
2191
2192 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 * Launches the intent referred by the clicked shortcut.
2194 *
2195 * @param v The view representing the clicked shortcut.
2196 */
2197 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002198 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2199 // view has detached (it's possible for this to happen if the view is removed mid touch).
2200 if (v.getWindowToken() == null) {
2201 return;
2202 }
2203
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002204 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002205 return;
2206 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002207
Adam Cohen1697b792013-09-17 19:08:21 -07002208 if (v instanceof Workspace) {
2209 if (mWorkspace.isInOverviewMode()) {
2210 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002211 }
2212 return;
2213 }
2214
2215 if (v instanceof CellLayout) {
2216 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002217 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002218 }
2219 }
2220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002222 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002224 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2225 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002226
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002227 // Check for special shortcuts
2228 if (intent.getComponent() != null) {
2229 final String shortcutClass = intent.getComponent().getClassName();
2230
2231 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002232 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002233 return;
2234 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2235 MemoryDumpActivity.startDump(this);
2236 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002237 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2238 toggleShowWeightWatcher();
2239 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002240 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002241 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002242
2243 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002244 int[] pos = new int[2];
2245 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002246 intent.setSourceBounds(new Rect(pos[0], pos[1],
2247 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002248
2249 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002250
Daniel Sandlerff02d492013-08-05 02:12:05 -04002251 mStats.recordLaunch(intent, shortcut);
2252
Michael Jurkaddd62e92011-02-16 17:49:14 -08002253 if (success && v instanceof BubbleTextView) {
2254 mWaitingForResume = (BubbleTextView) v;
2255 mWaitingForResume.setStayPressed(true);
2256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002258 if (v instanceof FolderIcon) {
2259 FolderIcon fi = (FolderIcon) v;
2260 handleFolderClick(fi);
2261 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002262 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002263 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002264 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002265 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002266 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268 }
2269 }
2270
Michael Jurka0e260592010-06-30 17:07:39 -07002271 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002272 return false;
2273 }
2274
Michael Jurkaaf442092010-06-10 17:01:57 -07002275 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002276 * Event handler for the search button
2277 *
2278 * @param v The view that was clicked.
2279 */
2280 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002281 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2282
Amith Yamasania135ba82011-08-09 17:42:01 -07002283 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002284 }
2285
2286 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002287 * Event handler for the voice button
2288 *
2289 * @param v The view that was clicked.
2290 */
2291 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002292 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002293
Bjorn Bringertc459e522013-06-07 19:36:01 +01002294 startVoice();
2295 }
2296
2297 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002298 try {
2299 final SearchManager searchManager =
2300 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2301 ComponentName activityName = searchManager.getGlobalSearchActivity();
2302 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002303 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002304 if (activityName != null) {
2305 intent.setPackage(activityName.getPackageName());
2306 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002307 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002308 } catch (ActivityNotFoundException e) {
2309 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002310 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002311 startActivitySafely(null, intent, "onClickVoiceButton");
2312 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002313 }
2314
2315 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002316 * Event handler for the "grid" button that appears on the home screen, which
2317 * enters all apps mode.
2318 *
2319 * @param v The view that was clicked.
2320 */
2321 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002322 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002323 }
2324
2325 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002326 // Provide the same haptic feedback that the system offers for virtual keys.
2327 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002328 }
2329
Adam Cohen61f560d2013-09-30 15:58:20 -07002330 public void performHapticFeedbackOnTouchDown(View v) {
2331 // Provide the same haptic feedback that the system offers for virtual keys.
2332 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2333 }
2334
2335 public View.OnTouchListener getHapticFeedbackTouchListener() {
2336 if (mHapticFeedbackTouchListener == null) {
2337 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2338 @Override
2339 public boolean onTouch(View v, MotionEvent event) {
2340 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2341 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2342 }
2343 return false;
2344 }
2345 };
2346 }
2347 return mHapticFeedbackTouchListener;
2348 }
2349
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002350 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002351 if (!DISABLE_MARKET_BUTTON) {
2352 if (mAppMarketIntent != null) {
2353 startActivitySafely(v, mAppMarketIntent, "app market");
2354 } else {
2355 Log.e(TAG, "Invalid app market intent.");
2356 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002357 }
2358 }
2359
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002360 /**
2361 * Called when the user stops interacting with the launcher.
2362 * This implies that the user is now on the homescreen and is not doing housekeeping.
2363 */
2364 protected void onInteractionEnd() {}
2365
2366 /**
2367 * Called when the user starts interacting with the launcher.
2368 * The possible interactions are:
2369 * - open all apps
2370 * - reorder an app shortcut, or a widget
2371 * - open the overview mode.
2372 * This is a good time to stop doing things that only make sense
2373 * when the user is on the homescreen and not doing housekeeping.
2374 */
2375 protected void onInteractionBegin() {}
2376
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002377 void startApplicationDetailsActivity(ComponentName componentName) {
2378 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002379 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2380 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002381 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002382 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002383 }
2384
Michael Jurka1e2f4652013-07-08 18:03:46 -07002385 // returns true if the activity was started
2386 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002387 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002388 // System applications cannot be installed. For now, show a toast explaining that.
2389 // We may give them the option of disabling apps this way.
2390 int messageId = R.string.uninstall_system_app_text;
2391 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002392 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002393 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002394 String packageName = componentName.getPackageName();
2395 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002396 Intent intent = new Intent(
2397 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002398 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2399 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002400 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002401 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002402 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002403 }
2404
Michael Jurka86a720e2012-05-09 11:23:23 -07002405 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002409 // Only launch using the new animation if the shortcut has not opted out (this is a
2410 // private contract between launcher and may be ignored in the future).
2411 boolean useLaunchAnimation = (v != null) &&
2412 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2413 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002414 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2415 v.getMeasuredWidth(), v.getMeasuredHeight());
2416
2417 startActivity(intent, opts.toBundle());
2418 } else {
2419 startActivity(intent);
2420 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002421 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 } catch (SecurityException e) {
2423 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002424 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002426 "or use the exported attribute for this activity. "
2427 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002429 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002431
Michael Jurka86a720e2012-05-09 11:23:23 -07002432 boolean startActivitySafely(View v, Intent intent, Object tag) {
2433 boolean success = false;
2434 try {
2435 success = startActivity(v, intent, tag);
2436 } catch (ActivityNotFoundException e) {
2437 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2438 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2439 }
2440 return success;
2441 }
2442
Adam Cohena9cf38f2011-05-02 15:36:58 -07002443 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002444 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002445 Folder openFolder = mWorkspace.getFolderForTag(info);
2446
2447 // If the folder info reports that the associated folder is open, then verify that
2448 // it is actually opened. There have been a few instances where this gets out of sync.
2449 if (info.opened && openFolder == null) {
2450 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002451 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002452 info.opened = false;
2453 }
2454
Adam Cohenfb91f302012-06-11 15:45:18 -07002455 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 // Close any open folder
2457 closeFolder();
2458 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002459 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 } else {
2461 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002462 int folderScreen;
2463 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002464 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 // .. and close it
2466 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002467 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 // Close any folder open on the current screen
2469 closeFolder();
2470 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002471 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002472 }
2473 }
2474 }
2475 }
2476
Adam Cohen268c4752012-06-06 17:47:33 -07002477 /**
2478 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2479 * in the DragLayer in the exact absolute location of the original FolderIcon.
2480 */
2481 private void copyFolderIconToImage(FolderIcon fi) {
2482 final int width = fi.getMeasuredWidth();
2483 final int height = fi.getMeasuredHeight();
2484
2485 // Lazy load ImageView, Bitmap and Canvas
2486 if (mFolderIconImageView == null) {
2487 mFolderIconImageView = new ImageView(this);
2488 }
2489 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2490 mFolderIconBitmap.getHeight() != height) {
2491 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2492 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2493 }
2494
2495 DragLayer.LayoutParams lp;
2496 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2497 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2498 } else {
2499 lp = new DragLayer.LayoutParams(width, height);
2500 }
2501
Adam Cohen307fe232012-08-16 17:55:58 -07002502 // The layout from which the folder is being opened may be scaled, adjust the starting
2503 // view size by this scale factor.
2504 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002505 lp.customPosition = true;
2506 lp.x = mRectForFolderAnimation.left;
2507 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002508 lp.width = (int) (scale * width);
2509 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002510
2511 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2512 fi.draw(mFolderIconCanvas);
2513 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002514 if (fi.getFolder() != null) {
2515 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2516 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002517 }
Adam Cohen268c4752012-06-06 17:47:33 -07002518 // Just in case this image view is still in the drag layer from a previous animation,
2519 // we remove it and re-add it.
2520 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2521 mDragLayer.removeView(mFolderIconImageView);
2522 }
2523 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002524 if (fi.getFolder() != null) {
2525 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002526 }
Adam Cohen268c4752012-06-06 17:47:33 -07002527 }
2528
Adam Cohen2801caf2011-05-13 20:57:39 -07002529 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002530 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002531 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2532 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2533 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2534
Adam Cohenc51934b2011-07-26 21:07:43 -07002535 FolderInfo info = (FolderInfo) fi.getTag();
2536 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2537 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002538 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2539 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002540 }
2541
Adam Cohen268c4752012-06-06 17:47:33 -07002542 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2543 copyFolderIconToImage(fi);
2544 fi.setVisibility(View.INVISIBLE);
2545
Michael Jurka2ecf9952012-06-18 12:52:28 -07002546 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002547 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002548 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2549 oa.start();
2550 }
2551
Adam Cohen268c4752012-06-06 17:47:33 -07002552 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002553 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002554 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2555 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2556 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2557
Adam Cohen268c4752012-06-06 17:47:33 -07002558 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002559
Adam Cohen268c4752012-06-06 17:47:33 -07002560 // We remove and re-draw the FolderIcon in-case it has changed
2561 mDragLayer.removeView(mFolderIconImageView);
2562 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002563 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002564 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002565 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002566 oa.addListener(new AnimatorListenerAdapter() {
2567 @Override
2568 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002569 if (cl != null) {
2570 cl.clearFolderLeaveBehind();
2571 // Remove the ImageView copy of the FolderIcon and make the original visible.
2572 mDragLayer.removeView(mFolderIconImageView);
2573 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002574 }
2575 }
2576 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002577 oa.start();
2578 }
2579
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002581 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582 * is animated relative to the specified View. If the View is null, no animation
2583 * is played.
2584 *
2585 * @param folderInfo The FolderInfo describing the folder to open.
2586 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002587 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002588 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002589 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590
Adam Cohena9cf38f2011-05-02 15:36:58 -07002591 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592
Adam Cohen4554ee12011-08-03 16:13:21 -07002593 // Just verify that the folder hasn't already been added to the DragLayer.
2594 // There was a one-off crash where the folder had a parent already.
2595 if (folder.getParent() == null) {
2596 mDragLayer.addView(folder);
2597 mDragController.addDropTarget((DropTarget) folder);
2598 } else {
2599 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2600 folder.getParent() + ").");
2601 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002602 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002603 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002604
2605 // Notify the accessibility manager that this folder "window" has appeared and occluded
2606 // the workspace items
2607 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2608 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002609 }
2610
2611 public void closeFolder() {
2612 Folder folder = mWorkspace.getOpenFolder();
2613 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002614 if (folder.isEditingName()) {
2615 folder.dismissEditingName();
2616 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002617 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002618
2619 // Dismiss the folder cling
2620 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002621 }
2622 }
2623
2624 void closeFolder(Folder folder) {
2625 folder.getInfo().opened = false;
2626
2627 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2628 if (parent != null) {
2629 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2630 shrinkAndFadeInFolderIcon(fi);
2631 }
2632 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002633
2634 // Notify the accessibility manager that this folder "window" has disappeard and no
2635 // longer occludeds the workspace items
2636 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 }
2638
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002639 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002640 if (!isDraggingEnabled()) return false;
2641 if (isWorkspaceLocked()) return false;
2642 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002643
Adam Cohen1697b792013-09-17 19:08:21 -07002644 if (v instanceof Workspace) {
2645 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002646 if (mWorkspace.enterOverviewMode()) {
2647 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2648 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2649 return true;
2650 } else {
2651 return false;
2652 }
Adam Cohen1697b792013-09-17 19:08:21 -07002653 }
Adam Cohen1697b792013-09-17 19:08:21 -07002654 }
2655
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002657 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 }
2659
Michael Jurka0280c3b2010-09-17 15:00:07 -07002660 resetAddInfo();
2661 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002662 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002663 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002664 return true;
2665 }
2666
Winson Chung3d503fb2011-07-13 17:25:49 -07002667 // The hotseat touch handling does not go through Workspace, and we always allow long press
2668 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002669 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002670 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2671 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002672 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002673 // User long pressed on empty space
2674 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2675 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2676 // Disabling reordering until we sort out some issues.
2677 if (mWorkspace.isInOverviewMode()) {
2678 mWorkspace.startReordering(v);
2679 } else {
2680 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002682 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002683 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002684 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002685 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002686 }
2687 }
2688 }
2689 return true;
2690 }
2691
Winson Chung3d503fb2011-07-13 17:25:49 -07002692 boolean isHotseatLayout(View layout) {
2693 return mHotseat != null && layout != null &&
2694 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2695 }
2696 Hotseat getHotseat() {
2697 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002698 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002699 View getOverviewPanel() {
2700 return mOverviewPanel;
2701 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002702 SearchDropTargetBar getSearchBar() {
2703 return mSearchDropTargetBar;
2704 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002705
Winson Chung3d503fb2011-07-13 17:25:49 -07002706 /**
2707 * Returns the CellLayout of the specified container at the specified screen.
2708 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002709 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002710 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2711 if (mHotseat != null) {
2712 return mHotseat.getLayout();
2713 } else {
2714 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002715 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002716 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002717 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002718 }
2719 }
2720
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002721 Workspace getWorkspace() {
2722 return mWorkspace;
2723 }
2724
Daniel Sandler843e8602010-06-07 14:59:01 -04002725 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002726 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002727 }
2728
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002729 /**
2730 * Helper method for the cameraZoomIn/cameraZoomOut animations
2731 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002732 * @param scaleFactor The scale factor used for the zoom
2733 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002734 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002735 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002736 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002737 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002738
Craig Mautner360310b2012-10-26 15:13:08 -07002739 private void setWorkspaceBackground(boolean workspace) {
2740 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002741 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002742 }
2743
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002744 void updateWallpaperVisibility(boolean visible) {
2745 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2746 int curflags = getWindow().getAttributes().flags
2747 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2748 if (wpflags != curflags) {
2749 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2750 }
Craig Mautner360310b2012-10-26 15:13:08 -07002751 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002752 }
2753
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002754 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2755 if (v instanceof LauncherTransitionable) {
2756 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2757 }
2758 }
2759
Michael Jurkabed61d22012-02-14 22:51:29 -08002760 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2761 if (v instanceof LauncherTransitionable) {
2762 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2763 }
Winson Chung70442722012-02-10 15:43:22 -08002764
2765 // Update the workspace transition step as well
2766 dispatchOnLauncherTransitionStep(v, 0f);
2767 }
2768
2769 private void dispatchOnLauncherTransitionStep(View v, float t) {
2770 if (v instanceof LauncherTransitionable) {
2771 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2772 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002773 }
2774
2775 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2776 if (v instanceof LauncherTransitionable) {
2777 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2778 }
Winson Chung70442722012-02-10 15:43:22 -08002779
2780 // Update the workspace transition step as well
2781 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002782 }
2783
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002784 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002785 * Things to test when changing the following seven functions.
2786 * - Home from workspace
2787 * - from center screen
2788 * - from other screens
2789 * - Home from all apps
2790 * - from center screen
2791 * - from other screens
2792 * - Back from all apps
2793 * - from center screen
2794 * - from other screens
2795 * - Launch app from workspace and quit
2796 * - with back
2797 * - with home
2798 * - Launch app from all apps and quit
2799 * - with back
2800 * - with home
2801 * - Go to a screen that's not the default, then all
2802 * apps, and launch and app, and go back
2803 * - with back
2804 * -with home
2805 * - On workspace, long press power and go back
2806 * - with back
2807 * - with home
2808 * - On all apps, long press power and go back
2809 * - with back
2810 * - with home
2811 * - On workspace, power off
2812 * - On all apps, power off
2813 * - Launch an app and turn off the screen while in that app
2814 * - Go back with home key
2815 * - Go back with back key TODO: make this not go to workspace
2816 * - From all apps
2817 * - From workspace
2818 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2819 * - From all apps
2820 * - From the center workspace
2821 * - From another workspace
2822 */
2823
2824 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002825 * Zoom the camera out from the workspace to reveal 'toView'.
2826 * Assumes that the view to show is anchored at either the very top or very bottom
2827 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002828 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002829 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002830 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2831 showAppsCustomizeHelper(animated, springLoaded, contentType);
2832 }
2833 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2834 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002835 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002836 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002837 mStateAnimation.cancel();
2838 mStateAnimation = null;
2839 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002840 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002841
Winson Chungf0ea4d32011-06-06 14:27:16 -07002842 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2843 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2844 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002845 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002846 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002847 final int startDelay =
2848 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002849
Michael Jurkab3e22d92011-10-31 15:58:33 -07002850 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002851
Michael Jurkad74c9842011-07-10 12:44:21 -07002852 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002853 Animator workspaceAnim =
2854 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002855 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2856 // Set the content type for the all apps space
2857 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2858 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002859
2860 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002861 toView.setScaleX(scale);
2862 toView.setScaleY(scale);
2863 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2864 scaleAnim.
2865 scaleX(1f).scaleY(1f).
2866 setDuration(duration).
2867 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002868
Winson Chungf0ea4d32011-06-06 14:27:16 -07002869 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002870 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002871 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002872 .ofFloat(toView, "alpha", 0f, 1f)
2873 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002874 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002875 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2876 @Override
2877 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002878 if (animation == null) {
2879 throw new RuntimeException("animation is null");
2880 }
Winson Chung70442722012-02-10 15:43:22 -08002881 float t = (Float) animation.getAnimatedValue();
2882 dispatchOnLauncherTransitionStep(fromView, t);
2883 dispatchOnLauncherTransitionStep(toView, t);
2884 }
2885 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002886
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002887 // toView should appear right at the end of the workspace shrink
2888 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002889 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002890 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002891 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002892
2893 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002894 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002895 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002896 // Prepare the position
2897 toView.setTranslationX(0.0f);
2898 toView.setTranslationY(0.0f);
2899 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002900 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002901 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002902 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002903 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002904 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2905 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002906
Winson Chungc7d2b602012-05-16 17:02:20 -07002907 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002908 if (mSearchDropTargetBar != null) {
2909 mSearchDropTargetBar.hideSearchBar(false);
2910 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002911 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002912 });
2913
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002914 if (workspaceAnim != null) {
2915 mStateAnimation.play(workspaceAnim);
2916 }
Michael Jurka1899a362011-11-03 13:50:45 -07002917
2918 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002919
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002920 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2921 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002922
2923 // If any of the objects being animated haven't been measured/laid out
2924 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002925 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002926 (mWorkspace.getMeasuredWidth() == 0) ||
2927 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002928 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002929 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002930
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002931 final AnimatorSet stateAnimation = mStateAnimation;
2932 final Runnable startAnimRunnable = new Runnable() {
2933 public void run() {
2934 // Check that mStateAnimation hasn't changed while
2935 // we waited for a layout/draw pass
2936 if (mStateAnimation != stateAnimation)
2937 return;
2938 setPivotsForZoom(toView, scale);
2939 dispatchOnLauncherTransitionStart(fromView, animated, false);
2940 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002941 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002942 }
2943 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002944 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002945 final ViewTreeObserver observer = toView.getViewTreeObserver();
2946 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2947 public void onGlobalLayout() {
2948 startAnimRunnable.run();
2949 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2950 }
2951 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002952 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002953 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002954 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002955 } else {
2956 toView.setTranslationX(0.0f);
2957 toView.setTranslationY(0.0f);
2958 toView.setScaleX(1.0f);
2959 toView.setScaleY(1.0f);
2960 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002961 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002962
Daniel Sandlere4f98912013-06-25 15:13:26 -04002963 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002964 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002965 if (mSearchDropTargetBar != null) {
2966 mSearchDropTargetBar.hideSearchBar(false);
2967 }
Michael Jurkaabded662011-03-04 12:06:57 -08002968 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002969 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002970 dispatchOnLauncherTransitionStart(fromView, animated, false);
2971 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002972 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002973 dispatchOnLauncherTransitionStart(toView, animated, false);
2974 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002975 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002976 }
2977
2978 /**
2979 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002980 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002981 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002982 */
Adam Cohened307df2013-10-02 09:37:31 -07002983 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08002984 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002985
Michael Jurkab3e22d92011-10-31 15:58:33 -07002986 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002987 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002988 mStateAnimation.cancel();
2989 mStateAnimation = null;
2990 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002991 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002992
Winson Chungf0ea4d32011-06-06 14:27:16 -07002993 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002994 final int fadeOutDuration =
2995 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002996 final float scaleFactor = (float)
2997 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2998 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002999 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003000 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003001 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003002 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3003 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003004 toState, animated, stagger, -1);
3005 } else if (toState == Workspace.State.SPRING_LOADED ||
3006 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003007 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003008 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003009 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003010
Michael Jurkab3e22d92011-10-31 15:58:33 -07003011 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003012 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003013 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003014 final LauncherViewPropertyAnimator scaleAnim =
3015 new LauncherViewPropertyAnimator(fromView);
3016 scaleAnim.
3017 scaleX(scaleFactor).scaleY(scaleFactor).
3018 setDuration(duration).
3019 setInterpolator(new Workspace.ZoomInInterpolator());
3020
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003021 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003022 .ofFloat(fromView, "alpha", 1f, 0f)
3023 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003024 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003025 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3026 @Override
3027 public void onAnimationUpdate(ValueAnimator animation) {
3028 float t = 1f - (Float) animation.getAnimatedValue();
3029 dispatchOnLauncherTransitionStep(fromView, t);
3030 dispatchOnLauncherTransitionStep(toView, t);
3031 }
3032 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003033
Michael Jurka2ecf9952012-06-18 12:52:28 -07003034 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003035
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003036 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3037 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003038 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003039
3040 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003041 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003042 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003043 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003044 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3045 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003046 if (onCompleteRunnable != null) {
3047 onCompleteRunnable.run();
3048 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003049 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003050 }
3051 });
3052
Winson Chungf0ea4d32011-06-06 14:27:16 -07003053 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003054 if (workspaceAnim != null) {
3055 mStateAnimation.play(workspaceAnim);
3056 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003057 dispatchOnLauncherTransitionStart(fromView, animated, true);
3058 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003059 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003060 } else {
3061 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003062 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003063 dispatchOnLauncherTransitionStart(fromView, animated, true);
3064 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003065 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003066 dispatchOnLauncherTransitionStart(toView, animated, true);
3067 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003068 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003069 }
3070
Michael Jurkae326f182011-11-21 14:05:46 -08003071 @Override
3072 public void onTrimMemory(int level) {
3073 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003074 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003075 mAppsCustomizeTabHost.onTrimMemory();
3076 }
3077 }
3078
Adam Cohened307df2013-10-02 09:37:31 -07003079 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003080 showWorkspace(animated, null);
3081 }
3082
Adam Cohened307df2013-10-02 09:37:31 -07003083 protected void showWorkspace() {
3084 showWorkspace(true);
3085 }
3086
Adam Cohened66b2b2012-01-23 17:28:51 -08003087 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003088 if (mWorkspace.isInOverviewMode()) {
3089 mWorkspace.exitOverviewMode(animated);
3090 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003091 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003092 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003093 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003094 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003095
Winson Chungc7d2b602012-05-16 17:02:20 -07003096 // Show the search bar (only animate if we were showing the drop target bar in spring
3097 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003098 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003099 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003100 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003101
Michael Jurkab3e22d92011-10-31 15:58:33 -07003102 // Set focus to the AppsCustomize button
3103 if (mAllAppsButton != null) {
3104 mAllAppsButton.requestFocus();
3105 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003106 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003107
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003108 // Change the state *after* we've called all the transition code
3109 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003110
Winson Chung5fb63472011-02-02 17:03:37 -08003111 // Resume the auto-advance of widgets
3112 mUserPresent = true;
3113 updateRunning();
3114
alanv1d4fde62012-10-17 13:15:47 -07003115 // Send an accessibility event to announce the context change
3116 getWindow().getDecorView()
3117 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003118
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003119 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003120 }
3121
Adam Cohened307df2013-10-02 09:37:31 -07003122 void showOverviewMode(boolean animated) {
3123 mWorkspace.setVisibility(View.VISIBLE);
3124 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3125 mState = State.WORKSPACE;
3126 onWorkspaceShown(animated);
3127 }
3128
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003129 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003130 }
3131
Winson Chung82963d52013-10-09 11:20:57 -07003132 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3133 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003134 if (mState != State.WORKSPACE) return;
3135
Winson Chung82963d52013-10-09 11:20:57 -07003136 if (resetPageToZero) {
3137 mAppsCustomizeTabHost.reset();
3138 }
Winson Chungc58497e2013-09-03 17:48:37 -07003139 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003140 mAppsCustomizeTabHost.requestFocus();
3141
Michael Jurkab3e22d92011-10-31 15:58:33 -07003142 // Change the state *after* we've called all the transition code
3143 mState = State.APPS_CUSTOMIZE;
3144
3145 // Pause the auto-advance of widgets until we are out of AllApps
3146 mUserPresent = false;
3147 updateRunning();
3148 closeFolder();
3149
3150 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003151 getWindow().getDecorView()
3152 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003153 }
3154
Adam Cohen7777d962011-08-18 18:58:38 -07003155 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003156 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003157 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003158 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003159 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003160 }
Adam Cohen7777d962011-08-18 18:58:38 -07003161
Adam Cohenad4e15c2013-10-17 16:21:35 -07003162 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003163 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003164 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3165
Winson Chunge7a03942011-08-05 15:05:12 -07003166 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003167 @Override
3168 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003169 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003170 // Before we show workspace, hide all apps again because
3171 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3172 // clean up our state transition functions
3173 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003174 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003175 } else {
3176 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003177 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003178 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003179 }, delay);
3180
Winson Chung557d6ed2011-07-08 15:34:52 -07003181 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003182
Michael Jurkad3ef3062010-11-23 16:23:58 -08003183 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003184 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003185 final boolean animated = true;
3186 final boolean springLoaded = true;
3187 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003188 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003189 }
3190 // Otherwise, we are not in spring loaded mode, so don't do anything.
3191 }
3192
Michael Jurkab3e22d92011-10-31 15:58:33 -07003193 void lockAllApps() {
3194 // TODO
3195 }
3196
3197 void unlockAllApps() {
3198 // TODO
3199 }
3200
Winson Chungf0ea4d32011-06-06 14:27:16 -07003201 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003202 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003203 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003204 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003205 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003206 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003207 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003208 int duration = 0;
3209 if (mSearchDropTargetBar != null) {
3210 duration = mSearchDropTargetBar.getTransitionInDuration();
3211 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003212 mHotseat.animate().alpha(1f).setDuration(duration);
3213 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003214 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003215 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003216 }
3217 }
3218 }
3219
3220 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003221 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003222 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003223 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003224 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003225 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003226 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003227 int duration = 0;
3228 if (mSearchDropTargetBar != null) {
3229 duration = mSearchDropTargetBar.getTransitionOutDuration();
3230 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003231 mHotseat.animate().alpha(0f).setDuration(duration);
3232 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003233 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003234 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003235 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003236 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003237 }
3238
Patrick Dubroy5f445422011-02-18 14:35:21 -08003239 /**
3240 * Add an item from all apps or customize onto the given workspace screen.
3241 * If layout is null, add to the current screen.
3242 */
3243 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003244 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003245 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003246 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003247 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003248
Winson Chungdff8ebb2011-09-08 17:25:31 -07003249 /** Maps the current orientation to an index for referencing orientation correct global icons */
3250 private int getCurrentOrientationIndexForGlobalIcons() {
3251 // default - 0, landscape - 1
3252 switch (getResources().getConfiguration().orientation) {
3253 case Configuration.ORIENTATION_LANDSCAPE:
3254 return 1;
3255 default:
3256 return 0;
3257 }
3258 }
3259
Michael Jurkaae65ee32012-04-30 11:45:54 -07003260 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003261 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003262 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003263 // Look for the toolbar icon specified in the activity meta-data
3264 Bundle metaData = packageManager.getActivityInfo(
3265 activityName, PackageManager.GET_META_DATA).metaData;
3266 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003267 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003268 if (iconResId != 0) {
3269 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003270 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003271 }
3272 }
3273 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003274 // This can happen if the activity defines an invalid drawable
3275 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3276 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003277 } catch (Resources.NotFoundException nfe) {
3278 // This can happen if the activity defines an invalid drawable
3279 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3280 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003281 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003282 return null;
3283 }
3284
3285 // if successful in getting icon, return it; otherwise, set button to use default drawable
3286 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003287 int buttonId, ComponentName activityName, int fallbackDrawableId,
3288 String toolbarResourceName) {
3289 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003290 Resources r = getResources();
3291 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3292 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003293
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003294 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003295 // If we were unable to find the icon via the meta-data, use a generic one
3296 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003297 toolbarIcon = r.getDrawable(fallbackDrawableId);
3298 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003299 if (button != null) {
3300 button.setCompoundDrawables(toolbarIcon, null, null, null);
3301 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003302 return null;
3303 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003304 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003305 if (button != null) {
3306 button.setCompoundDrawables(toolbarIcon, null, null, null);
3307 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003308 return toolbarIcon.getConstantState();
3309 }
3310 }
3311
3312 // if successful in getting icon, return it; otherwise, set button to use default drawable
3313 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003314 int buttonId, ComponentName activityName, int fallbackDrawableId,
3315 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003316 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003317 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003318
Michael Jurka19e0fc52011-07-22 18:00:21 -07003319 if (button != null) {
3320 // If we were unable to find the icon via the meta-data, use a
3321 // generic one
3322 if (toolbarIcon == null) {
3323 button.setImageResource(fallbackDrawableId);
3324 } else {
3325 button.setImageDrawable(toolbarIcon);
3326 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003327 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003328
3329 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3330
Michael Jurka0423dcf2010-10-05 14:56:18 -07003331 }
3332
Winson Chung1b884092012-06-08 17:13:02 -07003333 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003334 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003335 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003336 }
3337
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003338 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003339 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003340 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003341 }
3342
Winson Chungbb185bd2011-11-21 12:31:42 -08003343 private void invalidatePressedFocusedStates(View container, View button) {
3344 if (container instanceof HolographicLinearLayout) {
3345 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3346 layout.invalidatePressedFocusedStates();
3347 } else if (button instanceof HolographicImageView) {
3348 HolographicImageView view = (HolographicImageView) button;
3349 view.invalidatePressedFocusedStates();
3350 }
3351 }
3352
Cristina Stancu476493b2013-08-07 17:20:14 +01003353 public View getQsbBar() {
3354 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003355 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3356 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003357 }
3358 return mQsbBar;
3359 }
3360
3361 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003362 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003363 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003364 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003365 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003366
Winson Chung1cad91e2011-05-25 17:41:01 -07003367 final SearchManager searchManager =
3368 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3369 ComponentName activityName = searchManager.getGlobalSearchActivity();
3370 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003371 int coi = getCurrentOrientationIndexForGlobalIcons();
3372 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003373 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3374 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3375 if (sGlobalSearchIcon[coi] == null) {
3376 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3377 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3378 TOOLBAR_ICON_METADATA_NAME);
3379 }
3380
Winson Chungc51db6a2011-10-05 11:44:49 -07003381 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3382 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003383 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003384 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003385 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003386 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003387 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3388 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003389 if (searchButton != null) searchButton.setVisibility(View.GONE);
3390 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003391 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003392 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003393 }
3394 }
3395
Cristina Stancu476493b2013-08-07 17:20:14 +01003396 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003397 final View searchButtonContainer = findViewById(R.id.search_button_container);
3398 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003399 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003400 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003401 }
3402
Cristina Stancu476493b2013-08-07 17:20:14 +01003403 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003404 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003405 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003406
Winson Chungc51db6a2011-10-05 11:44:49 -07003407 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003408 final SearchManager searchManager =
3409 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3410 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3411
3412 ComponentName activityName = null;
3413 if (globalSearchActivity != null) {
3414 // Check if the global search activity handles voice search
3415 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3416 intent.setPackage(globalSearchActivity.getPackageName());
3417 activityName = intent.resolveActivity(getPackageManager());
3418 }
3419
3420 if (activityName == null) {
3421 // Fallback: check if an activity other than the global search activity
3422 // resolves this
3423 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3424 activityName = intent.resolveActivity(getPackageManager());
3425 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003426 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003427 int coi = getCurrentOrientationIndexForGlobalIcons();
3428 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003429 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3430 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3431 if (sVoiceSearchIcon[coi] == null) {
3432 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3433 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3434 TOOLBAR_ICON_METADATA_NAME);
3435 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003436 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003437 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003438 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003439 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003440 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003441 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003442 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003443 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003444 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003445 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003446 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003447 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003448
Cristina Stancu476493b2013-08-07 17:20:14 +01003449 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003450 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3451 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003452 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003453 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003454 }
3455
Winson Chung5841efa2013-09-30 18:06:44 -07003456 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003457 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3458 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003459 boolean visible = !forceDisableVoiceButtonProxy &&
3460 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003461 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003462 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003463 }
3464 }
Winson Chung5841efa2013-09-30 18:06:44 -07003465
3466 /**
3467 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3468 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3469 */
3470 public void disableVoiceButtonProxy(boolean disabled) {
3471 updateVoiceButtonProxyVisible(disabled);
3472 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003473 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003474 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003475 */
3476 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003477 if (!DISABLE_MARKET_BUTTON) {
3478 final View marketButton = findViewById(R.id.market_button);
3479 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3480 // Find the app market activity by resolving an intent.
3481 // (If multiple app markets are installed, it will return the ResolverActivity.)
3482 ComponentName activityName = intent.resolveActivity(getPackageManager());
3483 if (activityName != null) {
3484 int coi = getCurrentOrientationIndexForGlobalIcons();
3485 mAppMarketIntent = intent;
3486 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3487 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3488 TOOLBAR_ICON_METADATA_NAME);
3489 marketButton.setVisibility(View.VISIBLE);
3490 } else {
3491 // We should hide and disable the view so that we don't try and restore the visibility
3492 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3493 marketButton.setVisibility(View.GONE);
3494 marketButton.setEnabled(false);
3495 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003496 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003497 }
3498
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003499 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003500 if (!DISABLE_MARKET_BUTTON) {
3501 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3502 Resources r = getResources();
3503 Drawable marketIconDrawable = d.newDrawable(r);
3504 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3505 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3506 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003507
Winson Chungd64a6662013-09-30 11:06:59 -07003508 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3509 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003510 }
3511
Michael Jurkad7c28052012-04-27 15:43:36 -07003512 @Override
3513 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003514 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003515 final List<CharSequence> text = event.getText();
3516 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003517 // Populate event with a fake title based on the current state.
3518 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003519 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003520 } else {
3521 text.add(getString(R.string.all_apps_home_button_label));
3522 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003523 return result;
3524 }
3525
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003526 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003527 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003528 */
3529 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3530 @Override
3531 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003532 closeSystemDialogs();
3533 }
3534 }
3535
3536 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003537 * Receives notifications whenever the appwidgets are reset.
3538 */
3539 private class AppWidgetResetObserver extends ContentObserver {
3540 public AppWidgetResetObserver() {
3541 super(new Handler());
3542 }
3543
3544 @Override
3545 public void onChange(boolean selfChange) {
3546 onAppWidgetReset();
3547 }
3548 }
3549
3550 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003551 * If the activity is currently paused, signal that we need to run the passed Runnable
3552 * in onResume.
3553 *
3554 * This needs to be called from incoming places where resources might have been loaded
3555 * while we are paused. That is becaues the Configuration might be wrong
3556 * when we're not running, and if it comes back to what it was when we
3557 * were paused, we are not restarted.
3558 *
3559 * Implementation of the method from LauncherModel.Callbacks.
3560 *
3561 * @return true if we are currently paused. The caller might be able to
3562 * skip some work in that case since we will come back again.
3563 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003564 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003565 if (mPaused) {
3566 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003567 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003568 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003569 }
3570 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003571 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003572 return true;
3573 } else {
3574 return false;
3575 }
3576 }
3577
Michael Jurkac402cd92013-05-20 15:49:32 +02003578 private boolean waitUntilResume(Runnable run) {
3579 return waitUntilResume(run, false);
3580 }
3581
Michael Jurka1e2f4652013-07-08 18:03:46 -07003582 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003583 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003584 }
3585
Michael Jurka7607c2f2013-04-03 14:33:19 -07003586 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003587 * If the activity is currently paused, signal that we need to re-run the loader
3588 * in onResume.
3589 *
3590 * This needs to be called from incoming places where resources might have been loaded
3591 * while we are paused. That is becaues the Configuration might be wrong
3592 * when we're not running, and if it comes back to what it was when we
3593 * were paused, we are not restarted.
3594 *
3595 * Implementation of the method from LauncherModel.Callbacks.
3596 *
3597 * @return true if we are currently paused. The caller might be able to
3598 * skip some work in that case since we will come back again.
3599 */
3600 public boolean setLoadOnResume() {
3601 if (mPaused) {
3602 Log.i(TAG, "setLoadOnResume");
3603 mOnResumeNeedsLoad = true;
3604 return true;
3605 } else {
3606 return false;
3607 }
3608 }
3609
3610 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003612 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003614 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003615 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003616 } else {
3617 return SCREEN_COUNT / 2;
3618 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003620
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 /**
3622 * Refreshes the shortcuts shown on the workspace.
3623 *
3624 * Implementation of the method from LauncherModel.Callbacks.
3625 */
3626 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003627 // If we're starting binding all over again, clear any bind calls we'd postponed in
3628 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3629 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003630 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003631
Winson Chungd64d1762013-08-20 14:37:16 -07003632 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003633 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003634 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003635
Michael Jurka05bf6442011-11-30 20:28:53 -08003636 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003637 if (mHotseat != null) {
3638 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003639 }
3640 }
3641
Adam Cohendcd297f2013-06-18 13:13:40 -07003642 @Override
3643 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003644 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003645
Adam Cohen5084cba2013-09-03 12:01:16 -07003646 // If there are no screens, we need to have an empty screen
3647 if (orderedScreenIds.size() == 0) {
3648 mWorkspace.addExtraEmptyScreen();
3649 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003650
3651 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003652 // setCurrentPage() so ensure that all pages are added before calling this).
3653 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003654 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3655 mWorkspace.createCustomContentPage();
3656 }
Winson Chung64359a52013-07-08 17:17:08 -07003657 }
3658
3659 @Override
3660 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003661 int count = orderedScreenIds.size();
3662 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003663 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003664 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003665 }
3666
Adam Cohen39a06042013-07-19 14:30:12 -07003667 private boolean shouldShowWeightWatcher() {
3668 String spKey = LauncherAppState.getSharedPreferencesKey();
3669 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003670 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003671
3672 return show;
3673 }
3674
3675 private void toggleShowWeightWatcher() {
3676 String spKey = LauncherAppState.getSharedPreferencesKey();
3677 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3678 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3679
3680 show = !show;
3681
3682 SharedPreferences.Editor editor = sp.edit();
3683 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3684 editor.commit();
3685
3686 if (mWeightWatcher != null) {
3687 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3688 }
3689 }
3690
Winson Chungd64d1762013-08-20 14:37:16 -07003691 public void bindAppsAdded(final ArrayList<Long> newScreens,
3692 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003693 final ArrayList<ItemInfo> addAnimated,
3694 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003695 Runnable r = new Runnable() {
3696 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003697 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003698 }
3699 };
3700 if (waitUntilResume(r)) {
3701 return;
3702 }
3703
Winson Chungd64d1762013-08-20 14:37:16 -07003704 // Add the new screens
3705 bindAddScreens(newScreens);
3706
3707 // We add the items without animation on non-visible pages, and with
3708 // animations on the new page (which we will try and snap to).
3709 if (!addNotAnimated.isEmpty()) {
3710 bindItems(addNotAnimated, 0,
3711 addNotAnimated.size(), false);
3712 }
3713 if (!addAnimated.isEmpty()) {
3714 bindItems(addAnimated, 0,
3715 addAnimated.size(), true);
3716 }
Winson Chungc58497e2013-09-03 17:48:37 -07003717
Winson Chung87412982013-10-03 18:34:14 -07003718 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003719 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003720
Winson Chungc58497e2013-09-03 17:48:37 -07003721 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3722 addedApps != null && mAppsCustomizeContent != null) {
3723 mAppsCustomizeContent.addApps(addedApps);
3724 }
Winson Chungd64d1762013-08-20 14:37:16 -07003725 }
3726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003727 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 * Bind the items start-end from the list.
3729 *
3730 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003731 */
Winson Chung64359a52013-07-08 17:17:08 -07003732 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3733 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003734 Runnable r = new Runnable() {
3735 public void run() {
3736 bindItems(shortcuts, start, end, forceAnimateIcons);
3737 }
3738 };
3739 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003740 return;
3741 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003742
Winson Chungf0c6ae02012-03-21 16:10:31 -07003743 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003744 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3745 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003746 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003747 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003748 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003749 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003750 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003751
3752 // Short circuit if we are loading dock items for a configuration which has no dock
3753 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3754 mHotseat == null) {
3755 continue;
3756 }
3757
Joe Onorato9c1289c2009-08-17 11:03:03 -04003758 switch (item.itemType) {
3759 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3760 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003761 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003762 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003763
Winson Chung64359a52013-07-08 17:17:08 -07003764 /*
3765 * TODO: FIX collision case
3766 */
Winson Chungce376632013-07-11 16:12:41 -07003767 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3768 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3769 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3770 throw new RuntimeException("OCCUPIED");
3771 }
Winson Chung64359a52013-07-08 17:17:08 -07003772 }
3773
Adam Cohendcd297f2013-06-18 13:13:40 -07003774 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3775 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003776 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003777 // Animate all the applications up now
3778 shortcut.setAlpha(0f);
3779 shortcut.setScaleX(0f);
3780 shortcut.setScaleY(0f);
3781 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003782 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003783 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003784 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003785 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003786 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003787 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003788 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003789 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3790 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003792 default:
3793 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003794 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003795 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003796
Winson Chung997a9232013-07-24 15:33:46 -07003797 if (animateIcons) {
3798 // Animate to the correct page
3799 if (newShortcutsScreenId > -1) {
3800 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003801 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003802 final Runnable startBounceAnimRunnable = new Runnable() {
3803 public void run() {
3804 anim.playTogether(bounceAnims);
3805 anim.start();
3806 }
3807 };
Winson Chung997a9232013-07-24 15:33:46 -07003808 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003809 // We post the animation slightly delayed to prevent slowdowns
3810 // when we are loading right after we return to launcher.
3811 mWorkspace.postDelayed(new Runnable() {
3812 public void run() {
3813 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003814 mWorkspace.postDelayed(startBounceAnimRunnable,
3815 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003816 }
3817 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003818 } else {
3819 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003820 }
3821 }
Winson Chung64359a52013-07-08 17:17:08 -07003822 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003824 }
3825
Joe Onorato9c1289c2009-08-17 11:03:03 -04003826 /**
3827 * Implementation of the method from LauncherModel.Callbacks.
3828 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003829 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003830 Runnable r = new Runnable() {
3831 public void run() {
3832 bindFolders(folders);
3833 }
3834 };
3835 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003836 return;
3837 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003838 sFolders.clear();
3839 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003840 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003841
3842 /**
3843 * Add the views for a widget to the workspace.
3844 *
3845 * Implementation of the method from LauncherModel.Callbacks.
3846 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003847 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003848 Runnable r = new Runnable() {
3849 public void run() {
3850 bindAppWidget(item);
3851 }
3852 };
3853 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003854 return;
3855 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003856
Daniel Sandler843e8602010-06-07 14:59:01 -04003857 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3858 if (DEBUG_WIDGETS) {
3859 Log.d(TAG, "bindAppWidget: " + item);
3860 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003861 final Workspace workspace = mWorkspace;
3862
3863 final int appWidgetId = item.appWidgetId;
3864 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003865 if (DEBUG_WIDGETS) {
3866 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3867 }
3868
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3870
Joe Onorato9c1289c2009-08-17 11:03:03 -04003871 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003872 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873
Adam Cohendcd297f2013-06-18 13:13:40 -07003874 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003875 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003876 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3877
Joe Onorato9c1289c2009-08-17 11:03:03 -04003878 workspace.requestLayout();
3879
Daniel Sandler843e8602010-06-07 14:59:01 -04003880 if (DEBUG_WIDGETS) {
3881 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3882 + (SystemClock.uptimeMillis()-start) + "ms");
3883 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 }
3885
Adam Cohen1462de32012-07-24 22:34:36 -07003886 public void onPageBoundSynchronously(int page) {
3887 mSynchronouslyBoundPages.add(page);
3888 }
3889
Joe Onorato9c1289c2009-08-17 11:03:03 -04003890 /**
3891 * Callback saying that there aren't any more items to bind.
3892 *
3893 * Implementation of the method from LauncherModel.Callbacks.
3894 */
Adam Cohene25af792013-06-06 23:08:25 -07003895 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003896 Runnable r = new Runnable() {
3897 public void run() {
3898 finishBindingItems(upgradePath);
3899 }
3900 };
3901 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003902 return;
3903 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003904 if (mSavedState != null) {
3905 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003906 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908 mSavedState = null;
3909 }
3910
Adam Cohen1462de32012-07-24 22:34:36 -07003911 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003912
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003913 // If we received the result of any pending adds while the loader was running (e.g. the
3914 // widget configuration forced an orientation change), process them now.
3915 for (int i = 0; i < sPendingAddList.size(); i++) {
3916 completeAdd(sPendingAddList.get(i));
3917 }
3918 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003919
Winson Chungc51db6a2011-10-05 11:44:49 -07003920 // Update the market app icon as necessary (the other icons will be managed in response to
3921 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003922 if (!DISABLE_MARKET_BUTTON) {
3923 updateAppMarketIcon();
3924 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003925
Winson Chungf0c6ae02012-03-21 16:10:31 -07003926 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003927 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003928 mWorkspace.getUniqueComponents(true, null);
3929 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003930 }
Adam Cohen99894d92013-06-14 11:22:59 -07003931
Adam Cohen66a01fd2013-06-11 12:48:00 -07003932 mWorkspace.post(new Runnable() {
3933 @Override
3934 public void run() {
3935 onFinishBindingItems();
3936 }
3937 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003938 }
3939
Winson Chunga0b7e862013-09-05 16:03:15 -07003940 public boolean isAllAppsButtonRank(int rank) {
3941 if (mHotseat != null) {
3942 return mHotseat.isAllAppsButtonRank(rank);
3943 }
3944 return false;
3945 }
3946
Winson Chunga2413752012-04-03 14:22:34 -07003947 private boolean canRunNewAppsAnimation() {
3948 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3949 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3950 }
3951
Winson Chungc9168342013-06-26 14:54:55 -07003952 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3953 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3954 PropertyValuesHolder.ofFloat("alpha", 1f),
3955 PropertyValuesHolder.ofFloat("scaleX", 1f),
3956 PropertyValuesHolder.ofFloat("scaleY", 1f));
3957 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3958 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3959 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3960 return bounceAnim;
3961 }
3962
Adam Cohen27772732013-11-11 14:00:56 +00003963 public boolean useVerticalBarLayout() {
3964 return LauncherAppState.getInstance().getDynamicGrid().
3965 getDeviceProfile().isVerticalBarLayout();
3966 }
3967
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003968 @Override
3969 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003970 boolean searchVisible = updateGlobalSearchIcon();
3971 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003972 if (mSearchDropTargetBar != null) {
3973 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3974 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003975 }
3976
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003977 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003978 * Add the icons for all apps.
3979 *
3980 * Implementation of the method from LauncherModel.Callbacks.
3981 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003982 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003983 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3984 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3985 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3986 getHotseat().addAllAppsFolder(mIconCache, apps,
3987 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3988 }
3989 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003990 }
Winson Chungc58497e2013-09-03 17:48:37 -07003991 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07003992 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07003993 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07003994 mAppsCustomizeContent.onPackagesUpdated(
3995 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07003996 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 }
3999
4000 /**
4001 * A package was updated.
4002 *
4003 * Implementation of the method from LauncherModel.Callbacks.
4004 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004005 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004006 Runnable r = new Runnable() {
4007 public void run() {
4008 bindAppsUpdated(apps);
4009 }
4010 };
4011 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004012 return;
4013 }
4014
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004015 if (mWorkspace != null) {
4016 mWorkspace.updateShortcuts(apps);
4017 }
Winson Chungc58497e2013-09-03 17:48:37 -07004018
4019 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4020 mAppsCustomizeContent != null) {
4021 mAppsCustomizeContent.updateApps(apps);
4022 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004023 }
4024
4025 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004026 * A package was uninstalled. We take both the super set of packageNames
4027 * in addition to specific applications to remove, the reason being that
4028 * this can be called when a package is updated as well. In that scenario,
4029 * we only remove specific components from the workspace, where as
4030 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004031 *
4032 * Implementation of the method from LauncherModel.Callbacks.
4033 */
Winson Chung83892cc2013-05-01 16:53:33 -07004034 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004035 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004036 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004037 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004038 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004039 }
Winson Chungd64d1762013-08-20 14:37:16 -07004040 };
4041 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004042 return;
4043 }
4044
Winson Chungdf95eb12013-10-16 14:57:07 -07004045 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004046 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004047 }
4048 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004049 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004050 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004051
Winson Chunga1820962011-10-03 16:31:06 -07004052 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004053 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004054
Winson Chungdf95eb12013-10-16 14:57:07 -07004055 // Update AllApps
Winson Chungc58497e2013-09-03 17:48:37 -07004056 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4057 mAppsCustomizeContent != null) {
4058 mAppsCustomizeContent.removeApps(appInfos);
4059 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060 }
Joe Onoratobe386092009-11-17 17:32:16 -08004061
4062 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004063 * A number of packages were updated.
4064 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004065 private ArrayList<Object> mWidgetsAndShortcuts;
4066 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004067 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004068 bindPackagesUpdated(mWidgetsAndShortcuts);
4069 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004070 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004071 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004072 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4073 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4074 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004075 return;
4076 }
4077
Winson Chung64359a52013-07-08 17:17:08 -07004078 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004079 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4080 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004081 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004082 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004083 }
4084
Winson Chung400438b2011-01-16 17:53:48 -08004085 private int mapConfigurationOriActivityInfoOri(int configOri) {
4086 final Display d = getWindowManager().getDefaultDisplay();
4087 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4088 switch (d.getRotation()) {
4089 case Surface.ROTATION_0:
4090 case Surface.ROTATION_180:
4091 // We are currently in the same basic orientation as the natural orientation
4092 naturalOri = configOri;
4093 break;
4094 case Surface.ROTATION_90:
4095 case Surface.ROTATION_270:
4096 // We are currently in the other basic orientation to the natural orientation
4097 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4098 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4099 break;
4100 }
4101
4102 int[] oriMap = {
4103 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4104 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4105 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4106 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4107 };
4108 // Since the map starts at portrait, we need to offset if this device's natural orientation
4109 // is landscape.
4110 int indexOffset = 0;
4111 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4112 indexOffset = 1;
4113 }
4114 return oriMap[(d.getRotation() + indexOffset) % 4];
4115 }
Adam Cohen446e9402011-09-15 18:21:21 -07004116
Winson Chung4b919f82012-05-01 10:44:08 -07004117 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004118 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004119 getResources().getBoolean(R.bool.allow_rotation);
4120 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004121 }
Winson Chung4b919f82012-05-01 10:44:08 -07004122 public void lockScreenOrientation() {
4123 if (isRotationEnabled()) {
4124 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4125 .getConfiguration().orientation));
4126 }
4127 }
4128 public void unlockScreenOrientation(boolean immediate) {
4129 if (isRotationEnabled()) {
4130 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004131 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004132 } else {
4133 mHandler.postDelayed(new Runnable() {
4134 public void run() {
4135 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4136 }
4137 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004138 }
Winson Chung4b919f82012-05-01 10:44:08 -07004139 }
Winson Chung400438b2011-01-16 17:53:48 -08004140 }
4141
Winson Chung82f55532011-08-09 14:14:23 -07004142 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004143 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004144 if (DISABLE_CLINGS) {
4145 return false;
4146 }
4147
Winson Chung3a6e7f32013-10-09 15:50:52 -07004148 // For now, limit only to phones
4149 LauncherAppState app = LauncherAppState.getInstance();
4150 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4151 if (grid.isTablet()) {
4152 return false;
4153 }
Winson Chung13dbfe12013-10-31 11:42:39 -07004154 if (grid.isLandscape) {
4155 return false;
4156 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004157
Brett Chabot2a9e2282011-08-23 15:03:13 -07004158 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004159 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004160
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004161 // Disable clings for accessibility when explore by touch is enabled
4162 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4163 ACCESSIBILITY_SERVICE);
4164 if (a11yManager.isTouchExplorationEnabled()) {
4165 return false;
4166 }
4167
Michael Jurkae233a8b2013-03-19 13:49:20 +01004168 // Restricted secondary users (child mode) will potentially have very few apps
4169 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004170// boolean supportsLimitedUsers =
4171// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4172// Account[] accounts = AccountManager.get(this).getAccounts();
4173// if (supportsLimitedUsers && accounts.length == 0) {
4174// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4175// Bundle restrictions = um.getUserRestrictions();
4176// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4177// return false;
4178// }
4179// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004180 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004181 }
Michael Jurka22143132012-10-04 17:42:38 +02004182
Winson Chungfa545132013-09-26 17:45:32 -07004183 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004184 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004185 Cling cling = (Cling) findViewById(clingId);
4186 View scrim = null;
4187 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004188 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004189 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004190 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004191 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004192 cling.show(animate, SHOW_CLING_DURATION);
4193
4194 if (dimNavBarVisibilty) {
4195 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4196 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004197 }
4198 }
4199 return cling;
4200 }
Michael Jurka22143132012-10-04 17:42:38 +02004201
Winson Chungaf40f202013-09-18 18:26:31 -07004202 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4203 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004204 // To catch cases where siblings of top-level views are made invisible, just check whether
4205 // the cling is directly set to GONE before dismissing it.
4206 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004207 final Runnable cleanUpClingCb = new Runnable() {
4208 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004209 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004210 // We should update the shared preferences on a background thread
Michael Jurka43467462013-11-14 12:03:58 +01004211 new AsyncTask<Void, Void, Void>() {
4212 public Void doInBackground(Void ... args) {
Winson Chung46353de2012-02-16 14:05:10 -08004213 SharedPreferences.Editor editor = mSharedPrefs.edit();
4214 editor.putBoolean(flag, true);
4215 editor.commit();
Michael Jurka43467462013-11-14 12:03:58 +01004216 return null;
Winson Chung46353de2012-02-16 14:05:10 -08004217 }
Michael Jurka43467462013-11-14 12:03:58 +01004218 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungaf40f202013-09-18 18:26:31 -07004219 if (postAnimationCb != null) {
4220 postAnimationCb.run();
4221 }
4222 }
4223 };
4224 if (duration <= 0) {
4225 cleanUpClingCb.run();
4226 } else {
4227 cling.hide(duration, cleanUpClingCb);
4228 }
Michael Jurka22143132012-10-04 17:42:38 +02004229 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004230
4231 if (restoreNavBarVisibilty) {
4232 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4233 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4234 }
Winson Chung82f55532011-08-09 14:14:23 -07004235 }
4236 }
Michael Jurka22143132012-10-04 17:42:38 +02004237
Winson Chung9d9d74f2011-09-19 11:49:12 -07004238 private void removeCling(int id) {
4239 final View cling = findViewById(id);
4240 if (cling != null) {
4241 final ViewGroup parent = (ViewGroup) cling.getParent();
4242 parent.post(new Runnable() {
4243 @Override
4244 public void run() {
4245 parent.removeView(cling);
4246 }
4247 });
Michael Jurka22143132012-10-04 17:42:38 +02004248 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004249 }
4250 }
Michael Jurka974c3862012-05-22 22:00:31 -07004251
4252 private boolean skipCustomClingIfNoAccounts() {
4253 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4254 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4255 if (customCling) {
4256 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004257 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004258 Account[] accounts = am.getAccountsByType("com.google");
4259 return accounts.length == 0;
4260 }
4261 return false;
4262 }
4263
Winson Chung82e5c982013-10-09 12:04:50 -07004264 public void updateCustomContentHintVisibility() {
4265 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4266 String ccHintStr = getFirstRunCustomContentHint();
4267
4268 if (mWorkspace.hasCustomContent()) {
4269 // Show the custom content hint if ccHintStr is not empty
4270 if (cling != null) {
4271 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4272 }
4273 } else {
4274 // Hide the custom content hint
4275 if (cling != null) {
4276 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4277 }
4278 }
4279 }
4280
4281 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4282 boolean animate) {
4283 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4284 if (ccHint != null) {
4285 if (visible && !ccHintStr.isEmpty()) {
4286 ccHint.setText(ccHintStr);
4287 ccHint.setVisibility(View.VISIBLE);
4288 if (animate) {
4289 ccHint.setAlpha(0f);
4290 ccHint.animate().alpha(1f)
4291 .setDuration(SHOW_CLING_DURATION)
4292 .start();
4293 } else {
4294 ccHint.setAlpha(1f);
4295 }
4296 } else {
4297 if (animate) {
4298 ccHint.animate().alpha(0f)
4299 .setDuration(SHOW_CLING_DURATION)
4300 .setListener(new AnimatorListenerAdapter() {
4301 @Override
4302 public void onAnimationEnd(Animator animation) {
4303 ccHint.setVisibility(View.GONE);
4304 }
4305 })
4306 .start();
4307 } else {
4308 ccHint.setAlpha(0f);
4309 ccHint.setVisibility(View.GONE);
4310 }
4311 }
4312 }
4313 }
4314
Winson Chungaf40f202013-09-18 18:26:31 -07004315 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004316 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004317 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004318 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004319 // If we're not using the default workspace layout, replace workspace cling
4320 // with a custom workspace cling (usually specified in an overlay)
4321 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004322 if (!DISABLE_CUSTOM_CLINGS) {
4323 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4324 getResources().getBoolean(R.bool.config_useCustomClings)) {
4325 // Use a custom cling
4326 View cling = findViewById(R.id.workspace_cling);
4327 ViewGroup clingParent = (ViewGroup) cling.getParent();
4328 int clingIndex = clingParent.indexOfChild(cling);
4329 clingParent.removeViewAt(clingIndex);
4330 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4331 clingParent.addView(customCling, clingIndex);
4332 customCling.setId(R.id.workspace_cling);
4333 }
4334 }
4335 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4336 if (cling != null) {
4337 String sbHintStr = getFirstRunClingSearchBarHint();
4338 String ccHintStr = getFirstRunCustomContentHint();
4339 if (!sbHintStr.isEmpty()) {
4340 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4341 sbHint.setText(sbHintStr);
4342 sbHint.setVisibility(View.VISIBLE);
4343 }
Winson Chung82e5c982013-10-09 12:04:50 -07004344 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004345 }
Winson Chungfa545132013-09-26 17:45:32 -07004346 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004347 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004348 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004349 }
4350 }
Winson Chungaf40f202013-09-18 18:26:31 -07004351
Winson Chunge6eabff2013-09-24 11:01:23 -07004352 protected String getFirstRunClingSearchBarHint() {
4353 return "";
4354 }
4355 protected String getFirstRunCustomContentHint() {
4356 return "";
4357 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004358 protected int getFirstRunFocusedHotseatAppDrawableId() {
4359 return -1;
4360 }
4361 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4362 return null;
4363 }
4364 protected int getFirstRunFocusedHotseatAppRank() {
4365 return -1;
4366 }
4367 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4368 return "";
4369 }
4370 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4371 return "";
4372 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004373
Winson Chungaf40f202013-09-18 18:26:31 -07004374 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004375 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004376 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004377 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004378 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4379
4380 // Set the focused hotseat app if there is one
4381 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4382 getFirstRunFocusedHotseatAppRank(),
4383 getFirstRunFocusedHotseatAppComponentName(),
4384 getFirstRunFocusedHotseatAppBubbleTitle(),
4385 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004386 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004387 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004388 }
4389 }
4390 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004391 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004392 if (isClingsEnabled() &&
4393 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004394 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4395 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004396 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004397 } else {
4398 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004399 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004400 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004401 }
Michael Jurka104c4562013-07-08 18:03:46 -07004402 protected SharedPreferences getSharedPrefs() {
4403 return mSharedPrefs;
4404 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004405 public boolean isFolderClingVisible() {
4406 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004407 if (cling != null) {
4408 return cling.getVisibility() == View.VISIBLE;
4409 }
4410 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004411 }
Winson Chungaf40f202013-09-18 18:26:31 -07004412 public void dismissFirstRunCling(View v) {
4413 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4414 Runnable cb = new Runnable() {
4415 public void run() {
4416 // Show the workspace cling next
4417 showFirstRunWorkspaceCling();
4418 }
4419 };
4420 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4421 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004422
4423 // Fade out the search bar for the workspace cling coming up
4424 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004425 }
Winson Chung82f55532011-08-09 14:14:23 -07004426 public void dismissWorkspaceCling(View v) {
4427 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004428 Runnable cb = null;
4429 if (v == null) {
4430 cb = new Runnable() {
4431 public void run() {
4432 mWorkspace.enterOverviewMode();
4433 }
4434 };
4435 }
4436 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4437 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004438
4439 // Fade in the search bar
4440 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004441 }
4442 public void dismissFolderCling(View v) {
4443 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004444 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4445 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004446 }
4447
Winson Chung80baf5a2010-08-09 16:03:15 -07004448 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004449 * Prints out out state for debugging.
4450 */
4451 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004452 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004453 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004454 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4455 Log.d(TAG, "mRestoring=" + mRestoring);
4456 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4457 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004458 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004459 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004460
Winson Chung785d2eb2011-04-14 16:08:02 -07004461 if (mAppsCustomizeContent != null) {
4462 mAppsCustomizeContent.dumpState();
4463 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004464 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004465 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004466
4467 @Override
4468 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4469 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004470 synchronized (sDumpLogs) {
4471 writer.println(" ");
4472 writer.println("Debug logs: ");
4473 for (int i = 0; i < sDumpLogs.size(); i++) {
4474 writer.println(" " + sDumpLogs.get(i));
4475 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004476 }
4477 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004478
4479 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004480 if (DEBUG_DUMP_LOG) {
4481 synchronized (sDumpLogs) {
4482 Log.d(TAG, "");
4483 Log.d(TAG, "*********************");
4484 Log.d(TAG, "Launcher debug logs: ");
4485 for (int i = 0; i < sDumpLogs.size(); i++) {
4486 Log.d(TAG, " " + sDumpLogs.get(i));
4487 }
4488 Log.d(TAG, "*********************");
4489 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004490 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004491 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004492 }
4493
4494 public static void addDumpLog(String tag, String log, boolean debugLog) {
4495 if (debugLog) {
4496 Log.d(tag, log);
4497 }
Winson Chungede41292013-09-19 16:27:36 -07004498 if (DEBUG_DUMP_LOG) {
4499 sDateStamp.setTime(System.currentTimeMillis());
4500 synchronized (sDumpLogs) {
4501 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004502 }
Winson Chungede41292013-09-19 16:27:36 -07004503 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004504 }
4505
Winson Chungede41292013-09-19 16:27:36 -07004506 public void dumpLogsToLocalData() {
4507 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004508 new AsyncTask<Void, Void, Void>() {
4509 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004510 boolean success = false;
4511 sDateStamp.setTime(sRunStart);
4512 String FILENAME = sDateStamp.getMonth() + "-"
4513 + sDateStamp.getDay() + "_"
4514 + sDateStamp.getHours() + "-"
4515 + sDateStamp.getMinutes() + "_"
4516 + sDateStamp.getSeconds() + ".txt";
4517
4518 FileOutputStream fos = null;
4519 File outFile = null;
4520 try {
4521 outFile = new File(getFilesDir(), FILENAME);
4522 outFile.createNewFile();
4523 fos = new FileOutputStream(outFile);
4524 } catch (Exception e) {
4525 e.printStackTrace();
4526 }
4527 if (fos != null) {
4528 PrintWriter writer = new PrintWriter(fos);
4529
4530 writer.println(" ");
4531 writer.println("Debug logs: ");
4532 synchronized (sDumpLogs) {
4533 for (int i = 0; i < sDumpLogs.size(); i++) {
4534 writer.println(" " + sDumpLogs.get(i));
4535 }
4536 }
4537 writer.close();
4538 }
4539 try {
4540 if (fos != null) {
4541 fos.close();
4542 success = true;
4543 }
4544 } catch (IOException e) {
4545 e.printStackTrace();
4546 }
Michael Jurka43467462013-11-14 12:03:58 +01004547 return null;
Winson Chungede41292013-09-19 16:27:36 -07004548 }
Michael Jurka43467462013-11-14 12:03:58 +01004549 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004550 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004552}
Michael Jurka2763be32011-02-24 11:19:57 -08004553
Michael Jurkaabded662011-03-04 12:06:57 -08004554interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004555 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004556 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004557 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004558 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004559 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004560}