blob: 0637b24f0d60f07fede04ccd4267f7e2f1656966 [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;
582 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700583 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 public void run() {
585 writeConfiguration(Launcher.this, localeConfiguration);
586 }
587 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700588 }
589 }
590
591 private static class LocaleConfiguration {
592 public String locale;
593 public int mcc = -1;
594 public int mnc = -1;
595 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700596
Romain Guy98d01652009-06-30 16:21:04 -0700597 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
598 DataInputStream in = null;
599 try {
600 in = new DataInputStream(context.openFileInput(PREFERENCES));
601 configuration.locale = in.readUTF();
602 configuration.mcc = in.readInt();
603 configuration.mnc = in.readInt();
604 } catch (FileNotFoundException e) {
605 // Ignore
606 } catch (IOException e) {
607 // Ignore
608 } finally {
609 if (in != null) {
610 try {
611 in.close();
612 } catch (IOException e) {
613 // Ignore
614 }
615 }
616 }
617 }
618
619 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
620 DataOutputStream out = null;
621 try {
622 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
623 out.writeUTF(configuration.locale);
624 out.writeInt(configuration.mcc);
625 out.writeInt(configuration.mnc);
626 out.flush();
627 } catch (FileNotFoundException e) {
628 // Ignore
629 } catch (IOException e) {
630 //noinspection ResultOfMethodCallIgnored
631 context.getFileStreamPath(PREFERENCES).delete();
632 } finally {
633 if (out != null) {
634 try {
635 out.close();
636 } catch (IOException e) {
637 // Ignore
638 }
639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
641 }
642
Anton Hanssona2f665f2013-09-26 12:18:32 +0100643 public Stats getStats() {
644 return mStats;
645 }
646
Bjorn Bringertc459e522013-06-07 19:36:01 +0100647 public LayoutInflater getInflater() {
648 return mInflater;
649 }
650
Adam Cohen716b51e2011-06-30 12:09:54 -0700651 public DragLayer getDragLayer() {
652 return mDragLayer;
653 }
654
Winson Chung36a62fe2012-05-06 18:04:42 -0700655 boolean isDraggingEnabled() {
656 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
657 // that is subsequently removed from the workspace in startBinding().
658 return !mModel.isLoadingWorkspace();
659 }
660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 static int getScreen() {
662 synchronized (sLock) {
663 return sScreen;
664 }
665 }
666
667 static void setScreen(int screen) {
668 synchronized (sLock) {
669 sScreen = screen;
670 }
671 }
672
Winson Chung557d6ed2011-07-08 15:34:52 -0700673 /**
674 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
675 * a configuration step, this allows the proper animations to run after other transitions.
676 */
677 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700678 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 switch (args.requestCode) {
680 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700682 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 break;
684 case REQUEST_PICK_SHORTCUT:
685 processShortcut(args.intent);
686 break;
687 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700688 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700689 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700690 result = true;
691 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800692 case REQUEST_CREATE_APPWIDGET:
693 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700694 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700695 result = true;
696 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 }
Michael Jurka27614d22012-04-02 06:40:22 -0700698 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
699 // if you turned the screen off and then back while in All Apps, Launcher would not
700 // return to the workspace. Clearing mAddInfo.container here fixes this issue
701 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700702 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 }
704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 protected void onActivityResult(
707 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700708 // Reset the startActivity waiting flag
709 mWaitingForResult = false;
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 Chung82963d52013-10-09 11:20:57 -0700872 showAllApps(false, AppsCustomizePagedView.ContentType.Applications, 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 Chung82963d52013-10-09 11:20:57 -07001194 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001195 }
1196 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001197 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1198
1199 View wallpaperButton = findViewById(R.id.wallpaper_button);
1200 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001201 @Override
1202 public void onClick(View arg0) {
1203 startWallpaper();
1204 }
1205 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001206 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1207
1208 View settingsButton = findViewById(R.id.settings_button);
1209 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001210 @Override
1211 public void onClick(View arg0) {
1212 startSettings();
1213 }
1214 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001215 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001216 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001217
Winson Chung4c98d922011-05-31 16:50:48 -07001218 // Setup the workspace
1219 mWorkspace.setHapticFeedbackEnabled(false);
1220 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001221 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001222 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001223
Winson Chungf0ea4d32011-06-06 14:27:16 -07001224 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001225 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001226
Winson Chungf0ea4d32011-06-06 14:27:16 -07001227 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001228 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001229 mAppsCustomizeContent = (AppsCustomizePagedView)
1230 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1231 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001232
Winson Chung3d503fb2011-07-13 17:25:49 -07001233 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001234 dragController.setDragScoller(mWorkspace);
1235 dragController.setScrollView(mDragLayer);
1236 dragController.setMoveTarget(mWorkspace);
1237 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001238 if (mSearchDropTargetBar != null) {
1239 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001240 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001241
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001242 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001243 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001244 mWeightWatcher = new WeightWatcher(this);
1245 mWeightWatcher.setAlpha(0.5f);
1246 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001247 new FrameLayout.LayoutParams(
1248 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001249 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001250 Gravity.BOTTOM)
1251 );
Adam Cohen39a06042013-07-19 14:30:12 -07001252
1253 boolean show = shouldShowWeightWatcher();
1254 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
1257
1258 /**
1259 * Creates a view representing a shortcut.
1260 *
1261 * @param info The data structure describing the shortcut.
1262 *
1263 * @return A View inflated from R.layout.application.
1264 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001265 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001267 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
1269
1270 /**
1271 * Creates a view representing a shortcut inflated from the specified resource.
1272 *
1273 * @param layoutResId The id of the XML layout used to create the shortcut.
1274 * @param parent The group the shortcut belongs to.
1275 * @param info The data structure describing the shortcut.
1276 *
1277 * @return A View inflated from layoutResId.
1278 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001279 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001280 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1281 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 return favorite;
1284 }
1285
1286 /**
1287 * Add an application shortcut to the workspace.
1288 *
1289 * @param data The intent describing the application.
1290 * @param cellInfo The position on screen where to create the shortcut.
1291 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001292 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001293 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001294 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001295
Adam Cohenfbba09b2011-07-18 21:43:05 -07001296 // First we check if we already know the exact location where we want to add this item.
1297 if (cellX >= 0 && cellY >= 0) {
1298 cellXY[0] = cellX;
1299 cellXY[1] = cellY;
1300 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001301 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001302 return;
1303 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001305 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001306
Romain Guy73b979d2009-06-09 12:57:21 -07001307 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001308 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001310 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001311 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001312 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001313 } else {
1314 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
1316 }
Romain Guycbb89e42009-06-08 15:52:54 -07001317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 /**
1319 * Add a shortcut to the workspace.
1320 *
1321 * @param data The intent describing the shortcut.
1322 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001324 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001325 int cellY) {
1326 int[] cellXY = mTmpAddItemCellCoordinates;
1327 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001328 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001329
Michael Jurkad3ef3062010-11-23 16:23:58 -08001330 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001331
Adam Cohen558baaf2011-08-15 15:22:57 -07001332 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001333 if (info == null) {
1334 return;
1335 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001336 final View view = createShortcut(info);
1337
Adam Cohenfbba09b2011-07-18 21:43:05 -07001338 // First we check if we already know the exact location where we want to add this item.
1339 if (cellX >= 0 && cellY >= 0) {
1340 cellXY[0] = cellX;
1341 cellXY[1] = cellY;
1342 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001343
1344 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001345 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001346 true, null,null)) {
1347 return;
1348 }
1349 DragObject dragObject = new DragObject();
1350 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001351 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1352 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001353 return;
1354 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001355 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001356 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001358 foundCellSpan = (result != null);
1359 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001360 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001361 }
1362
1363 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001364 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001365 return;
1366 }
1367
Adam Cohendcd297f2013-06-18 13:13:40 -07001368 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369
1370 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001371 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001372 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
1374 }
1375
Adam Cohen2f093b62012-04-30 18:59:53 -07001376 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1377 int minHeight) {
1378 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001379 // We want to account for the extra amount of padding that we are adding to the widget
1380 // to ensure that it gets the full amount of space that it has requested
1381 int requiredWidth = minWidth + padding.left + padding.right;
1382 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001383 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001384 }
1385
Adam Cohen2f093b62012-04-30 18:59:53 -07001386 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1387 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001388 }
1389
Adam Cohen2f093b62012-04-30 18:59:53 -07001390 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1391 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001392 }
1393
Adam Cohen2f093b62012-04-30 18:59:53 -07001394 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1395 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001396 }
1397
Adam Cohen2f093b62012-04-30 18:59:53 -07001398 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1399 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001400 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001401 }
1402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001404 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001406 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001407 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001409 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001410 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1411 if (appWidgetInfo == null) {
1412 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1413 }
Romain Guycbb89e42009-06-08 15:52:54 -07001414
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001415 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001416 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001417
Adam Cohen2f093b62012-04-30 18:59:53 -07001418 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1419 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001420
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001422 // We have saved the position to which the widget was dragged-- this really only matters
1423 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001424 int[] cellXY = mTmpAddItemCellCoordinates;
1425 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001426 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001427 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001428 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1429 cellXY[0] = mPendingAddInfo.cellX;
1430 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001431 spanXY[0] = mPendingAddInfo.spanX;
1432 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001433 foundCellSpan = true;
1434 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001435 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001436 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001437 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1438 spanXY[1], cellXY, finalSpan);
1439 spanXY[0] = finalSpan[0];
1440 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001441 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001442 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001443 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001444 }
1445
Michael Jurka0280c3b2010-09-17 15:00:07 -07001446 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001447 if (appWidgetId != -1) {
1448 // Deleting an app widget ID is a void call but writes to disk before returning
1449 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001450 new Thread("deleteAppWidgetId") {
1451 public void run() {
1452 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1453 }
1454 }.start();
1455 }
Winson Chung93eef082012-03-23 15:59:27 -07001456 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001457 return;
1458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001460 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001461 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1462 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001463 launcherInfo.spanX = spanXY[0];
1464 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001465 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1466 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001469 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470
1471 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001472 if (hostView == null) {
1473 // Perform actual inflation because we're live
1474 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1475 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1476 } else {
1477 // The AppWidgetHostView has already been inflated and instantiated
1478 launcherInfo.hostView = hostView;
1479 }
Romain Guycbb89e42009-06-08 15:52:54 -07001480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001482 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001483 launcherInfo.notifyWidgetSizeChanged(this);
1484
Adam Cohendcd297f2013-06-18 13:13:40 -07001485 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001486 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001487
1488 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001490 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
Romain Guycbb89e42009-06-08 15:52:54 -07001492
Adam Cohended9f8d2010-11-03 13:25:16 -07001493 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1494 @Override
1495 public void onReceive(Context context, Intent intent) {
1496 final String action = intent.getAction();
1497 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1498 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001499 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001500 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001501
Winson Chungc100e8e2011-08-09 16:02:43 -07001502 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001503 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001504 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001505 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001506 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001507 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1508 mUserPresent = true;
1509 updateRunning();
1510 }
1511 }
1512 };
1513
1514 @Override
1515 public void onAttachedToWindow() {
1516 super.onAttachedToWindow();
1517
1518 // Listen for broadcasts related to user-presence
1519 final IntentFilter filter = new IntentFilter();
1520 filter.addAction(Intent.ACTION_SCREEN_OFF);
1521 filter.addAction(Intent.ACTION_USER_PRESENT);
1522 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001523 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001524 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001525 mVisible = true;
1526 }
1527
1528 @Override
1529 public void onDetachedFromWindow() {
1530 super.onDetachedFromWindow();
1531 mVisible = false;
1532
Adam Cohend113e0c2010-11-11 10:48:05 -08001533 if (mAttached) {
1534 unregisterReceiver(mReceiver);
1535 mAttached = false;
1536 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001537 updateRunning();
1538 }
1539
1540 public void onWindowVisibilityChanged(int visibility) {
1541 mVisible = visibility == View.VISIBLE;
1542 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001543 // The following code used to be in onResume, but it turns out onResume is called when
1544 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1545 // is a more appropriate event to handle
1546 if (mVisible) {
1547 mAppsCustomizeTabHost.onWindowVisible();
1548 if (!mWorkspaceLoading) {
1549 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001550 // We want to let Launcher draw itself at least once before we force it to build
1551 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001552 // apps is nice and speedy.
1553 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001554 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001555 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001556 if (mStarted) return;
1557 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001558 // We delay the layer building a bit in order to give
1559 // other message processing a time to run. In particular
1560 // this avoids a delay in hiding the IME if it was
1561 // currently shown, because doing that may involve
1562 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001563 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001564 final ViewTreeObserver.OnDrawListener listener = this;
1565 mWorkspace.post(new Runnable() {
1566 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001567 if (mWorkspace != null &&
1568 mWorkspace.getViewTreeObserver() != null) {
1569 mWorkspace.getViewTreeObserver().
1570 removeOnDrawListener(listener);
1571 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001572 }
1573 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001574 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001575 }
1576 });
1577 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001578 // When Launcher comes back to foreground, a different Activity might be responsible for
1579 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001580 if (!DISABLE_MARKET_BUTTON) {
1581 updateAppMarketIcon();
1582 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001583 clearTypedText();
1584 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001585 }
1586
1587 private void sendAdvanceMessage(long delay) {
1588 mHandler.removeMessages(ADVANCE_MSG);
1589 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1590 mHandler.sendMessageDelayed(msg, delay);
1591 mAutoAdvanceSentTime = System.currentTimeMillis();
1592 }
1593
1594 private void updateRunning() {
1595 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1596 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1597 mAutoAdvanceRunning = autoAdvanceRunning;
1598 if (autoAdvanceRunning) {
1599 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1600 sendAdvanceMessage(delay);
1601 } else {
1602 if (!mWidgetsToAdvance.isEmpty()) {
1603 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1604 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1605 }
1606 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001607 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001608 }
1609 }
1610 }
1611
1612 private final Handler mHandler = new Handler() {
1613 @Override
1614 public void handleMessage(Message msg) {
1615 if (msg.what == ADVANCE_MSG) {
1616 int i = 0;
1617 for (View key: mWidgetsToAdvance.keySet()) {
1618 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1619 final int delay = mAdvanceStagger * i;
1620 if (v instanceof Advanceable) {
1621 postDelayed(new Runnable() {
1622 public void run() {
1623 ((Advanceable) v).advance();
1624 }
1625 }, delay);
1626 }
1627 i++;
1628 }
1629 sendAdvanceMessage(mAdvanceInterval);
1630 }
1631 }
1632 };
1633
1634 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001635 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001636 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1637 if (v instanceof Advanceable) {
1638 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001639 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 updateRunning();
1641 }
1642 }
1643
1644 void removeWidgetToAutoAdvance(View hostView) {
1645 if (mWidgetsToAdvance.containsKey(hostView)) {
1646 mWidgetsToAdvance.remove(hostView);
1647 updateRunning();
1648 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001649 }
1650
Joe Onorato9c1289c2009-08-17 11:03:03 -04001651 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001653 launcherInfo.hostView = null;
1654 }
1655
Winson Chung93eef082012-03-23 15:59:27 -07001656 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1657 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1658 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001659 }
1660
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001661 public LauncherAppWidgetHost getAppWidgetHost() {
1662 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 }
Romain Guycbb89e42009-06-08 15:52:54 -07001664
Winson Chunga9abd0e2010-10-27 17:18:37 -07001665 public LauncherModel getModel() {
1666 return mModel;
1667 }
1668
Bjorn Bringertc459e522013-06-07 19:36:01 +01001669 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001670 getWindow().closeAllPanels();
1671
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001672 // Whatever we were doing is hereby canceled.
1673 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001674 }
1675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 @Override
1677 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001678 long startTime = 0;
1679 if (DEBUG_RESUME_TIME) {
1680 startTime = System.currentTimeMillis();
1681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 super.onNewIntent(intent);
1683
1684 // Close the menu
1685 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001686 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001687 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001688
Adam Cohened307df2013-10-02 09:37:31 -07001689 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1690 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1691 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001692
Adam Cohen6fecd412013-10-02 17:41:50 -07001693 if (mWorkspace == null) {
1694 // Can be cases where mWorkspace is null, this prevents a NPE
1695 return;
1696 }
1697 Folder openFolder = mWorkspace.getOpenFolder();
1698 // In all these cases, only animate if we're already on home
1699 mWorkspace.exitWidgetResizeMode();
1700 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1701 openFolder == null) {
1702 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001703 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001704
Adam Cohen6fecd412013-10-02 17:41:50 -07001705 closeFolder();
1706 exitSpringLoadedDragMode();
1707
1708 // If we are already on home, then just animate back to the workspace,
1709 // otherwise, just wait until onResume to set the state back to Workspace
1710 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001711 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001712 } else {
1713 mOnResumeState = State.WORKSPACE;
1714 }
1715
1716 final View v = getWindow().peekDecorView();
1717 if (v != null && v.getWindowToken() != null) {
1718 InputMethodManager imm = (InputMethodManager)getSystemService(
1719 INPUT_METHOD_SERVICE);
1720 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1721 }
1722
1723 // Reset the apps customize page
1724 if (mAppsCustomizeTabHost != null) {
1725 mAppsCustomizeTabHost.reset();
1726 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 }
Adam Cohened307df2013-10-02 09:37:31 -07001728
Michael Jurka447bf842013-05-15 14:52:15 +02001729 if (DEBUG_RESUME_TIME) {
1730 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 }
1733
1734 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001735 public void onRestoreInstanceState(Bundle state) {
1736 super.onRestoreInstanceState(state);
1737 for (int page: mSynchronouslyBoundPages) {
1738 mWorkspace.restoreInstanceStateForChild(page);
1739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 }
1741
1742 @Override
1743 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001744 if (mWorkspace.getChildCount() > 0) {
1745 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getRestorePage());
1746 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001747 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748
Michael Jurka883f55b2010-10-21 15:47:14 -07001749 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001750 // We close any open folder since it will not be re-opened, and we need to make sure
1751 // this state is reflected.
1752 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753
Adam Cohendcd297f2013-06-18 13:13:40 -07001754 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001755 mWaitingForResult) {
1756 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001757 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001758 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1759 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001760 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1761 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1762 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001763 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 }
1765
1766 if (mFolderInfo != null && mWaitingForResult) {
1767 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1768 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1769 }
Michael Jurka946ad472010-07-09 18:05:18 -07001770
Winson Chung785d2eb2011-04-14 16:08:02 -07001771 // Save the current AppsCustomize tab
1772 if (mAppsCustomizeTabHost != null) {
1773 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1774 if (currentTabTag != null) {
1775 outState.putString("apps_customize_currentTab", currentTabTag);
1776 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001777 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1778 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
1781
1782 @Override
1783 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001785
Winson Chunge7a03942011-08-05 15:05:12 -07001786 // Remove all pending runnables
1787 mHandler.removeMessages(ADVANCE_MSG);
1788 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001789 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001790
Winson Chungcd2b0142011-06-08 16:02:26 -07001791 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001792 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001793 mModel.stopLoader();
1794 app.setLauncher(null);
1795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001797 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001799 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001801 mAppWidgetHost = null;
1802
1803 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804
1805 TextKeyListener.getInstance().release();
1806
Winson Chung81b52252012-08-27 15:34:29 -07001807 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1808 // to prevent leaking Launcher activities on orientation change.
1809 if (mModel != null) {
1810 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1811 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001812
1813 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001814 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001815
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001816 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001817 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1818 mWorkspace.removeAllViews();
1819 mWorkspace = null;
1820 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001821
Michael Jurka2ecf9952012-06-18 12:52:28 -07001822 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 }
1824
Adam Cohena9cf38f2011-05-02 15:36:58 -07001825 public DragController getDragController() {
1826 return mDragController;
1827 }
1828
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 @Override
1830 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001831 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 super.startActivityForResult(intent, requestCode);
1833 }
1834
Winson Chung70d72102011-08-12 11:24:35 -07001835 /**
1836 * Indicates that we want global search for this activity by setting the globalSearch
1837 * argument for {@link #startSearch} to true.
1838 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001840 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001842
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001843 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001844
Karl Rosaen138a0412009-04-23 19:00:21 -07001845 if (initialQuery == null) {
1846 // Use any text typed in the launcher as the initial query
1847 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 if (appSearchData == null) {
1850 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001851 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 }
Winson Chungadf0c182012-08-23 14:59:07 -07001853 Rect sourceBounds = new Rect();
1854 if (mSearchDropTargetBar != null) {
1855 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1856 }
Romain Guycbb89e42009-06-08 15:52:54 -07001857
Bjorn Bringertc459e522013-06-07 19:36:01 +01001858 startSearch(initialQuery, selectInitialQuery,
1859 appSearchData, sourceBounds);
1860 }
1861
1862 public void startSearch(String initialQuery,
1863 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001864 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001865 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001866 }
1867
1868 /**
1869 * Starts the global search activity. This code is a copied from SearchManager
1870 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001871 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001872 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001873 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001874 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1875 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1876 if (globalSearchActivity == null) {
1877 Log.w(TAG, "No global search activity found.");
1878 return;
1879 }
1880 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1881 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1882 intent.setComponent(globalSearchActivity);
1883 // Make sure that we have a Bundle to put source in
1884 if (appSearchData == null) {
1885 appSearchData = new Bundle();
1886 } else {
1887 appSearchData = new Bundle(appSearchData);
1888 }
1889 // Set source to package name of app that starts global search, if not set already.
1890 if (!appSearchData.containsKey("source")) {
1891 appSearchData.putString("source", getPackageName());
1892 }
1893 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1894 if (!TextUtils.isEmpty(initialQuery)) {
1895 intent.putExtra(SearchManager.QUERY, initialQuery);
1896 }
1897 if (selectInitialQuery) {
1898 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1899 }
1900 intent.setSourceBounds(sourceBounds);
1901 try {
1902 startActivity(intent);
1903 } catch (ActivityNotFoundException ex) {
1904 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
1907
Winson Chung70d72102011-08-12 11:24:35 -07001908 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001909 public boolean onPrepareOptionsMenu(Menu menu) {
1910 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001911 if (!mWorkspace.isInOverviewMode()) {
1912 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001913 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001914 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001915 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001917 @Override
1918 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001919 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001920 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001921 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001922 }
1923
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 public boolean isWorkspaceLocked() {
1925 return mWorkspaceLoading || mWaitingForResult;
1926 }
1927
Michael Jurka0280c3b2010-09-17 15:00:07 -07001928 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001929 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001930 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001931 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1932 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001933 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001934 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001935 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001936
Adam Cohenad4e15c2013-10-17 16:21:35 -07001937 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1938 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
1939 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1940 }
1941
1942 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1943 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
1944 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001945 if (appWidgetInfo.configure != null) {
1946 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08001947 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07001948
Bjorn Bringert7984c942009-12-09 15:38:25 +00001949 // Launch over to configure widget, if needed
1950 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001951 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001952 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001953 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001955 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001956 Runnable onComplete = new Runnable() {
1957 @Override
1958 public void run() {
1959 // Exit spring loaded mode if necessary after adding the widget
1960 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1961 null);
1962 }
1963 };
Adam Cohendcd297f2013-06-18 13:13:40 -07001964 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001965 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001966 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
1968 }
Romain Guycbb89e42009-06-08 15:52:54 -07001969
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001970 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001971 // Close any folders that may be open.
1972 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001973 mWorkspace.moveToCustomContentScreen(animate);
1974 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001975 /**
1976 * Process a shortcut drop.
1977 *
1978 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001979 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001980 * @param cell The cell it should be added to, optional
1981 * @param position The location on the screen where it was dropped, optional
1982 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001983 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001984 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001985 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001986 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001988 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001989
1990 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 mPendingAddInfo.cellX = cell[0];
1992 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001993 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001994
1995 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1996 createShortcutIntent.setComponent(componentName);
1997 processShortcut(createShortcutIntent);
1998 }
1999
Adam Cohenfbba09b2011-07-18 21:43:05 -07002000 /**
2001 * Process a widget drop.
2002 *
2003 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002004 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002005 * @param cell The cell it should be added to, optional
2006 * @param position The location on the screen where it was dropped, optional
2007 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002008 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002009 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002010 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002011 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002012 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002013 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002014 mPendingAddInfo.minSpanX = info.minSpanX;
2015 mPendingAddInfo.minSpanY = info.minSpanY;
2016
Adam Cohenfbba09b2011-07-18 21:43:05 -07002017 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002018 mPendingAddInfo.cellX = cell[0];
2019 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002020 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002021 if (span != null) {
2022 mPendingAddInfo.spanX = span[0];
2023 mPendingAddInfo.spanY = span[1];
2024 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002025
Adam Cohened66b2b2012-01-23 17:28:51 -08002026 AppWidgetHostView hostView = info.boundWidget;
2027 int appWidgetId;
2028 if (hostView != null) {
2029 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002030 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002031 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002032 // In this case, we either need to start an activity to get permission to bind
2033 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002034 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002035 Bundle options = info.bindOptions;
2036
2037 boolean success = false;
2038 if (options != null) {
2039 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2040 info.componentName, options);
2041 } else {
2042 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2043 info.componentName);
2044 }
2045 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002046 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002047 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002048 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002049 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2050 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2051 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002052 // TODO: we need to make sure that this accounts for the options bundle.
2053 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002054 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2055 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002056 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002057 }
2058
Joe Onoratodeb98af2010-02-19 14:59:39 -08002059 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002060 // Handle case where user selected "Applications"
2061 String applicationName = getResources().getString(R.string.group_applications);
2062 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002063
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002064 if (applicationName != null && applicationName.equals(shortcutName)) {
2065 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2066 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002067
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002068 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2069 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002070 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002071 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002072 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002073 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076
Winson Chung24ab2f12010-09-16 14:10:47 -07002077 void processWallpaper(Intent intent) {
2078 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2079 }
2080
Adam Cohendcd297f2013-06-18 13:13:40 -07002081 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002082 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002083 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 folderInfo.title = getText(R.string.folder_name);
2085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002087 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002088 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002089 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090
2091 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002092 FolderIcon newFolder =
2093 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002094 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002095 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002096 // Force measure the new folder icon
2097 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2098 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002099 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 }
Romain Guycbb89e42009-06-08 15:52:54 -07002101
Joe Onorato9c1289c2009-08-17 11:03:03 -04002102 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002103 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002104 }
2105
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002106 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002108 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002109 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 }
2111
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002112 protected ComponentName getWallpaperPickerComponent() {
2113 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2114 }
2115
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002116 /**
2117 * Registers various content observers. The current implementation registers
2118 * only a favorites observer to keep track of the favorites applications.
2119 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002120 private void registerContentObservers() {
2121 ContentResolver resolver = getContentResolver();
2122 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2123 true, mWidgetObserver);
2124 }
2125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 @Override
2127 public boolean dispatchKeyEvent(KeyEvent event) {
2128 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2129 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002131 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002132 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002133 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002134 dumpState();
2135 return true;
2136 }
2137 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002138 }
2139 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2140 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002141 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 return true;
2143 }
2144 }
2145
2146 return super.dispatchKeyEvent(event);
2147 }
2148
Joe Onorato88ec0992009-11-19 13:16:06 -08002149 @Override
2150 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002151 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002152 if (mAppsCustomizeContent.getContentType() ==
2153 AppsCustomizePagedView.ContentType.Applications) {
2154 showWorkspace(true);
2155 } else {
2156 showOverviewMode(true);
2157 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002158 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002159 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002160 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002161 Folder openFolder = mWorkspace.getOpenFolder();
2162 if (openFolder.isEditingName()) {
2163 openFolder.dismissEditingName();
2164 } else {
2165 closeFolder();
2166 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002167 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002168 mWorkspace.exitWidgetResizeMode();
2169
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002170 // Back button is a no-op here, but give at least some feedback for the button press
2171 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002172 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002173 }
2174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002176 * Re-listen when widgets are reset.
2177 */
2178 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002179 if (mAppWidgetHost != null) {
2180 mAppWidgetHost.startListening();
2181 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002182 }
2183
2184 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 * Launches the intent referred by the clicked shortcut.
2186 *
2187 * @param v The view representing the clicked shortcut.
2188 */
2189 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002190 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2191 // view has detached (it's possible for this to happen if the view is removed mid touch).
2192 if (v.getWindowToken() == null) {
2193 return;
2194 }
2195
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002196 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002197 return;
2198 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002199
Adam Cohen1697b792013-09-17 19:08:21 -07002200 if (v instanceof Workspace) {
2201 if (mWorkspace.isInOverviewMode()) {
2202 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002203 }
2204 return;
2205 }
2206
2207 if (v instanceof CellLayout) {
2208 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002209 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002210 }
2211 }
2212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002214 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002216 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2217 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002218
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002219 // Check for special shortcuts
2220 if (intent.getComponent() != null) {
2221 final String shortcutClass = intent.getComponent().getClassName();
2222
2223 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002224 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002225 return;
2226 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2227 MemoryDumpActivity.startDump(this);
2228 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002229 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2230 toggleShowWeightWatcher();
2231 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002232 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002233 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002234
2235 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002236 int[] pos = new int[2];
2237 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002238 intent.setSourceBounds(new Rect(pos[0], pos[1],
2239 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002240
2241 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002242
Daniel Sandlerff02d492013-08-05 02:12:05 -04002243 mStats.recordLaunch(intent, shortcut);
2244
Michael Jurkaddd62e92011-02-16 17:49:14 -08002245 if (success && v instanceof BubbleTextView) {
2246 mWaitingForResume = (BubbleTextView) v;
2247 mWaitingForResume.setStayPressed(true);
2248 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002250 if (v instanceof FolderIcon) {
2251 FolderIcon fi = (FolderIcon) v;
2252 handleFolderClick(fi);
2253 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002254 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002255 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002256 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002257 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002258 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002259 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 }
2261 }
2262
Michael Jurka0e260592010-06-30 17:07:39 -07002263 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002264 return false;
2265 }
2266
Michael Jurkaaf442092010-06-10 17:01:57 -07002267 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002268 * Event handler for the search button
2269 *
2270 * @param v The view that was clicked.
2271 */
2272 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002273 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2274
Amith Yamasania135ba82011-08-09 17:42:01 -07002275 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002276 }
2277
2278 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002279 * Event handler for the voice button
2280 *
2281 * @param v The view that was clicked.
2282 */
2283 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002284 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002285
Bjorn Bringertc459e522013-06-07 19:36:01 +01002286 startVoice();
2287 }
2288
2289 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002290 try {
2291 final SearchManager searchManager =
2292 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2293 ComponentName activityName = searchManager.getGlobalSearchActivity();
2294 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002295 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002296 if (activityName != null) {
2297 intent.setPackage(activityName.getPackageName());
2298 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002299 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002300 } catch (ActivityNotFoundException e) {
2301 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002302 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002303 startActivitySafely(null, intent, "onClickVoiceButton");
2304 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002305 }
2306
2307 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002308 * Event handler for the "grid" button that appears on the home screen, which
2309 * enters all apps mode.
2310 *
2311 * @param v The view that was clicked.
2312 */
2313 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002314 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002315 }
2316
2317 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002318 // Provide the same haptic feedback that the system offers for virtual keys.
2319 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002320 }
2321
Adam Cohen61f560d2013-09-30 15:58:20 -07002322 public void performHapticFeedbackOnTouchDown(View v) {
2323 // Provide the same haptic feedback that the system offers for virtual keys.
2324 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2325 }
2326
2327 public View.OnTouchListener getHapticFeedbackTouchListener() {
2328 if (mHapticFeedbackTouchListener == null) {
2329 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2330 @Override
2331 public boolean onTouch(View v, MotionEvent event) {
2332 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2333 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2334 }
2335 return false;
2336 }
2337 };
2338 }
2339 return mHapticFeedbackTouchListener;
2340 }
2341
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002342 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002343 if (!DISABLE_MARKET_BUTTON) {
2344 if (mAppMarketIntent != null) {
2345 startActivitySafely(v, mAppMarketIntent, "app market");
2346 } else {
2347 Log.e(TAG, "Invalid app market intent.");
2348 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002349 }
2350 }
2351
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002352 /**
2353 * Called when the user stops interacting with the launcher.
2354 * This implies that the user is now on the homescreen and is not doing housekeeping.
2355 */
2356 protected void onInteractionEnd() {}
2357
2358 /**
2359 * Called when the user starts interacting with the launcher.
2360 * The possible interactions are:
2361 * - open all apps
2362 * - reorder an app shortcut, or a widget
2363 * - open the overview mode.
2364 * This is a good time to stop doing things that only make sense
2365 * when the user is on the homescreen and not doing housekeeping.
2366 */
2367 protected void onInteractionBegin() {}
2368
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002369 void startApplicationDetailsActivity(ComponentName componentName) {
2370 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002371 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2372 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002373 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002374 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002375 }
2376
Michael Jurka1e2f4652013-07-08 18:03:46 -07002377 // returns true if the activity was started
2378 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002379 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002380 // System applications cannot be installed. For now, show a toast explaining that.
2381 // We may give them the option of disabling apps this way.
2382 int messageId = R.string.uninstall_system_app_text;
2383 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002384 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002385 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002386 String packageName = componentName.getPackageName();
2387 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002388 Intent intent = new Intent(
2389 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002390 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2391 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002392 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002393 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002394 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002395 }
2396
Michael Jurka86a720e2012-05-09 11:23:23 -07002397 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002401 // Only launch using the new animation if the shortcut has not opted out (this is a
2402 // private contract between launcher and may be ignored in the future).
2403 boolean useLaunchAnimation = (v != null) &&
2404 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2405 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002406 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2407 v.getMeasuredWidth(), v.getMeasuredHeight());
2408
2409 startActivity(intent, opts.toBundle());
2410 } else {
2411 startActivity(intent);
2412 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002413 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 } catch (SecurityException e) {
2415 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002416 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002418 "or use the exported attribute for this activity. "
2419 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002421 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002423
Michael Jurka86a720e2012-05-09 11:23:23 -07002424 boolean startActivitySafely(View v, Intent intent, Object tag) {
2425 boolean success = false;
2426 try {
2427 success = startActivity(v, intent, tag);
2428 } catch (ActivityNotFoundException e) {
2429 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2430 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2431 }
2432 return success;
2433 }
2434
Adam Cohena9cf38f2011-05-02 15:36:58 -07002435 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002436 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002437 Folder openFolder = mWorkspace.getFolderForTag(info);
2438
2439 // If the folder info reports that the associated folder is open, then verify that
2440 // it is actually opened. There have been a few instances where this gets out of sync.
2441 if (info.opened && openFolder == null) {
2442 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002443 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002444 info.opened = false;
2445 }
2446
Adam Cohenfb91f302012-06-11 15:45:18 -07002447 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 // Close any open folder
2449 closeFolder();
2450 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002451 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 } else {
2453 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 int folderScreen;
2455 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002456 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 // .. and close it
2458 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002459 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 // Close any folder open on the current screen
2461 closeFolder();
2462 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002463 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 }
2465 }
2466 }
2467 }
2468
Adam Cohen268c4752012-06-06 17:47:33 -07002469 /**
2470 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2471 * in the DragLayer in the exact absolute location of the original FolderIcon.
2472 */
2473 private void copyFolderIconToImage(FolderIcon fi) {
2474 final int width = fi.getMeasuredWidth();
2475 final int height = fi.getMeasuredHeight();
2476
2477 // Lazy load ImageView, Bitmap and Canvas
2478 if (mFolderIconImageView == null) {
2479 mFolderIconImageView = new ImageView(this);
2480 }
2481 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2482 mFolderIconBitmap.getHeight() != height) {
2483 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2484 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2485 }
2486
2487 DragLayer.LayoutParams lp;
2488 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2489 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2490 } else {
2491 lp = new DragLayer.LayoutParams(width, height);
2492 }
2493
Adam Cohen307fe232012-08-16 17:55:58 -07002494 // The layout from which the folder is being opened may be scaled, adjust the starting
2495 // view size by this scale factor.
2496 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002497 lp.customPosition = true;
2498 lp.x = mRectForFolderAnimation.left;
2499 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002500 lp.width = (int) (scale * width);
2501 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002502
2503 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2504 fi.draw(mFolderIconCanvas);
2505 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002506 if (fi.getFolder() != null) {
2507 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2508 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002509 }
Adam Cohen268c4752012-06-06 17:47:33 -07002510 // Just in case this image view is still in the drag layer from a previous animation,
2511 // we remove it and re-add it.
2512 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2513 mDragLayer.removeView(mFolderIconImageView);
2514 }
2515 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002516 if (fi.getFolder() != null) {
2517 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002518 }
Adam Cohen268c4752012-06-06 17:47:33 -07002519 }
2520
Adam Cohen2801caf2011-05-13 20:57:39 -07002521 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002522 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002523 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2524 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2525 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2526
Adam Cohenc51934b2011-07-26 21:07:43 -07002527 FolderInfo info = (FolderInfo) fi.getTag();
2528 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2529 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002530 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2531 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002532 }
2533
Adam Cohen268c4752012-06-06 17:47:33 -07002534 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2535 copyFolderIconToImage(fi);
2536 fi.setVisibility(View.INVISIBLE);
2537
Michael Jurka2ecf9952012-06-18 12:52:28 -07002538 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002539 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002540 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2541 oa.start();
2542 }
2543
Adam Cohen268c4752012-06-06 17:47:33 -07002544 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002545 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002546 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2547 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2548 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2549
Adam Cohen268c4752012-06-06 17:47:33 -07002550 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002551
Adam Cohen268c4752012-06-06 17:47:33 -07002552 // We remove and re-draw the FolderIcon in-case it has changed
2553 mDragLayer.removeView(mFolderIconImageView);
2554 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002555 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002556 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002557 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002558 oa.addListener(new AnimatorListenerAdapter() {
2559 @Override
2560 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002561 if (cl != null) {
2562 cl.clearFolderLeaveBehind();
2563 // Remove the ImageView copy of the FolderIcon and make the original visible.
2564 mDragLayer.removeView(mFolderIconImageView);
2565 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002566 }
2567 }
2568 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002569 oa.start();
2570 }
2571
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002572 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002573 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 * is animated relative to the specified View. If the View is null, no animation
2575 * is played.
2576 *
2577 * @param folderInfo The FolderInfo describing the folder to open.
2578 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002579 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002580 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002581 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582
Adam Cohena9cf38f2011-05-02 15:36:58 -07002583 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584
Adam Cohen4554ee12011-08-03 16:13:21 -07002585 // Just verify that the folder hasn't already been added to the DragLayer.
2586 // There was a one-off crash where the folder had a parent already.
2587 if (folder.getParent() == null) {
2588 mDragLayer.addView(folder);
2589 mDragController.addDropTarget((DropTarget) folder);
2590 } else {
2591 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2592 folder.getParent() + ").");
2593 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002594 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002595 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002596
2597 // Notify the accessibility manager that this folder "window" has appeared and occluded
2598 // the workspace items
2599 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2600 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002601 }
2602
2603 public void closeFolder() {
2604 Folder folder = mWorkspace.getOpenFolder();
2605 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002606 if (folder.isEditingName()) {
2607 folder.dismissEditingName();
2608 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002609 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002610
2611 // Dismiss the folder cling
2612 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002613 }
2614 }
2615
2616 void closeFolder(Folder folder) {
2617 folder.getInfo().opened = false;
2618
2619 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2620 if (parent != null) {
2621 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2622 shrinkAndFadeInFolderIcon(fi);
2623 }
2624 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002625
2626 // Notify the accessibility manager that this folder "window" has disappeard and no
2627 // longer occludeds the workspace items
2628 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002629 }
2630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002632 if (!isDraggingEnabled()) return false;
2633 if (isWorkspaceLocked()) return false;
2634 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002635
Adam Cohen1697b792013-09-17 19:08:21 -07002636 if (v instanceof Workspace) {
2637 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002638 if (mWorkspace.enterOverviewMode()) {
2639 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2640 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2641 return true;
2642 } else {
2643 return false;
2644 }
Adam Cohen1697b792013-09-17 19:08:21 -07002645 }
Adam Cohen1697b792013-09-17 19:08:21 -07002646 }
2647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002648 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002649 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002650 }
2651
Michael Jurka0280c3b2010-09-17 15:00:07 -07002652 resetAddInfo();
2653 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002654 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002655 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 return true;
2657 }
2658
Winson Chung3d503fb2011-07-13 17:25:49 -07002659 // The hotseat touch handling does not go through Workspace, and we always allow long press
2660 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002661 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002662 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2663 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002664 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002665 // User long pressed on empty space
2666 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2667 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2668 // Disabling reordering until we sort out some issues.
2669 if (mWorkspace.isInOverviewMode()) {
2670 mWorkspace.startReordering(v);
2671 } else {
2672 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002674 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002675 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002677 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 }
2679 }
2680 }
2681 return true;
2682 }
2683
Winson Chung3d503fb2011-07-13 17:25:49 -07002684 boolean isHotseatLayout(View layout) {
2685 return mHotseat != null && layout != null &&
2686 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2687 }
2688 Hotseat getHotseat() {
2689 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002690 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002691 View getOverviewPanel() {
2692 return mOverviewPanel;
2693 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002694 SearchDropTargetBar getSearchBar() {
2695 return mSearchDropTargetBar;
2696 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002697
Winson Chung3d503fb2011-07-13 17:25:49 -07002698 /**
2699 * Returns the CellLayout of the specified container at the specified screen.
2700 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002701 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002702 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2703 if (mHotseat != null) {
2704 return mHotseat.getLayout();
2705 } else {
2706 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002707 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002708 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002709 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002710 }
2711 }
2712
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 Workspace getWorkspace() {
2714 return mWorkspace;
2715 }
2716
Daniel Sandler843e8602010-06-07 14:59:01 -04002717 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002718 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002719 }
2720
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002721 /**
2722 * Helper method for the cameraZoomIn/cameraZoomOut animations
2723 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002724 * @param scaleFactor The scale factor used for the zoom
2725 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002726 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002727 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002728 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002729 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002730
Craig Mautner360310b2012-10-26 15:13:08 -07002731 private void setWorkspaceBackground(boolean workspace) {
2732 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002733 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002734 }
2735
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002736 void updateWallpaperVisibility(boolean visible) {
2737 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2738 int curflags = getWindow().getAttributes().flags
2739 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2740 if (wpflags != curflags) {
2741 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2742 }
Craig Mautner360310b2012-10-26 15:13:08 -07002743 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002744 }
2745
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002746 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2747 if (v instanceof LauncherTransitionable) {
2748 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2749 }
2750 }
2751
Michael Jurkabed61d22012-02-14 22:51:29 -08002752 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2753 if (v instanceof LauncherTransitionable) {
2754 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2755 }
Winson Chung70442722012-02-10 15:43:22 -08002756
2757 // Update the workspace transition step as well
2758 dispatchOnLauncherTransitionStep(v, 0f);
2759 }
2760
2761 private void dispatchOnLauncherTransitionStep(View v, float t) {
2762 if (v instanceof LauncherTransitionable) {
2763 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2764 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002765 }
2766
2767 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2768 if (v instanceof LauncherTransitionable) {
2769 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2770 }
Winson Chung70442722012-02-10 15:43:22 -08002771
2772 // Update the workspace transition step as well
2773 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002774 }
2775
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002776 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002777 * Things to test when changing the following seven functions.
2778 * - Home from workspace
2779 * - from center screen
2780 * - from other screens
2781 * - Home from all apps
2782 * - from center screen
2783 * - from other screens
2784 * - Back from all apps
2785 * - from center screen
2786 * - from other screens
2787 * - Launch app from workspace and quit
2788 * - with back
2789 * - with home
2790 * - Launch app from all apps and quit
2791 * - with back
2792 * - with home
2793 * - Go to a screen that's not the default, then all
2794 * apps, and launch and app, and go back
2795 * - with back
2796 * -with home
2797 * - On workspace, long press power and go back
2798 * - with back
2799 * - with home
2800 * - On all apps, long press power and go back
2801 * - with back
2802 * - with home
2803 * - On workspace, power off
2804 * - On all apps, power off
2805 * - Launch an app and turn off the screen while in that app
2806 * - Go back with home key
2807 * - Go back with back key TODO: make this not go to workspace
2808 * - From all apps
2809 * - From workspace
2810 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2811 * - From all apps
2812 * - From the center workspace
2813 * - From another workspace
2814 */
2815
2816 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002817 * Zoom the camera out from the workspace to reveal 'toView'.
2818 * Assumes that the view to show is anchored at either the very top or very bottom
2819 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002820 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002821 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002822 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2823 showAppsCustomizeHelper(animated, springLoaded, contentType);
2824 }
2825 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2826 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002827 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002828 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002829 mStateAnimation.cancel();
2830 mStateAnimation = null;
2831 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002832 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002833
Winson Chungf0ea4d32011-06-06 14:27:16 -07002834 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2835 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2836 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002837 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002838 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002839 final int startDelay =
2840 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002841
Michael Jurkab3e22d92011-10-31 15:58:33 -07002842 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002843
Michael Jurkad74c9842011-07-10 12:44:21 -07002844 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002845 Animator workspaceAnim =
2846 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002847 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2848 // Set the content type for the all apps space
2849 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2850 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002851
2852 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002853 toView.setScaleX(scale);
2854 toView.setScaleY(scale);
2855 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2856 scaleAnim.
2857 scaleX(1f).scaleY(1f).
2858 setDuration(duration).
2859 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002860
Winson Chungf0ea4d32011-06-06 14:27:16 -07002861 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002862 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002863 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002864 .ofFloat(toView, "alpha", 0f, 1f)
2865 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002866 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002867 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2868 @Override
2869 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002870 if (animation == null) {
2871 throw new RuntimeException("animation is null");
2872 }
Winson Chung70442722012-02-10 15:43:22 -08002873 float t = (Float) animation.getAnimatedValue();
2874 dispatchOnLauncherTransitionStep(fromView, t);
2875 dispatchOnLauncherTransitionStep(toView, t);
2876 }
2877 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002878
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002879 // toView should appear right at the end of the workspace shrink
2880 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002881 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002882 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002883 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002884
2885 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002886 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002887 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002888 // Prepare the position
2889 toView.setTranslationX(0.0f);
2890 toView.setTranslationY(0.0f);
2891 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002892 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002893 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002894 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002895 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002896 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2897 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002898
Winson Chungc7d2b602012-05-16 17:02:20 -07002899 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002900 if (mSearchDropTargetBar != null) {
2901 mSearchDropTargetBar.hideSearchBar(false);
2902 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002903 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002904 });
2905
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002906 if (workspaceAnim != null) {
2907 mStateAnimation.play(workspaceAnim);
2908 }
Michael Jurka1899a362011-11-03 13:50:45 -07002909
2910 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002911
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002912 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2913 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002914
2915 // If any of the objects being animated haven't been measured/laid out
2916 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002917 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002918 (mWorkspace.getMeasuredWidth() == 0) ||
2919 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002920 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002921 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002922
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002923 final AnimatorSet stateAnimation = mStateAnimation;
2924 final Runnable startAnimRunnable = new Runnable() {
2925 public void run() {
2926 // Check that mStateAnimation hasn't changed while
2927 // we waited for a layout/draw pass
2928 if (mStateAnimation != stateAnimation)
2929 return;
2930 setPivotsForZoom(toView, scale);
2931 dispatchOnLauncherTransitionStart(fromView, animated, false);
2932 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002933 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002934 }
2935 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002936 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002937 final ViewTreeObserver observer = toView.getViewTreeObserver();
2938 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2939 public void onGlobalLayout() {
2940 startAnimRunnable.run();
2941 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2942 }
2943 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002944 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002945 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002946 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002947 } else {
2948 toView.setTranslationX(0.0f);
2949 toView.setTranslationY(0.0f);
2950 toView.setScaleX(1.0f);
2951 toView.setScaleY(1.0f);
2952 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002953 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002954
Daniel Sandlere4f98912013-06-25 15:13:26 -04002955 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002956 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002957 if (mSearchDropTargetBar != null) {
2958 mSearchDropTargetBar.hideSearchBar(false);
2959 }
Michael Jurkaabded662011-03-04 12:06:57 -08002960 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002961 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002962 dispatchOnLauncherTransitionStart(fromView, animated, false);
2963 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002964 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002965 dispatchOnLauncherTransitionStart(toView, animated, false);
2966 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002967 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002968 }
2969
2970 /**
2971 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002972 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002973 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002974 */
Adam Cohened307df2013-10-02 09:37:31 -07002975 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08002976 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002977
Michael Jurkab3e22d92011-10-31 15:58:33 -07002978 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002979 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002980 mStateAnimation.cancel();
2981 mStateAnimation = null;
2982 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002983 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002984
Winson Chungf0ea4d32011-06-06 14:27:16 -07002985 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002986 final int fadeOutDuration =
2987 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002988 final float scaleFactor = (float)
2989 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2990 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002991 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002992 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07002993 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002994 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2995 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002996 toState, animated, stagger, -1);
2997 } else if (toState == Workspace.State.SPRING_LOADED ||
2998 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002999 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003000 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003001 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003002
Michael Jurkab3e22d92011-10-31 15:58:33 -07003003 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003004 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003005 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003006 final LauncherViewPropertyAnimator scaleAnim =
3007 new LauncherViewPropertyAnimator(fromView);
3008 scaleAnim.
3009 scaleX(scaleFactor).scaleY(scaleFactor).
3010 setDuration(duration).
3011 setInterpolator(new Workspace.ZoomInInterpolator());
3012
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003013 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003014 .ofFloat(fromView, "alpha", 1f, 0f)
3015 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003016 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003017 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3018 @Override
3019 public void onAnimationUpdate(ValueAnimator animation) {
3020 float t = 1f - (Float) animation.getAnimatedValue();
3021 dispatchOnLauncherTransitionStep(fromView, t);
3022 dispatchOnLauncherTransitionStep(toView, t);
3023 }
3024 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003025
Michael Jurka2ecf9952012-06-18 12:52:28 -07003026 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003027
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003028 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3029 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003030 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003031
3032 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003033 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003034 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003035 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003036 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3037 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003038 if (onCompleteRunnable != null) {
3039 onCompleteRunnable.run();
3040 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003041 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003042 }
3043 });
3044
Winson Chungf0ea4d32011-06-06 14:27:16 -07003045 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003046 if (workspaceAnim != null) {
3047 mStateAnimation.play(workspaceAnim);
3048 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003049 dispatchOnLauncherTransitionStart(fromView, animated, true);
3050 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003051 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003052 } else {
3053 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003054 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003055 dispatchOnLauncherTransitionStart(fromView, animated, true);
3056 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003057 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003058 dispatchOnLauncherTransitionStart(toView, animated, true);
3059 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003060 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003061 }
3062
Michael Jurkae326f182011-11-21 14:05:46 -08003063 @Override
3064 public void onTrimMemory(int level) {
3065 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003066 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003067 mAppsCustomizeTabHost.onTrimMemory();
3068 }
3069 }
3070
Adam Cohened307df2013-10-02 09:37:31 -07003071 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003072 showWorkspace(animated, null);
3073 }
3074
Adam Cohened307df2013-10-02 09:37:31 -07003075 protected void showWorkspace() {
3076 showWorkspace(true);
3077 }
3078
Adam Cohened66b2b2012-01-23 17:28:51 -08003079 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003080 if (mWorkspace.isInOverviewMode()) {
3081 mWorkspace.exitOverviewMode(animated);
3082 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003083 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003084 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003085 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003086 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003087
Winson Chungc7d2b602012-05-16 17:02:20 -07003088 // Show the search bar (only animate if we were showing the drop target bar in spring
3089 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003090 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003091 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003092 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003093
Michael Jurkab3e22d92011-10-31 15:58:33 -07003094 // Set focus to the AppsCustomize button
3095 if (mAllAppsButton != null) {
3096 mAllAppsButton.requestFocus();
3097 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003098 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003099
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003100 // Change the state *after* we've called all the transition code
3101 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003102
Winson Chung5fb63472011-02-02 17:03:37 -08003103 // Resume the auto-advance of widgets
3104 mUserPresent = true;
3105 updateRunning();
3106
alanv1d4fde62012-10-17 13:15:47 -07003107 // Send an accessibility event to announce the context change
3108 getWindow().getDecorView()
3109 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003110
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003111 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003112 }
3113
Adam Cohened307df2013-10-02 09:37:31 -07003114 void showOverviewMode(boolean animated) {
3115 mWorkspace.setVisibility(View.VISIBLE);
3116 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3117 mState = State.WORKSPACE;
3118 onWorkspaceShown(animated);
3119 }
3120
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003121 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003122 }
3123
Winson Chung82963d52013-10-09 11:20:57 -07003124 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3125 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003126 if (mState != State.WORKSPACE) return;
3127
Winson Chung82963d52013-10-09 11:20:57 -07003128 if (resetPageToZero) {
3129 mAppsCustomizeTabHost.reset();
3130 }
Winson Chungc58497e2013-09-03 17:48:37 -07003131 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003132 mAppsCustomizeTabHost.requestFocus();
3133
Michael Jurkab3e22d92011-10-31 15:58:33 -07003134 // Change the state *after* we've called all the transition code
3135 mState = State.APPS_CUSTOMIZE;
3136
3137 // Pause the auto-advance of widgets until we are out of AllApps
3138 mUserPresent = false;
3139 updateRunning();
3140 closeFolder();
3141
3142 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003143 getWindow().getDecorView()
3144 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003145 }
3146
Adam Cohen7777d962011-08-18 18:58:38 -07003147 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003148 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003149 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003150 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003151 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003152 }
Adam Cohen7777d962011-08-18 18:58:38 -07003153
Adam Cohenad4e15c2013-10-17 16:21:35 -07003154 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003155 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003156 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3157
Winson Chunge7a03942011-08-05 15:05:12 -07003158 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003159 @Override
3160 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003161 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003162 // Before we show workspace, hide all apps again because
3163 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3164 // clean up our state transition functions
3165 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003166 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003167 } else {
3168 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003169 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003170 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003171 }, delay);
3172
Winson Chung557d6ed2011-07-08 15:34:52 -07003173 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003174
Michael Jurkad3ef3062010-11-23 16:23:58 -08003175 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003176 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003177 final boolean animated = true;
3178 final boolean springLoaded = true;
3179 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003180 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003181 }
3182 // Otherwise, we are not in spring loaded mode, so don't do anything.
3183 }
3184
Michael Jurkab3e22d92011-10-31 15:58:33 -07003185 void lockAllApps() {
3186 // TODO
3187 }
3188
3189 void unlockAllApps() {
3190 // TODO
3191 }
3192
Winson Chungf0ea4d32011-06-06 14:27:16 -07003193 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003194 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003195 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003196 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003197 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003198 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003199 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003200 int duration = 0;
3201 if (mSearchDropTargetBar != null) {
3202 duration = mSearchDropTargetBar.getTransitionInDuration();
3203 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003204 mHotseat.animate().alpha(1f).setDuration(duration);
3205 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003206 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003207 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003208 }
3209 }
3210 }
3211
3212 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003213 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003214 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003215 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003216 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003217 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003218 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003219 int duration = 0;
3220 if (mSearchDropTargetBar != null) {
3221 duration = mSearchDropTargetBar.getTransitionOutDuration();
3222 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003223 mHotseat.animate().alpha(0f).setDuration(duration);
3224 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003225 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003226 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003227 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003228 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003229 }
3230
Patrick Dubroy5f445422011-02-18 14:35:21 -08003231 /**
3232 * Add an item from all apps or customize onto the given workspace screen.
3233 * If layout is null, add to the current screen.
3234 */
3235 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003236 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003237 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003238 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003239 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003240
Winson Chungdff8ebb2011-09-08 17:25:31 -07003241 /** Maps the current orientation to an index for referencing orientation correct global icons */
3242 private int getCurrentOrientationIndexForGlobalIcons() {
3243 // default - 0, landscape - 1
3244 switch (getResources().getConfiguration().orientation) {
3245 case Configuration.ORIENTATION_LANDSCAPE:
3246 return 1;
3247 default:
3248 return 0;
3249 }
3250 }
3251
Michael Jurkaae65ee32012-04-30 11:45:54 -07003252 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003253 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003254 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003255 // Look for the toolbar icon specified in the activity meta-data
3256 Bundle metaData = packageManager.getActivityInfo(
3257 activityName, PackageManager.GET_META_DATA).metaData;
3258 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003259 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003260 if (iconResId != 0) {
3261 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003262 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003263 }
3264 }
3265 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003266 // This can happen if the activity defines an invalid drawable
3267 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3268 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003269 } catch (Resources.NotFoundException nfe) {
3270 // This can happen if the activity defines an invalid drawable
3271 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3272 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003273 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003274 return null;
3275 }
3276
3277 // if successful in getting icon, return it; otherwise, set button to use default drawable
3278 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003279 int buttonId, ComponentName activityName, int fallbackDrawableId,
3280 String toolbarResourceName) {
3281 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003282 Resources r = getResources();
3283 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3284 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003285
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003286 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003287 // If we were unable to find the icon via the meta-data, use a generic one
3288 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003289 toolbarIcon = r.getDrawable(fallbackDrawableId);
3290 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003291 if (button != null) {
3292 button.setCompoundDrawables(toolbarIcon, null, null, null);
3293 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003294 return null;
3295 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003296 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003297 if (button != null) {
3298 button.setCompoundDrawables(toolbarIcon, null, null, null);
3299 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003300 return toolbarIcon.getConstantState();
3301 }
3302 }
3303
3304 // if successful in getting icon, return it; otherwise, set button to use default drawable
3305 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003306 int buttonId, ComponentName activityName, int fallbackDrawableId,
3307 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003308 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003309 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003310
Michael Jurka19e0fc52011-07-22 18:00:21 -07003311 if (button != null) {
3312 // If we were unable to find the icon via the meta-data, use a
3313 // generic one
3314 if (toolbarIcon == null) {
3315 button.setImageResource(fallbackDrawableId);
3316 } else {
3317 button.setImageDrawable(toolbarIcon);
3318 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003319 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003320
3321 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3322
Michael Jurka0423dcf2010-10-05 14:56:18 -07003323 }
3324
Winson Chung1b884092012-06-08 17:13:02 -07003325 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003326 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003327 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003328 }
3329
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003330 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003331 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003332 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003333 }
3334
Winson Chungbb185bd2011-11-21 12:31:42 -08003335 private void invalidatePressedFocusedStates(View container, View button) {
3336 if (container instanceof HolographicLinearLayout) {
3337 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3338 layout.invalidatePressedFocusedStates();
3339 } else if (button instanceof HolographicImageView) {
3340 HolographicImageView view = (HolographicImageView) button;
3341 view.invalidatePressedFocusedStates();
3342 }
3343 }
3344
Cristina Stancu476493b2013-08-07 17:20:14 +01003345 public View getQsbBar() {
3346 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003347 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3348 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003349 }
3350 return mQsbBar;
3351 }
3352
3353 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003354 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003355 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003356 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003357 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003358
Winson Chung1cad91e2011-05-25 17:41:01 -07003359 final SearchManager searchManager =
3360 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3361 ComponentName activityName = searchManager.getGlobalSearchActivity();
3362 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003363 int coi = getCurrentOrientationIndexForGlobalIcons();
3364 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003365 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3366 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3367 if (sGlobalSearchIcon[coi] == null) {
3368 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3369 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3370 TOOLBAR_ICON_METADATA_NAME);
3371 }
3372
Winson Chungc51db6a2011-10-05 11:44:49 -07003373 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3374 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003375 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003376 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003377 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003378 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003379 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3380 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003381 if (searchButton != null) searchButton.setVisibility(View.GONE);
3382 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003383 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003384 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003385 }
3386 }
3387
Cristina Stancu476493b2013-08-07 17:20:14 +01003388 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003389 final View searchButtonContainer = findViewById(R.id.search_button_container);
3390 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003391 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003392 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003393 }
3394
Cristina Stancu476493b2013-08-07 17:20:14 +01003395 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003396 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003397 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003398
Winson Chungc51db6a2011-10-05 11:44:49 -07003399 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003400 final SearchManager searchManager =
3401 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3402 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3403
3404 ComponentName activityName = null;
3405 if (globalSearchActivity != null) {
3406 // Check if the global search activity handles voice search
3407 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3408 intent.setPackage(globalSearchActivity.getPackageName());
3409 activityName = intent.resolveActivity(getPackageManager());
3410 }
3411
3412 if (activityName == null) {
3413 // Fallback: check if an activity other than the global search activity
3414 // resolves this
3415 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3416 activityName = intent.resolveActivity(getPackageManager());
3417 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003418 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003419 int coi = getCurrentOrientationIndexForGlobalIcons();
3420 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003421 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3422 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3423 if (sVoiceSearchIcon[coi] == null) {
3424 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3425 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3426 TOOLBAR_ICON_METADATA_NAME);
3427 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003428 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003429 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003430 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003431 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003432 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003433 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003434 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003435 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003436 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003437 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003438 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003439 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003440
Cristina Stancu476493b2013-08-07 17:20:14 +01003441 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003442 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3443 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003444 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003445 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003446 }
3447
Winson Chung5841efa2013-09-30 18:06:44 -07003448 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003449 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3450 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003451 boolean visible = !forceDisableVoiceButtonProxy &&
3452 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003453 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003454 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003455 }
3456 }
Winson Chung5841efa2013-09-30 18:06:44 -07003457
3458 /**
3459 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3460 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3461 */
3462 public void disableVoiceButtonProxy(boolean disabled) {
3463 updateVoiceButtonProxyVisible(disabled);
3464 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003465 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003466 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003467 */
3468 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003469 if (!DISABLE_MARKET_BUTTON) {
3470 final View marketButton = findViewById(R.id.market_button);
3471 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3472 // Find the app market activity by resolving an intent.
3473 // (If multiple app markets are installed, it will return the ResolverActivity.)
3474 ComponentName activityName = intent.resolveActivity(getPackageManager());
3475 if (activityName != null) {
3476 int coi = getCurrentOrientationIndexForGlobalIcons();
3477 mAppMarketIntent = intent;
3478 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3479 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3480 TOOLBAR_ICON_METADATA_NAME);
3481 marketButton.setVisibility(View.VISIBLE);
3482 } else {
3483 // We should hide and disable the view so that we don't try and restore the visibility
3484 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3485 marketButton.setVisibility(View.GONE);
3486 marketButton.setEnabled(false);
3487 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003488 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003489 }
3490
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003491 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003492 if (!DISABLE_MARKET_BUTTON) {
3493 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3494 Resources r = getResources();
3495 Drawable marketIconDrawable = d.newDrawable(r);
3496 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3497 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3498 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003499
Winson Chungd64a6662013-09-30 11:06:59 -07003500 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3501 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003502 }
3503
Michael Jurkad7c28052012-04-27 15:43:36 -07003504 @Override
3505 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003506 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003507 final List<CharSequence> text = event.getText();
3508 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003509 // Populate event with a fake title based on the current state.
3510 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003511 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003512 } else {
3513 text.add(getString(R.string.all_apps_home_button_label));
3514 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003515 return result;
3516 }
3517
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003518 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003519 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003520 */
3521 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3522 @Override
3523 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003524 closeSystemDialogs();
3525 }
3526 }
3527
3528 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003529 * Receives notifications whenever the appwidgets are reset.
3530 */
3531 private class AppWidgetResetObserver extends ContentObserver {
3532 public AppWidgetResetObserver() {
3533 super(new Handler());
3534 }
3535
3536 @Override
3537 public void onChange(boolean selfChange) {
3538 onAppWidgetReset();
3539 }
3540 }
3541
3542 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003543 * If the activity is currently paused, signal that we need to run the passed Runnable
3544 * in onResume.
3545 *
3546 * This needs to be called from incoming places where resources might have been loaded
3547 * while we are paused. That is becaues the Configuration might be wrong
3548 * when we're not running, and if it comes back to what it was when we
3549 * were paused, we are not restarted.
3550 *
3551 * Implementation of the method from LauncherModel.Callbacks.
3552 *
3553 * @return true if we are currently paused. The caller might be able to
3554 * skip some work in that case since we will come back again.
3555 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003556 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003557 if (mPaused) {
3558 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003559 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003560 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003561 }
3562 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003563 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003564 return true;
3565 } else {
3566 return false;
3567 }
3568 }
3569
Michael Jurkac402cd92013-05-20 15:49:32 +02003570 private boolean waitUntilResume(Runnable run) {
3571 return waitUntilResume(run, false);
3572 }
3573
Michael Jurka1e2f4652013-07-08 18:03:46 -07003574 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003575 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003576 }
3577
Michael Jurka7607c2f2013-04-03 14:33:19 -07003578 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003579 * If the activity is currently paused, signal that we need to re-run the loader
3580 * in onResume.
3581 *
3582 * This needs to be called from incoming places where resources might have been loaded
3583 * while we are paused. That is becaues the Configuration might be wrong
3584 * when we're not running, and if it comes back to what it was when we
3585 * were paused, we are not restarted.
3586 *
3587 * Implementation of the method from LauncherModel.Callbacks.
3588 *
3589 * @return true if we are currently paused. The caller might be able to
3590 * skip some work in that case since we will come back again.
3591 */
3592 public boolean setLoadOnResume() {
3593 if (mPaused) {
3594 Log.i(TAG, "setLoadOnResume");
3595 mOnResumeNeedsLoad = true;
3596 return true;
3597 } else {
3598 return false;
3599 }
3600 }
3601
3602 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003604 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003606 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003607 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003608 } else {
3609 return SCREEN_COUNT / 2;
3610 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003612
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 /**
3614 * Refreshes the shortcuts shown on the workspace.
3615 *
3616 * Implementation of the method from LauncherModel.Callbacks.
3617 */
3618 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003619 // If we're starting binding all over again, clear any bind calls we'd postponed in
3620 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3621 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003622 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003623
Winson Chungd64d1762013-08-20 14:37:16 -07003624 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003625 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003626 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003627
Michael Jurka05bf644e2011-11-30 20:28:53 -08003628 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003629 if (mHotseat != null) {
3630 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003631 }
3632 }
3633
Adam Cohendcd297f2013-06-18 13:13:40 -07003634 @Override
3635 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003636 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003637
Adam Cohen5084cba2013-09-03 12:01:16 -07003638 // If there are no screens, we need to have an empty screen
3639 if (orderedScreenIds.size() == 0) {
3640 mWorkspace.addExtraEmptyScreen();
3641 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003642
3643 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003644 // setCurrentPage() so ensure that all pages are added before calling this).
3645 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003646 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3647 mWorkspace.createCustomContentPage();
3648 }
Winson Chung64359a52013-07-08 17:17:08 -07003649 }
3650
3651 @Override
3652 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003653 int count = orderedScreenIds.size();
3654 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003655 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003656 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003657 }
3658
Adam Cohen39a06042013-07-19 14:30:12 -07003659 private boolean shouldShowWeightWatcher() {
3660 String spKey = LauncherAppState.getSharedPreferencesKey();
3661 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003662 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003663
3664 return show;
3665 }
3666
3667 private void toggleShowWeightWatcher() {
3668 String spKey = LauncherAppState.getSharedPreferencesKey();
3669 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3670 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3671
3672 show = !show;
3673
3674 SharedPreferences.Editor editor = sp.edit();
3675 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3676 editor.commit();
3677
3678 if (mWeightWatcher != null) {
3679 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3680 }
3681 }
3682
Winson Chungd64d1762013-08-20 14:37:16 -07003683 public void bindAppsAdded(final ArrayList<Long> newScreens,
3684 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003685 final ArrayList<ItemInfo> addAnimated,
3686 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003687 Runnable r = new Runnable() {
3688 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003689 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003690 }
3691 };
3692 if (waitUntilResume(r)) {
3693 return;
3694 }
3695
Winson Chungd64d1762013-08-20 14:37:16 -07003696 // Add the new screens
3697 bindAddScreens(newScreens);
3698
3699 // We add the items without animation on non-visible pages, and with
3700 // animations on the new page (which we will try and snap to).
3701 if (!addNotAnimated.isEmpty()) {
3702 bindItems(addNotAnimated, 0,
3703 addNotAnimated.size(), false);
3704 }
3705 if (!addAnimated.isEmpty()) {
3706 bindItems(addAnimated, 0,
3707 addAnimated.size(), true);
3708 }
Winson Chungc58497e2013-09-03 17:48:37 -07003709
Winson Chung87412982013-10-03 18:34:14 -07003710 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003711 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003712
Winson Chungc58497e2013-09-03 17:48:37 -07003713 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3714 addedApps != null && mAppsCustomizeContent != null) {
3715 mAppsCustomizeContent.addApps(addedApps);
3716 }
Winson Chungd64d1762013-08-20 14:37:16 -07003717 }
3718
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003719 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 * Bind the items start-end from the list.
3721 *
3722 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003723 */
Winson Chung64359a52013-07-08 17:17:08 -07003724 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3725 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003726 Runnable r = new Runnable() {
3727 public void run() {
3728 bindItems(shortcuts, start, end, forceAnimateIcons);
3729 }
3730 };
3731 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003732 return;
3733 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003734
Winson Chungf0c6ae02012-03-21 16:10:31 -07003735 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003736 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3737 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003738 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003739 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003740 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003741 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003742 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003743
3744 // Short circuit if we are loading dock items for a configuration which has no dock
3745 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3746 mHotseat == null) {
3747 continue;
3748 }
3749
Joe Onorato9c1289c2009-08-17 11:03:03 -04003750 switch (item.itemType) {
3751 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3752 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003753 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003754 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003755
Winson Chung64359a52013-07-08 17:17:08 -07003756 /*
3757 * TODO: FIX collision case
3758 */
Winson Chungce376632013-07-11 16:12:41 -07003759 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3760 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3761 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3762 throw new RuntimeException("OCCUPIED");
3763 }
Winson Chung64359a52013-07-08 17:17:08 -07003764 }
3765
Adam Cohendcd297f2013-06-18 13:13:40 -07003766 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3767 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003768 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003769 // Animate all the applications up now
3770 shortcut.setAlpha(0f);
3771 shortcut.setScaleX(0f);
3772 shortcut.setScaleY(0f);
3773 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003774 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003775 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003776 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003777 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003778 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003779 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003780 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003781 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3782 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003783 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003784 default:
3785 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003786 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003787 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003788
Winson Chung997a9232013-07-24 15:33:46 -07003789 if (animateIcons) {
3790 // Animate to the correct page
3791 if (newShortcutsScreenId > -1) {
3792 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003793 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003794 final Runnable startBounceAnimRunnable = new Runnable() {
3795 public void run() {
3796 anim.playTogether(bounceAnims);
3797 anim.start();
3798 }
3799 };
Winson Chung997a9232013-07-24 15:33:46 -07003800 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003801 // We post the animation slightly delayed to prevent slowdowns
3802 // when we are loading right after we return to launcher.
3803 mWorkspace.postDelayed(new Runnable() {
3804 public void run() {
3805 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003806 mWorkspace.postDelayed(startBounceAnimRunnable,
3807 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003808 }
3809 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003810 } else {
3811 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003812 }
3813 }
Winson Chung64359a52013-07-08 17:17:08 -07003814 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003816 }
3817
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818 /**
3819 * Implementation of the method from LauncherModel.Callbacks.
3820 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003821 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003822 Runnable r = new Runnable() {
3823 public void run() {
3824 bindFolders(folders);
3825 }
3826 };
3827 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003828 return;
3829 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003830 sFolders.clear();
3831 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003832 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003833
3834 /**
3835 * Add the views for a widget to the workspace.
3836 *
3837 * Implementation of the method from LauncherModel.Callbacks.
3838 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003839 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003840 Runnable r = new Runnable() {
3841 public void run() {
3842 bindAppWidget(item);
3843 }
3844 };
3845 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003846 return;
3847 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003848
Daniel Sandler843e8602010-06-07 14:59:01 -04003849 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3850 if (DEBUG_WIDGETS) {
3851 Log.d(TAG, "bindAppWidget: " + item);
3852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003853 final Workspace workspace = mWorkspace;
3854
3855 final int appWidgetId = item.appWidgetId;
3856 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003857 if (DEBUG_WIDGETS) {
3858 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3859 }
3860
Joe Onorato9c1289c2009-08-17 11:03:03 -04003861 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3862
Joe Onorato9c1289c2009-08-17 11:03:03 -04003863 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003864 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003865
Adam Cohendcd297f2013-06-18 13:13:40 -07003866 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003867 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003868 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3869
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870 workspace.requestLayout();
3871
Daniel Sandler843e8602010-06-07 14:59:01 -04003872 if (DEBUG_WIDGETS) {
3873 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3874 + (SystemClock.uptimeMillis()-start) + "ms");
3875 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003876 }
3877
Adam Cohen1462de32012-07-24 22:34:36 -07003878 public void onPageBoundSynchronously(int page) {
3879 mSynchronouslyBoundPages.add(page);
3880 }
3881
Joe Onorato9c1289c2009-08-17 11:03:03 -04003882 /**
3883 * Callback saying that there aren't any more items to bind.
3884 *
3885 * Implementation of the method from LauncherModel.Callbacks.
3886 */
Adam Cohene25af792013-06-06 23:08:25 -07003887 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003888 Runnable r = new Runnable() {
3889 public void run() {
3890 finishBindingItems(upgradePath);
3891 }
3892 };
3893 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003894 return;
3895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896 if (mSavedState != null) {
3897 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003898 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900 mSavedState = null;
3901 }
3902
Adam Cohen1462de32012-07-24 22:34:36 -07003903 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003904
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003905 // If we received the result of any pending adds while the loader was running (e.g. the
3906 // widget configuration forced an orientation change), process them now.
3907 for (int i = 0; i < sPendingAddList.size(); i++) {
3908 completeAdd(sPendingAddList.get(i));
3909 }
3910 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003911
Winson Chungc51db6a2011-10-05 11:44:49 -07003912 // Update the market app icon as necessary (the other icons will be managed in response to
3913 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003914 if (!DISABLE_MARKET_BUTTON) {
3915 updateAppMarketIcon();
3916 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003917
Winson Chungf0c6ae02012-03-21 16:10:31 -07003918 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003919 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003920 mWorkspace.getUniqueComponents(true, null);
3921 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003922 }
Adam Cohen99894d92013-06-14 11:22:59 -07003923
Adam Cohen66a01fd2013-06-11 12:48:00 -07003924 mWorkspace.post(new Runnable() {
3925 @Override
3926 public void run() {
3927 onFinishBindingItems();
3928 }
3929 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003930 }
3931
Winson Chunga0b7e862013-09-05 16:03:15 -07003932 public boolean isAllAppsButtonRank(int rank) {
3933 if (mHotseat != null) {
3934 return mHotseat.isAllAppsButtonRank(rank);
3935 }
3936 return false;
3937 }
3938
Winson Chunga2413752012-04-03 14:22:34 -07003939 private boolean canRunNewAppsAnimation() {
3940 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3941 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3942 }
3943
Winson Chungc9168342013-06-26 14:54:55 -07003944 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3945 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3946 PropertyValuesHolder.ofFloat("alpha", 1f),
3947 PropertyValuesHolder.ofFloat("scaleX", 1f),
3948 PropertyValuesHolder.ofFloat("scaleY", 1f));
3949 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3950 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3951 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3952 return bounceAnim;
3953 }
3954
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003955 @Override
3956 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003957 boolean searchVisible = updateGlobalSearchIcon();
3958 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003959 if (mSearchDropTargetBar != null) {
3960 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3961 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003962 }
3963
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003964 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 * Add the icons for all apps.
3966 *
3967 * Implementation of the method from LauncherModel.Callbacks.
3968 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003969 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003970 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3971 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3972 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3973 getHotseat().addAllAppsFolder(mIconCache, apps,
3974 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3975 }
3976 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003977 }
Winson Chungc58497e2013-09-03 17:48:37 -07003978 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07003979 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07003980 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07003981 mAppsCustomizeContent.onPackagesUpdated(
3982 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07003983 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003985 }
3986
3987 /**
3988 * A package was updated.
3989 *
3990 * Implementation of the method from LauncherModel.Callbacks.
3991 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003992 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003993 Runnable r = new Runnable() {
3994 public void run() {
3995 bindAppsUpdated(apps);
3996 }
3997 };
3998 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003999 return;
4000 }
4001
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004002 if (mWorkspace != null) {
4003 mWorkspace.updateShortcuts(apps);
4004 }
Winson Chungc58497e2013-09-03 17:48:37 -07004005
4006 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4007 mAppsCustomizeContent != null) {
4008 mAppsCustomizeContent.updateApps(apps);
4009 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004010 }
4011
4012 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004013 * A package was uninstalled. We take both the super set of packageNames
4014 * in addition to specific applications to remove, the reason being that
4015 * this can be called when a package is updated as well. In that scenario,
4016 * we only remove specific components from the workspace, where as
4017 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004018 *
4019 * Implementation of the method from LauncherModel.Callbacks.
4020 */
Winson Chung83892cc2013-05-01 16:53:33 -07004021 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004022 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004023 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004024 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004025 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004026 }
Winson Chungd64d1762013-08-20 14:37:16 -07004027 };
4028 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004029 return;
4030 }
4031
Winson Chungdf95eb12013-10-16 14:57:07 -07004032 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004033 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004034 }
4035 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004036 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004037 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004038
Winson Chunga1820962011-10-03 16:31:06 -07004039 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004040 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004041
Winson Chungdf95eb12013-10-16 14:57:07 -07004042 // Update AllApps
Winson Chungc58497e2013-09-03 17:48:37 -07004043 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4044 mAppsCustomizeContent != null) {
4045 mAppsCustomizeContent.removeApps(appInfos);
4046 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004047 }
Joe Onoratobe386092009-11-17 17:32:16 -08004048
4049 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004050 * A number of packages were updated.
4051 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004052 private ArrayList<Object> mWidgetsAndShortcuts;
4053 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004054 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004055 bindPackagesUpdated(mWidgetsAndShortcuts);
4056 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004057 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004058 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004059 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4060 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4061 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004062 return;
4063 }
4064
Winson Chung64359a52013-07-08 17:17:08 -07004065 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004066 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4067 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004068 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004069 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004070 }
4071
Winson Chung400438b2011-01-16 17:53:48 -08004072 private int mapConfigurationOriActivityInfoOri(int configOri) {
4073 final Display d = getWindowManager().getDefaultDisplay();
4074 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4075 switch (d.getRotation()) {
4076 case Surface.ROTATION_0:
4077 case Surface.ROTATION_180:
4078 // We are currently in the same basic orientation as the natural orientation
4079 naturalOri = configOri;
4080 break;
4081 case Surface.ROTATION_90:
4082 case Surface.ROTATION_270:
4083 // We are currently in the other basic orientation to the natural orientation
4084 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4085 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4086 break;
4087 }
4088
4089 int[] oriMap = {
4090 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4091 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4092 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4093 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4094 };
4095 // Since the map starts at portrait, we need to offset if this device's natural orientation
4096 // is landscape.
4097 int indexOffset = 0;
4098 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4099 indexOffset = 1;
4100 }
4101 return oriMap[(d.getRotation() + indexOffset) % 4];
4102 }
Adam Cohen446e9402011-09-15 18:21:21 -07004103
Winson Chung4b919f82012-05-01 10:44:08 -07004104 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004105 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004106 getResources().getBoolean(R.bool.allow_rotation);
4107 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004108 }
Winson Chung4b919f82012-05-01 10:44:08 -07004109 public void lockScreenOrientation() {
4110 if (isRotationEnabled()) {
4111 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4112 .getConfiguration().orientation));
4113 }
4114 }
4115 public void unlockScreenOrientation(boolean immediate) {
4116 if (isRotationEnabled()) {
4117 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004118 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004119 } else {
4120 mHandler.postDelayed(new Runnable() {
4121 public void run() {
4122 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4123 }
4124 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004125 }
Winson Chung4b919f82012-05-01 10:44:08 -07004126 }
Winson Chung400438b2011-01-16 17:53:48 -08004127 }
4128
Winson Chung82f55532011-08-09 14:14:23 -07004129 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004130 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004131 if (DISABLE_CLINGS) {
4132 return false;
4133 }
4134
Winson Chung3a6e7f32013-10-09 15:50:52 -07004135 // For now, limit only to phones
4136 LauncherAppState app = LauncherAppState.getInstance();
4137 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4138 if (grid.isTablet()) {
4139 return false;
4140 }
Winson Chung13dbfe12013-10-31 11:42:39 -07004141 if (grid.isLandscape) {
4142 return false;
4143 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004144
Brett Chabot2a9e2282011-08-23 15:03:13 -07004145 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004146 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004147
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004148 // Disable clings for accessibility when explore by touch is enabled
4149 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4150 ACCESSIBILITY_SERVICE);
4151 if (a11yManager.isTouchExplorationEnabled()) {
4152 return false;
4153 }
4154
Michael Jurkae233a8b2013-03-19 13:49:20 +01004155 // Restricted secondary users (child mode) will potentially have very few apps
4156 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004157// boolean supportsLimitedUsers =
4158// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4159// Account[] accounts = AccountManager.get(this).getAccounts();
4160// if (supportsLimitedUsers && accounts.length == 0) {
4161// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4162// Bundle restrictions = um.getUserRestrictions();
4163// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4164// return false;
4165// }
4166// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004167 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004168 }
Michael Jurka22143132012-10-04 17:42:38 +02004169
Winson Chungfa545132013-09-26 17:45:32 -07004170 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004171 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004172 Cling cling = (Cling) findViewById(clingId);
4173 View scrim = null;
4174 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004175 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004176 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004177 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004178 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004179 cling.show(animate, SHOW_CLING_DURATION);
4180
4181 if (dimNavBarVisibilty) {
4182 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4183 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004184 }
4185 }
4186 return cling;
4187 }
Michael Jurka22143132012-10-04 17:42:38 +02004188
Winson Chungaf40f202013-09-18 18:26:31 -07004189 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4190 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004191 // To catch cases where siblings of top-level views are made invisible, just check whether
4192 // the cling is directly set to GONE before dismissing it.
4193 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004194 final Runnable cleanUpClingCb = new Runnable() {
4195 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004196 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004197 // We should update the shared preferences on a background thread
4198 new Thread("dismissClingThread") {
4199 public void run() {
4200 SharedPreferences.Editor editor = mSharedPrefs.edit();
4201 editor.putBoolean(flag, true);
4202 editor.commit();
4203 }
4204 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004205 if (postAnimationCb != null) {
4206 postAnimationCb.run();
4207 }
4208 }
4209 };
4210 if (duration <= 0) {
4211 cleanUpClingCb.run();
4212 } else {
4213 cling.hide(duration, cleanUpClingCb);
4214 }
Michael Jurka22143132012-10-04 17:42:38 +02004215 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004216
4217 if (restoreNavBarVisibilty) {
4218 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4219 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4220 }
Winson Chung82f55532011-08-09 14:14:23 -07004221 }
4222 }
Michael Jurka22143132012-10-04 17:42:38 +02004223
Winson Chung9d9d74f2011-09-19 11:49:12 -07004224 private void removeCling(int id) {
4225 final View cling = findViewById(id);
4226 if (cling != null) {
4227 final ViewGroup parent = (ViewGroup) cling.getParent();
4228 parent.post(new Runnable() {
4229 @Override
4230 public void run() {
4231 parent.removeView(cling);
4232 }
4233 });
Michael Jurka22143132012-10-04 17:42:38 +02004234 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004235 }
4236 }
Michael Jurka974c3862012-05-22 22:00:31 -07004237
4238 private boolean skipCustomClingIfNoAccounts() {
4239 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4240 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4241 if (customCling) {
4242 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004243 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004244 Account[] accounts = am.getAccountsByType("com.google");
4245 return accounts.length == 0;
4246 }
4247 return false;
4248 }
4249
Winson Chung82e5c982013-10-09 12:04:50 -07004250 public void updateCustomContentHintVisibility() {
4251 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4252 String ccHintStr = getFirstRunCustomContentHint();
4253
4254 if (mWorkspace.hasCustomContent()) {
4255 // Show the custom content hint if ccHintStr is not empty
4256 if (cling != null) {
4257 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4258 }
4259 } else {
4260 // Hide the custom content hint
4261 if (cling != null) {
4262 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4263 }
4264 }
4265 }
4266
4267 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4268 boolean animate) {
4269 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4270 if (ccHint != null) {
4271 if (visible && !ccHintStr.isEmpty()) {
4272 ccHint.setText(ccHintStr);
4273 ccHint.setVisibility(View.VISIBLE);
4274 if (animate) {
4275 ccHint.setAlpha(0f);
4276 ccHint.animate().alpha(1f)
4277 .setDuration(SHOW_CLING_DURATION)
4278 .start();
4279 } else {
4280 ccHint.setAlpha(1f);
4281 }
4282 } else {
4283 if (animate) {
4284 ccHint.animate().alpha(0f)
4285 .setDuration(SHOW_CLING_DURATION)
4286 .setListener(new AnimatorListenerAdapter() {
4287 @Override
4288 public void onAnimationEnd(Animator animation) {
4289 ccHint.setVisibility(View.GONE);
4290 }
4291 })
4292 .start();
4293 } else {
4294 ccHint.setAlpha(0f);
4295 ccHint.setVisibility(View.GONE);
4296 }
4297 }
4298 }
4299 }
4300
Winson Chungaf40f202013-09-18 18:26:31 -07004301 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004302 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004303 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004304 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004305 // If we're not using the default workspace layout, replace workspace cling
4306 // with a custom workspace cling (usually specified in an overlay)
4307 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004308 if (!DISABLE_CUSTOM_CLINGS) {
4309 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4310 getResources().getBoolean(R.bool.config_useCustomClings)) {
4311 // Use a custom cling
4312 View cling = findViewById(R.id.workspace_cling);
4313 ViewGroup clingParent = (ViewGroup) cling.getParent();
4314 int clingIndex = clingParent.indexOfChild(cling);
4315 clingParent.removeViewAt(clingIndex);
4316 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4317 clingParent.addView(customCling, clingIndex);
4318 customCling.setId(R.id.workspace_cling);
4319 }
4320 }
4321 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4322 if (cling != null) {
4323 String sbHintStr = getFirstRunClingSearchBarHint();
4324 String ccHintStr = getFirstRunCustomContentHint();
4325 if (!sbHintStr.isEmpty()) {
4326 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4327 sbHint.setText(sbHintStr);
4328 sbHint.setVisibility(View.VISIBLE);
4329 }
Winson Chung82e5c982013-10-09 12:04:50 -07004330 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004331 }
Winson Chungfa545132013-09-26 17:45:32 -07004332 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004333 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004334 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004335 }
4336 }
Winson Chungaf40f202013-09-18 18:26:31 -07004337
Winson Chunge6eabff2013-09-24 11:01:23 -07004338 protected String getFirstRunClingSearchBarHint() {
4339 return "";
4340 }
4341 protected String getFirstRunCustomContentHint() {
4342 return "";
4343 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004344 protected int getFirstRunFocusedHotseatAppDrawableId() {
4345 return -1;
4346 }
4347 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4348 return null;
4349 }
4350 protected int getFirstRunFocusedHotseatAppRank() {
4351 return -1;
4352 }
4353 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4354 return "";
4355 }
4356 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4357 return "";
4358 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004359
Winson Chungaf40f202013-09-18 18:26:31 -07004360 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004361 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004362 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004363 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004364 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4365
4366 // Set the focused hotseat app if there is one
4367 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4368 getFirstRunFocusedHotseatAppRank(),
4369 getFirstRunFocusedHotseatAppComponentName(),
4370 getFirstRunFocusedHotseatAppBubbleTitle(),
4371 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004372 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004373 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004374 }
4375 }
4376 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004377 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004378 if (isClingsEnabled() &&
4379 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004380 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4381 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004382 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004383 } else {
4384 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004385 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004386 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004387 }
Michael Jurka104c4562013-07-08 18:03:46 -07004388 protected SharedPreferences getSharedPrefs() {
4389 return mSharedPrefs;
4390 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004391 public boolean isFolderClingVisible() {
4392 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004393 if (cling != null) {
4394 return cling.getVisibility() == View.VISIBLE;
4395 }
4396 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004397 }
Winson Chungaf40f202013-09-18 18:26:31 -07004398 public void dismissFirstRunCling(View v) {
4399 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4400 Runnable cb = new Runnable() {
4401 public void run() {
4402 // Show the workspace cling next
4403 showFirstRunWorkspaceCling();
4404 }
4405 };
4406 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4407 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004408
4409 // Fade out the search bar for the workspace cling coming up
4410 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004411 }
Winson Chung82f55532011-08-09 14:14:23 -07004412 public void dismissWorkspaceCling(View v) {
4413 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004414 Runnable cb = null;
4415 if (v == null) {
4416 cb = new Runnable() {
4417 public void run() {
4418 mWorkspace.enterOverviewMode();
4419 }
4420 };
4421 }
4422 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4423 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004424
4425 // Fade in the search bar
4426 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004427 }
4428 public void dismissFolderCling(View v) {
4429 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004430 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4431 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004432 }
4433
Winson Chung80baf5a2010-08-09 16:03:15 -07004434 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004435 * Prints out out state for debugging.
4436 */
4437 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004438 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004439 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004440 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4441 Log.d(TAG, "mRestoring=" + mRestoring);
4442 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4443 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004444 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004445 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004446
Winson Chung785d2eb2011-04-14 16:08:02 -07004447 if (mAppsCustomizeContent != null) {
4448 mAppsCustomizeContent.dumpState();
4449 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004450 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004451 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004452
4453 @Override
4454 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4455 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004456 synchronized (sDumpLogs) {
4457 writer.println(" ");
4458 writer.println("Debug logs: ");
4459 for (int i = 0; i < sDumpLogs.size(); i++) {
4460 writer.println(" " + sDumpLogs.get(i));
4461 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004462 }
4463 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004464
4465 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004466 if (DEBUG_DUMP_LOG) {
4467 synchronized (sDumpLogs) {
4468 Log.d(TAG, "");
4469 Log.d(TAG, "*********************");
4470 Log.d(TAG, "Launcher debug logs: ");
4471 for (int i = 0; i < sDumpLogs.size(); i++) {
4472 Log.d(TAG, " " + sDumpLogs.get(i));
4473 }
4474 Log.d(TAG, "*********************");
4475 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004476 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004477 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004478 }
4479
4480 public static void addDumpLog(String tag, String log, boolean debugLog) {
4481 if (debugLog) {
4482 Log.d(tag, log);
4483 }
Winson Chungede41292013-09-19 16:27:36 -07004484 if (DEBUG_DUMP_LOG) {
4485 sDateStamp.setTime(System.currentTimeMillis());
4486 synchronized (sDumpLogs) {
4487 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004488 }
Winson Chungede41292013-09-19 16:27:36 -07004489 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004490 }
4491
Winson Chungede41292013-09-19 16:27:36 -07004492 public void dumpLogsToLocalData() {
4493 if (DEBUG_DUMP_LOG) {
4494 new Thread("DumpLogsToLocalData") {
4495 @Override
4496 public void run() {
4497 boolean success = false;
4498 sDateStamp.setTime(sRunStart);
4499 String FILENAME = sDateStamp.getMonth() + "-"
4500 + sDateStamp.getDay() + "_"
4501 + sDateStamp.getHours() + "-"
4502 + sDateStamp.getMinutes() + "_"
4503 + sDateStamp.getSeconds() + ".txt";
4504
4505 FileOutputStream fos = null;
4506 File outFile = null;
4507 try {
4508 outFile = new File(getFilesDir(), FILENAME);
4509 outFile.createNewFile();
4510 fos = new FileOutputStream(outFile);
4511 } catch (Exception e) {
4512 e.printStackTrace();
4513 }
4514 if (fos != null) {
4515 PrintWriter writer = new PrintWriter(fos);
4516
4517 writer.println(" ");
4518 writer.println("Debug logs: ");
4519 synchronized (sDumpLogs) {
4520 for (int i = 0; i < sDumpLogs.size(); i++) {
4521 writer.println(" " + sDumpLogs.get(i));
4522 }
4523 }
4524 writer.close();
4525 }
4526 try {
4527 if (fos != null) {
4528 fos.close();
4529 success = true;
4530 }
4531 } catch (IOException e) {
4532 e.printStackTrace();
4533 }
4534 }
4535 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004536 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004538}
Michael Jurka2763be32011-02-24 11:19:57 -08004539
Michael Jurkaabded662011-03-04 12:06:57 -08004540interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004541 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004542 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004543 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004544 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004545 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004546}