blob: 84173b01b8ce3932f816929e24af36efddfc24bd [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 Chung07e045c2013-11-06 15:49:51 -0800872 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700873 }
874 mOnResumeState = State.NONE;
875
Craig Mautner360310b2012-10-26 15:13:08 -0700876 // Background was set to gradient in onPause(), restore to black if in all apps.
877 setWorkspaceBackground(mState == State.WORKSPACE);
878
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800879 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700880 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700881 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700885 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700887 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200888 // We might have postponed some bind calls until onResume (see waitUntilResume) --
889 // execute them here
890 long startTimeCallbacks = 0;
891 if (DEBUG_RESUME_TIME) {
892 startTimeCallbacks = System.currentTimeMillis();
893 }
894
895 if (mAppsCustomizeContent != null) {
896 mAppsCustomizeContent.setBulkBind(true);
897 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700898 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
899 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200900 }
901 if (mAppsCustomizeContent != null) {
902 mAppsCustomizeContent.setBulkBind(false);
903 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700904 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200905 if (DEBUG_RESUME_TIME) {
906 Log.d(TAG, "Time spent processing callbacks in onResume: " +
907 (System.currentTimeMillis() - startTimeCallbacks));
908 }
Michael Jurka447bf842013-05-15 14:52:15 +0200909 }
Michael Jurka54554252013-08-01 12:52:23 +0200910 if (mOnResumeCallbacks.size() > 0) {
911 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
912 mOnResumeCallbacks.get(i).run();
913 }
914 mOnResumeCallbacks.clear();
915 }
Winson Chunge4e50662012-01-23 14:45:13 -0800916
917 // Reset the pressed state of icons that were locked in the press state while activities
918 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800919 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800920 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800921 mWaitingForResume.setStayPressed(false);
922 }
Winson Chunge4e50662012-01-23 14:45:13 -0800923 if (mAppsCustomizeContent != null) {
924 // Resets the previous all apps icon press state
925 mAppsCustomizeContent.resetDrawableState();
926 }
Winson Chung82963d52013-10-09 11:20:57 -0700927
Adam Cohen06dff352012-06-01 17:17:08 -0700928 // It is possible that widgets can receive updates while launcher is not in the foreground.
929 // Consequently, the widgets will be inflated in the orientation of the foreground activity
930 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
931 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700932 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700933
Winson Chung780fe592013-09-26 14:48:44 -0700934 // Process any items that were added while Launcher was away.
935 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
936
Winson Chung5841efa2013-09-30 18:06:44 -0700937 // Update the voice search button proxy
938 updateVoiceButtonProxyVisible(false);
939
Adam Cohenf9426d52012-06-04 17:26:21 -0700940 // Again, as with the above scenario, it's possible that one or more of the global icons
941 // were updated in the wrong orientation.
942 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200943 if (DEBUG_RESUME_TIME) {
944 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
945 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700946
947 if (mWorkspace.getCustomContentCallbacks() != null) {
948 // If we are resuming and the custom content is the current page, we call onShow().
949 // It is also poassible that onShow will instead be called slightly after first layout
950 // if PagedView#setRestorePage was set to the custom content page in onCreate().
951 if (mWorkspace.isOnOrMovingToCustomContent()) {
952 mWorkspace.getCustomContentCallbacks().onShow();
953 }
954 }
Adam Cohenedaaa302013-10-01 17:33:27 -0700955 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -0700956 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -0700957 }
958
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700960 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700961 // Ensure that items added to Launcher are queued until Launcher returns
962 InstallShortcutReceiver.enableInstallQueue();
963
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700964 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700965 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800966 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700967 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700968
969 // We call onHide() aggressively. The custom content callbacks should be able to
970 // debounce excess onHide calls.
971 if (mWorkspace.getCustomContentCallbacks() != null) {
972 mWorkspace.getCustomContentCallbacks().onHide();
973 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700974 }
Romain Guycbb89e42009-06-08 15:52:54 -0700975
Adam Cohen66a01fd2013-06-11 12:48:00 -0700976 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -0700977 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600978 addCustomContentToLeft();
979 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700980 }
981
Adam Cohenbffe7452013-07-22 18:21:45 -0700982 QSBScroller mQsbScroller = new QSBScroller() {
983 int scrollY = 0;
984
985 @Override
986 public void setScrollY(int scroll) {
987 scrollY = scroll;
988
989 if (mWorkspace.isOnOrMovingToCustomContent()) {
990 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +0100991 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -0700992 }
993 }
994 };
995
996 public void resetQSBScroll() {
997 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +0100998 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -0700999 }
1000
1001 public interface CustomContentCallbacks {
1002 // Custom content is completely shown
1003 public void onShow();
1004
1005 // Custom content is completely hidden
1006 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001007
1008 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1009 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001010 }
1011
Adam Cohen30bacb22013-08-29 14:25:25 -07001012 protected void startSettings() {
1013 }
1014
Adam Cohenbffe7452013-07-22 18:21:45 -07001015 public interface QSBScroller {
1016 public void setScrollY(int scrollY);
1017 }
1018
Winson Chung98ca0f72013-07-29 12:58:51 -07001019 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001020 CustomContentCallbacks callbacks, String description) {
1021 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001022 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001023 }
1024
Adam Cohenedb40762013-07-18 16:45:45 -07001025 // The custom content needs to offset its content to account for the QSB
1026 public int getTopOffsetForCustomContent() {
1027 return mWorkspace.getPaddingTop();
1028 }
1029
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001030 @Override
1031 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 // Flag the loader to stop early before switching
1033 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001034 if (mAppsCustomizeContent != null) {
1035 mAppsCustomizeContent.surrender();
1036 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001037 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001038 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001039
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001040 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001041 @Override
1042 public void onWindowFocusChanged(boolean hasFocus) {
1043 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001044 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001045 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 private boolean acceptFilter() {
1048 final InputMethodManager inputManager = (InputMethodManager)
1049 getSystemService(Context.INPUT_METHOD_SERVICE);
1050 return !inputManager.isFullscreenMode();
1051 }
1052
1053 @Override
1054 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001055 final int uniChar = event.getUnicodeChar();
1056 final boolean handled = super.onKeyDown(keyCode, event);
1057 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1058 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1060 keyCode, event);
1061 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001062 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001063 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001064 // showSearchDialog()
1065 // If there are multiple keystrokes before the search dialog takes focus,
1066 // onSearchRequested() will be called for every keystroke,
1067 // but it is idempotent, so it's fine.
1068 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 }
1070 }
1071
Joe Onorato8a9625e2010-01-28 15:55:35 -08001072 // Eat the long press event so the keyboard doesn't come up.
1073 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1074 return true;
1075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 return handled;
1078 }
1079
Karl Rosaen138a0412009-04-23 19:00:21 -07001080 private String getTypedText() {
1081 return mDefaultKeySsb.toString();
1082 }
1083
1084 private void clearTypedText() {
1085 mDefaultKeySsb.clear();
1086 mDefaultKeySsb.clearSpans();
1087 Selection.setSelection(mDefaultKeySsb, 0);
1088 }
1089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001091 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1092 * State
1093 */
1094 private static State intToState(int stateOrdinal) {
1095 State state = State.WORKSPACE;
1096 final State[] stateValues = State.values();
1097 for (int i = 0; i < stateValues.length; i++) {
1098 if (stateValues[i].ordinal() == stateOrdinal) {
1099 state = stateValues[i];
1100 break;
1101 }
1102 }
1103 return state;
1104 }
1105
1106 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 * Restores the previous state, if it exists.
1108 *
1109 * @param savedState The previous state.
1110 */
1111 private void restoreState(Bundle savedState) {
1112 if (savedState == null) {
1113 return;
1114 }
1115
Michael Jurka883f55b2010-10-21 15:47:14 -07001116 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001117 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001118 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001119 }
1120
Adam Cohen21cd0022013-10-09 18:57:02 -07001121 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1122 PagedView.INVALID_RESTORE_PAGE);
1123 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001124 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126
Winson Chung3d503fb2011-07-13 17:25:49 -07001127 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001128 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001129
Winson Chung3d503fb2011-07-13 17:25:49 -07001130 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1131 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001132 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001133 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1134 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001135 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1136 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1137 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001138 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001139 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 mRestoring = true;
1141 }
1142
1143 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1144 if (renameFolder) {
1145 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001146 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 mRestoring = true;
1148 }
Winson Chunga12a2502010-12-20 14:41:35 -08001149
Winson Chung785d2eb2011-04-14 16:08:02 -07001150 // Restore the AppsCustomize tab
1151 if (mAppsCustomizeTabHost != null) {
1152 String curTab = savedState.getString("apps_customize_currentTab");
1153 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001154 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001155 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001156 mAppsCustomizeContent.loadAssociatedPages(
1157 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001158 }
1159
Winson Chung5afbf7b2011-07-25 11:53:08 -07001160 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1161 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 }
1164
1165 /**
1166 * Finds all the views we need and configure them properly.
1167 */
1168 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001169 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001170
Craig Mautner360310b2012-10-26 15:13:08 -07001171 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001172 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1173 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001174
John Spurlock77e1f472013-09-11 10:09:51 -04001175 mLauncherView.setSystemUiVisibility(
1176 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001177 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
Winson Chung4c98d922011-05-31 16:50:48 -07001179 // Setup the drag layer
1180 mDragLayer.setup(this, dragController);
1181
Winson Chung3d503fb2011-07-13 17:25:49 -07001182 // Setup the hotseat
1183 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1184 if (mHotseat != null) {
1185 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001186 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001187 }
1188
Adam Cohenf358a4b2013-07-23 16:47:31 -07001189 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001190 View widgetButton = findViewById(R.id.widget_button);
1191 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001192 @Override
1193 public void onClick(View arg0) {
Winson 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() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001701 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001702 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 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001727
1728 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 }
Adam Cohened307df2013-10-02 09:37:31 -07001730
Michael Jurka447bf842013-05-15 14:52:15 +02001731 if (DEBUG_RESUME_TIME) {
1732 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1733 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 }
1735
Adam Coppa120b8e2013-11-12 16:26:04 +00001736 /**
1737 * Override point for subclasses to prevent movement to the default screen when the home
1738 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1739 */
1740 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1741 return true;
1742 }
1743
1744 /**
1745 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1746 */
1747 protected void onHomeIntent() {
1748 // Do nothing
1749 }
1750
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001752 public void onRestoreInstanceState(Bundle state) {
1753 super.onRestoreInstanceState(state);
1754 for (int page: mSynchronouslyBoundPages) {
1755 mWorkspace.restoreInstanceStateForChild(page);
1756 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 }
1758
1759 @Override
1760 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001761 if (mWorkspace.getChildCount() > 0) {
1762 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getRestorePage());
1763 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001764 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765
Michael Jurka883f55b2010-10-21 15:47:14 -07001766 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001767 // We close any open folder since it will not be re-opened, and we need to make sure
1768 // this state is reflected.
1769 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770
Adam Cohendcd297f2013-06-18 13:13:40 -07001771 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001772 mWaitingForResult) {
1773 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001774 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001775 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1776 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001777 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1778 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1779 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001780 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 }
1782
1783 if (mFolderInfo != null && mWaitingForResult) {
1784 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1785 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1786 }
Michael Jurka946ad472010-07-09 18:05:18 -07001787
Winson Chung785d2eb2011-04-14 16:08:02 -07001788 // Save the current AppsCustomize tab
1789 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001790 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1791 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001792 if (currentTabTag != null) {
1793 outState.putString("apps_customize_currentTab", currentTabTag);
1794 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001795 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1796 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
1799
1800 @Override
1801 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001803
Winson Chunge7a03942011-08-05 15:05:12 -07001804 // Remove all pending runnables
1805 mHandler.removeMessages(ADVANCE_MSG);
1806 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001807 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001808
Winson Chungcd2b0142011-06-08 16:02:26 -07001809 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001810 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001811 mModel.stopLoader();
1812 app.setLauncher(null);
1813
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001815 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001817 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001819 mAppWidgetHost = null;
1820
1821 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822
1823 TextKeyListener.getInstance().release();
1824
Winson Chung81b52252012-08-27 15:34:29 -07001825 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1826 // to prevent leaking Launcher activities on orientation change.
1827 if (mModel != null) {
1828 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1829 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001830
1831 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001832 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001833
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001834 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001835 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1836 mWorkspace.removeAllViews();
1837 mWorkspace = null;
1838 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001839
Michael Jurka2ecf9952012-06-18 12:52:28 -07001840 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 }
1842
Adam Cohena9cf38f2011-05-02 15:36:58 -07001843 public DragController getDragController() {
1844 return mDragController;
1845 }
1846
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 @Override
1848 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001849 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 super.startActivityForResult(intent, requestCode);
1851 }
1852
Winson Chung70d72102011-08-12 11:24:35 -07001853 /**
1854 * Indicates that we want global search for this activity by setting the globalSearch
1855 * argument for {@link #startSearch} to true.
1856 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001858 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001860
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001861 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001862
Karl Rosaen138a0412009-04-23 19:00:21 -07001863 if (initialQuery == null) {
1864 // Use any text typed in the launcher as the initial query
1865 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001866 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 if (appSearchData == null) {
1868 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001869 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 }
Winson Chungadf0c182012-08-23 14:59:07 -07001871 Rect sourceBounds = new Rect();
1872 if (mSearchDropTargetBar != null) {
1873 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1874 }
Romain Guycbb89e42009-06-08 15:52:54 -07001875
Bjorn Bringertc459e522013-06-07 19:36:01 +01001876 startSearch(initialQuery, selectInitialQuery,
1877 appSearchData, sourceBounds);
1878 }
1879
1880 public void startSearch(String initialQuery,
1881 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001882 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001883 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001884 }
1885
1886 /**
1887 * Starts the global search activity. This code is a copied from SearchManager
1888 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001889 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001890 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001891 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001892 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1893 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1894 if (globalSearchActivity == null) {
1895 Log.w(TAG, "No global search activity found.");
1896 return;
1897 }
1898 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1899 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1900 intent.setComponent(globalSearchActivity);
1901 // Make sure that we have a Bundle to put source in
1902 if (appSearchData == null) {
1903 appSearchData = new Bundle();
1904 } else {
1905 appSearchData = new Bundle(appSearchData);
1906 }
1907 // Set source to package name of app that starts global search, if not set already.
1908 if (!appSearchData.containsKey("source")) {
1909 appSearchData.putString("source", getPackageName());
1910 }
1911 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1912 if (!TextUtils.isEmpty(initialQuery)) {
1913 intent.putExtra(SearchManager.QUERY, initialQuery);
1914 }
1915 if (selectInitialQuery) {
1916 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1917 }
1918 intent.setSourceBounds(sourceBounds);
1919 try {
1920 startActivity(intent);
1921 } catch (ActivityNotFoundException ex) {
1922 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 }
1925
Winson Chung70d72102011-08-12 11:24:35 -07001926 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001927 public boolean onPrepareOptionsMenu(Menu menu) {
1928 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001929 if (!mWorkspace.isInOverviewMode()) {
1930 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001931 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001932 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001935 @Override
1936 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001937 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001938 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001939 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001940 }
1941
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942 public boolean isWorkspaceLocked() {
1943 return mWorkspaceLoading || mWaitingForResult;
1944 }
1945
Michael Jurka0280c3b2010-09-17 15:00:07 -07001946 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001947 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001949 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1950 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001951 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001952 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001953 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001954
Adam Cohenad4e15c2013-10-17 16:21:35 -07001955 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1956 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
1957 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1958 }
1959
1960 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1961 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
1962 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001963 if (appWidgetInfo.configure != null) {
1964 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08001965 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07001966
Bjorn Bringert7984c942009-12-09 15:38:25 +00001967 // Launch over to configure widget, if needed
1968 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001969 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001970 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001971 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001973 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001974 Runnable onComplete = new Runnable() {
1975 @Override
1976 public void run() {
1977 // Exit spring loaded mode if necessary after adding the widget
1978 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1979 null);
1980 }
1981 };
Adam Cohendcd297f2013-06-18 13:13:40 -07001982 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001983 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001984 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
1986 }
Romain Guycbb89e42009-06-08 15:52:54 -07001987
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001988 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001989 // Close any folders that may be open.
1990 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001991 mWorkspace.moveToCustomContentScreen(animate);
1992 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001993 /**
1994 * Process a shortcut drop.
1995 *
1996 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001998 * @param cell The cell it should be added to, optional
1999 * @param position The location on the screen where it was dropped, optional
2000 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002001 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002002 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002003 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002004 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002005 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002006 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002007
2008 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002009 mPendingAddInfo.cellX = cell[0];
2010 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002011 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002012
2013 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2014 createShortcutIntent.setComponent(componentName);
2015 processShortcut(createShortcutIntent);
2016 }
2017
Adam Cohenfbba09b2011-07-18 21:43:05 -07002018 /**
2019 * Process a widget drop.
2020 *
2021 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002022 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002023 * @param cell The cell it should be added to, optional
2024 * @param position The location on the screen where it was dropped, optional
2025 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002026 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002027 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002028 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002029 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002030 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002031 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002032 mPendingAddInfo.minSpanX = info.minSpanX;
2033 mPendingAddInfo.minSpanY = info.minSpanY;
2034
Adam Cohenfbba09b2011-07-18 21:43:05 -07002035 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002036 mPendingAddInfo.cellX = cell[0];
2037 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002038 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002039 if (span != null) {
2040 mPendingAddInfo.spanX = span[0];
2041 mPendingAddInfo.spanY = span[1];
2042 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002043
Adam Cohened66b2b2012-01-23 17:28:51 -08002044 AppWidgetHostView hostView = info.boundWidget;
2045 int appWidgetId;
2046 if (hostView != null) {
2047 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002048 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002049 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002050 // In this case, we either need to start an activity to get permission to bind
2051 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002052 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002053 Bundle options = info.bindOptions;
2054
2055 boolean success = false;
2056 if (options != null) {
2057 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2058 info.componentName, options);
2059 } else {
2060 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2061 info.componentName);
2062 }
2063 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002064 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002065 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002066 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002067 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2068 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2069 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002070 // TODO: we need to make sure that this accounts for the options bundle.
2071 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002072 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2073 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002074 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002075 }
2076
Joe Onoratodeb98af2010-02-19 14:59:39 -08002077 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002078 // Handle case where user selected "Applications"
2079 String applicationName = getResources().getString(R.string.group_applications);
2080 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002081
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002082 if (applicationName != null && applicationName.equals(shortcutName)) {
2083 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2084 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002085
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002086 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2087 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002088 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002089 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002090 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002091 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 }
2094
Winson Chung24ab2f12010-09-16 14:10:47 -07002095 void processWallpaper(Intent intent) {
2096 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2097 }
2098
Adam Cohendcd297f2013-06-18 13:13:40 -07002099 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002100 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002101 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 folderInfo.title = getText(R.string.folder_name);
2103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002105 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002106 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002107 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108
2109 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002110 FolderIcon newFolder =
2111 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002112 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002113 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002114 // Force measure the new folder icon
2115 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2116 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002117 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 }
Romain Guycbb89e42009-06-08 15:52:54 -07002119
Joe Onorato9c1289c2009-08-17 11:03:03 -04002120 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002121 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002122 }
2123
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002124 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002126 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002127 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 }
2129
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002130 protected ComponentName getWallpaperPickerComponent() {
2131 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2132 }
2133
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002134 /**
2135 * Registers various content observers. The current implementation registers
2136 * only a favorites observer to keep track of the favorites applications.
2137 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002138 private void registerContentObservers() {
2139 ContentResolver resolver = getContentResolver();
2140 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2141 true, mWidgetObserver);
2142 }
2143
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 @Override
2145 public boolean dispatchKeyEvent(KeyEvent event) {
2146 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2147 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002149 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002150 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002151 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002152 dumpState();
2153 return true;
2154 }
2155 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002156 }
2157 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2158 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002159 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 return true;
2161 }
2162 }
2163
2164 return super.dispatchKeyEvent(event);
2165 }
2166
Joe Onorato88ec0992009-11-19 13:16:06 -08002167 @Override
2168 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002169 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002170 if (mAppsCustomizeContent.getContentType() ==
2171 AppsCustomizePagedView.ContentType.Applications) {
2172 showWorkspace(true);
2173 } else {
2174 showOverviewMode(true);
2175 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002176 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002177 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002178 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002179 Folder openFolder = mWorkspace.getOpenFolder();
2180 if (openFolder.isEditingName()) {
2181 openFolder.dismissEditingName();
2182 } else {
2183 closeFolder();
2184 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002185 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002186 mWorkspace.exitWidgetResizeMode();
2187
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002188 // Back button is a no-op here, but give at least some feedback for the button press
2189 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002190 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002191 }
2192
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002194 * Re-listen when widgets are reset.
2195 */
2196 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002197 if (mAppWidgetHost != null) {
2198 mAppWidgetHost.startListening();
2199 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002200 }
2201
2202 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 * Launches the intent referred by the clicked shortcut.
2204 *
2205 * @param v The view representing the clicked shortcut.
2206 */
2207 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002208 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2209 // view has detached (it's possible for this to happen if the view is removed mid touch).
2210 if (v.getWindowToken() == null) {
2211 return;
2212 }
2213
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002214 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002215 return;
2216 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002217
Adam Cohen1697b792013-09-17 19:08:21 -07002218 if (v instanceof Workspace) {
2219 if (mWorkspace.isInOverviewMode()) {
2220 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002221 }
2222 return;
2223 }
2224
2225 if (v instanceof CellLayout) {
2226 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002227 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002228 }
2229 }
2230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002232 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002234 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2235 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002236
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002237 // Check for special shortcuts
2238 if (intent.getComponent() != null) {
2239 final String shortcutClass = intent.getComponent().getClassName();
2240
2241 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002242 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002243 return;
2244 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2245 MemoryDumpActivity.startDump(this);
2246 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002247 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2248 toggleShowWeightWatcher();
2249 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002250 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002251 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002252
2253 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002254 int[] pos = new int[2];
2255 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002256 intent.setSourceBounds(new Rect(pos[0], pos[1],
2257 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002258
2259 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002260
Daniel Sandlerff02d492013-08-05 02:12:05 -04002261 mStats.recordLaunch(intent, shortcut);
2262
Michael Jurkaddd62e92011-02-16 17:49:14 -08002263 if (success && v instanceof BubbleTextView) {
2264 mWaitingForResume = (BubbleTextView) v;
2265 mWaitingForResume.setStayPressed(true);
2266 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002268 if (v instanceof FolderIcon) {
2269 FolderIcon fi = (FolderIcon) v;
2270 handleFolderClick(fi);
2271 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002272 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002273 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002274 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002275 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002276 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 }
2279 }
2280
Michael Jurka0e260592010-06-30 17:07:39 -07002281 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002282 return false;
2283 }
2284
Michael Jurkaaf442092010-06-10 17:01:57 -07002285 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002286 * Event handler for the search button
2287 *
2288 * @param v The view that was clicked.
2289 */
2290 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002291 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2292
Amith Yamasania135ba82011-08-09 17:42:01 -07002293 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002294 }
2295
2296 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002297 * Event handler for the voice button
2298 *
2299 * @param v The view that was clicked.
2300 */
2301 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002302 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002303
Bjorn Bringertc459e522013-06-07 19:36:01 +01002304 startVoice();
2305 }
2306
2307 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002308 try {
2309 final SearchManager searchManager =
2310 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2311 ComponentName activityName = searchManager.getGlobalSearchActivity();
2312 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002313 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002314 if (activityName != null) {
2315 intent.setPackage(activityName.getPackageName());
2316 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002317 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002318 } catch (ActivityNotFoundException e) {
2319 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002320 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002321 startActivitySafely(null, intent, "onClickVoiceButton");
2322 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002323 }
2324
2325 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002326 * Event handler for the "grid" button that appears on the home screen, which
2327 * enters all apps mode.
2328 *
2329 * @param v The view that was clicked.
2330 */
2331 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002332 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002333 }
2334
2335 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002336 // Provide the same haptic feedback that the system offers for virtual keys.
2337 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002338 }
2339
Adam Cohen61f560d2013-09-30 15:58:20 -07002340 public void performHapticFeedbackOnTouchDown(View v) {
2341 // Provide the same haptic feedback that the system offers for virtual keys.
2342 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2343 }
2344
2345 public View.OnTouchListener getHapticFeedbackTouchListener() {
2346 if (mHapticFeedbackTouchListener == null) {
2347 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2348 @Override
2349 public boolean onTouch(View v, MotionEvent event) {
2350 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2351 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2352 }
2353 return false;
2354 }
2355 };
2356 }
2357 return mHapticFeedbackTouchListener;
2358 }
2359
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002360 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002361 if (!DISABLE_MARKET_BUTTON) {
2362 if (mAppMarketIntent != null) {
2363 startActivitySafely(v, mAppMarketIntent, "app market");
2364 } else {
2365 Log.e(TAG, "Invalid app market intent.");
2366 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002367 }
2368 }
2369
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002370 /**
2371 * Called when the user stops interacting with the launcher.
2372 * This implies that the user is now on the homescreen and is not doing housekeeping.
2373 */
2374 protected void onInteractionEnd() {}
2375
2376 /**
2377 * Called when the user starts interacting with the launcher.
2378 * The possible interactions are:
2379 * - open all apps
2380 * - reorder an app shortcut, or a widget
2381 * - open the overview mode.
2382 * This is a good time to stop doing things that only make sense
2383 * when the user is on the homescreen and not doing housekeeping.
2384 */
2385 protected void onInteractionBegin() {}
2386
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002387 void startApplicationDetailsActivity(ComponentName componentName) {
2388 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002389 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2390 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002391 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002392 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002393 }
2394
Michael Jurka1e2f4652013-07-08 18:03:46 -07002395 // returns true if the activity was started
2396 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002397 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002398 // System applications cannot be installed. For now, show a toast explaining that.
2399 // We may give them the option of disabling apps this way.
2400 int messageId = R.string.uninstall_system_app_text;
2401 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002402 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002403 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002404 String packageName = componentName.getPackageName();
2405 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002406 Intent intent = new Intent(
2407 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002408 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2409 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002410 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002411 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002412 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002413 }
2414
Michael Jurka86a720e2012-05-09 11:23:23 -07002415 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002419 // Only launch using the new animation if the shortcut has not opted out (this is a
2420 // private contract between launcher and may be ignored in the future).
2421 boolean useLaunchAnimation = (v != null) &&
2422 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2423 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002424 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2425 v.getMeasuredWidth(), v.getMeasuredHeight());
2426
2427 startActivity(intent, opts.toBundle());
2428 } else {
2429 startActivity(intent);
2430 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002431 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 } catch (SecurityException e) {
2433 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002434 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002436 "or use the exported attribute for this activity. "
2437 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002439 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002441
Michael Jurka86a720e2012-05-09 11:23:23 -07002442 boolean startActivitySafely(View v, Intent intent, Object tag) {
2443 boolean success = false;
2444 try {
2445 success = startActivity(v, intent, tag);
2446 } catch (ActivityNotFoundException e) {
2447 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2448 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2449 }
2450 return success;
2451 }
2452
Adam Cohena9cf38f2011-05-02 15:36:58 -07002453 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002454 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002455 Folder openFolder = mWorkspace.getFolderForTag(info);
2456
2457 // If the folder info reports that the associated folder is open, then verify that
2458 // it is actually opened. There have been a few instances where this gets out of sync.
2459 if (info.opened && openFolder == null) {
2460 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002461 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002462 info.opened = false;
2463 }
2464
Adam Cohenfb91f302012-06-11 15:45:18 -07002465 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 // Close any open folder
2467 closeFolder();
2468 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002469 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 } else {
2471 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002472 int folderScreen;
2473 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002474 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 // .. and close it
2476 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002477 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 // Close any folder open on the current screen
2479 closeFolder();
2480 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002481 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 }
2483 }
2484 }
2485 }
2486
Adam Cohen268c4752012-06-06 17:47:33 -07002487 /**
2488 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2489 * in the DragLayer in the exact absolute location of the original FolderIcon.
2490 */
2491 private void copyFolderIconToImage(FolderIcon fi) {
2492 final int width = fi.getMeasuredWidth();
2493 final int height = fi.getMeasuredHeight();
2494
2495 // Lazy load ImageView, Bitmap and Canvas
2496 if (mFolderIconImageView == null) {
2497 mFolderIconImageView = new ImageView(this);
2498 }
2499 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2500 mFolderIconBitmap.getHeight() != height) {
2501 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2502 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2503 }
2504
2505 DragLayer.LayoutParams lp;
2506 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2507 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2508 } else {
2509 lp = new DragLayer.LayoutParams(width, height);
2510 }
2511
Adam Cohen307fe232012-08-16 17:55:58 -07002512 // The layout from which the folder is being opened may be scaled, adjust the starting
2513 // view size by this scale factor.
2514 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002515 lp.customPosition = true;
2516 lp.x = mRectForFolderAnimation.left;
2517 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002518 lp.width = (int) (scale * width);
2519 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002520
2521 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2522 fi.draw(mFolderIconCanvas);
2523 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002524 if (fi.getFolder() != null) {
2525 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2526 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002527 }
Adam Cohen268c4752012-06-06 17:47:33 -07002528 // Just in case this image view is still in the drag layer from a previous animation,
2529 // we remove it and re-add it.
2530 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2531 mDragLayer.removeView(mFolderIconImageView);
2532 }
2533 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002534 if (fi.getFolder() != null) {
2535 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002536 }
Adam Cohen268c4752012-06-06 17:47:33 -07002537 }
2538
Adam Cohen2801caf2011-05-13 20:57:39 -07002539 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002540 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002541 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2542 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2543 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2544
Adam Cohenc51934b2011-07-26 21:07:43 -07002545 FolderInfo info = (FolderInfo) fi.getTag();
2546 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2547 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002548 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2549 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002550 }
2551
Adam Cohen268c4752012-06-06 17:47:33 -07002552 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2553 copyFolderIconToImage(fi);
2554 fi.setVisibility(View.INVISIBLE);
2555
Michael Jurka2ecf9952012-06-18 12:52:28 -07002556 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002557 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002558 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2559 oa.start();
2560 }
2561
Adam Cohen268c4752012-06-06 17:47:33 -07002562 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002563 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002564 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2565 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2566 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2567
Adam Cohen268c4752012-06-06 17:47:33 -07002568 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002569
Adam Cohen268c4752012-06-06 17:47:33 -07002570 // We remove and re-draw the FolderIcon in-case it has changed
2571 mDragLayer.removeView(mFolderIconImageView);
2572 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002573 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002574 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002575 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002576 oa.addListener(new AnimatorListenerAdapter() {
2577 @Override
2578 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002579 if (cl != null) {
2580 cl.clearFolderLeaveBehind();
2581 // Remove the ImageView copy of the FolderIcon and make the original visible.
2582 mDragLayer.removeView(mFolderIconImageView);
2583 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002584 }
2585 }
2586 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002587 oa.start();
2588 }
2589
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002591 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 * is animated relative to the specified View. If the View is null, no animation
2593 * is played.
2594 *
2595 * @param folderInfo The FolderInfo describing the folder to open.
2596 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002597 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002598 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002599 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600
Adam Cohena9cf38f2011-05-02 15:36:58 -07002601 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602
Adam Cohen4554ee12011-08-03 16:13:21 -07002603 // Just verify that the folder hasn't already been added to the DragLayer.
2604 // There was a one-off crash where the folder had a parent already.
2605 if (folder.getParent() == null) {
2606 mDragLayer.addView(folder);
2607 mDragController.addDropTarget((DropTarget) folder);
2608 } else {
2609 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2610 folder.getParent() + ").");
2611 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002612 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002613 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002614
2615 // Notify the accessibility manager that this folder "window" has appeared and occluded
2616 // the workspace items
2617 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2618 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002619 }
2620
2621 public void closeFolder() {
2622 Folder folder = mWorkspace.getOpenFolder();
2623 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002624 if (folder.isEditingName()) {
2625 folder.dismissEditingName();
2626 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002627 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002628
2629 // Dismiss the folder cling
2630 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002631 }
2632 }
2633
2634 void closeFolder(Folder folder) {
2635 folder.getInfo().opened = false;
2636
2637 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2638 if (parent != null) {
2639 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2640 shrinkAndFadeInFolderIcon(fi);
2641 }
2642 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002643
2644 // Notify the accessibility manager that this folder "window" has disappeard and no
2645 // longer occludeds the workspace items
2646 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002647 }
2648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002650 if (!isDraggingEnabled()) return false;
2651 if (isWorkspaceLocked()) return false;
2652 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002653
Adam Cohen1697b792013-09-17 19:08:21 -07002654 if (v instanceof Workspace) {
2655 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002656 if (mWorkspace.enterOverviewMode()) {
2657 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2658 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2659 return true;
2660 } else {
2661 return false;
2662 }
Adam Cohen1697b792013-09-17 19:08:21 -07002663 }
Adam Cohen1697b792013-09-17 19:08:21 -07002664 }
2665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002667 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002668 }
2669
Michael Jurka0280c3b2010-09-17 15:00:07 -07002670 resetAddInfo();
2671 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002672 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002673 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002674 return true;
2675 }
2676
Winson Chung3d503fb2011-07-13 17:25:49 -07002677 // The hotseat touch handling does not go through Workspace, and we always allow long press
2678 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002679 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002680 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2681 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002682 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002683 // User long pressed on empty space
2684 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2685 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2686 // Disabling reordering until we sort out some issues.
2687 if (mWorkspace.isInOverviewMode()) {
2688 mWorkspace.startReordering(v);
2689 } else {
2690 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002693 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002695 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002696 }
2697 }
2698 }
2699 return true;
2700 }
2701
Winson Chung3d503fb2011-07-13 17:25:49 -07002702 boolean isHotseatLayout(View layout) {
2703 return mHotseat != null && layout != null &&
2704 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2705 }
2706 Hotseat getHotseat() {
2707 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002708 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002709 View getOverviewPanel() {
2710 return mOverviewPanel;
2711 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002712 SearchDropTargetBar getSearchBar() {
2713 return mSearchDropTargetBar;
2714 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002715
Winson Chung3d503fb2011-07-13 17:25:49 -07002716 /**
2717 * Returns the CellLayout of the specified container at the specified screen.
2718 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002719 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002720 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2721 if (mHotseat != null) {
2722 return mHotseat.getLayout();
2723 } else {
2724 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002725 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002726 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002727 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002728 }
2729 }
2730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002731 Workspace getWorkspace() {
2732 return mWorkspace;
2733 }
2734
Daniel Sandler843e8602010-06-07 14:59:01 -04002735 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002736 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002737 }
2738
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002739 /**
2740 * Helper method for the cameraZoomIn/cameraZoomOut animations
2741 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002742 * @param scaleFactor The scale factor used for the zoom
2743 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002744 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002745 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002746 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002747 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002748
Craig Mautner360310b2012-10-26 15:13:08 -07002749 private void setWorkspaceBackground(boolean workspace) {
2750 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002751 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002752 }
2753
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002754 void updateWallpaperVisibility(boolean visible) {
2755 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2756 int curflags = getWindow().getAttributes().flags
2757 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2758 if (wpflags != curflags) {
2759 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2760 }
Craig Mautner360310b2012-10-26 15:13:08 -07002761 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002762 }
2763
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002764 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2765 if (v instanceof LauncherTransitionable) {
2766 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2767 }
2768 }
2769
Michael Jurkabed61d22012-02-14 22:51:29 -08002770 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2771 if (v instanceof LauncherTransitionable) {
2772 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2773 }
Winson Chung70442722012-02-10 15:43:22 -08002774
2775 // Update the workspace transition step as well
2776 dispatchOnLauncherTransitionStep(v, 0f);
2777 }
2778
2779 private void dispatchOnLauncherTransitionStep(View v, float t) {
2780 if (v instanceof LauncherTransitionable) {
2781 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2782 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002783 }
2784
2785 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2786 if (v instanceof LauncherTransitionable) {
2787 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2788 }
Winson Chung70442722012-02-10 15:43:22 -08002789
2790 // Update the workspace transition step as well
2791 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002792 }
2793
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002794 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002795 * Things to test when changing the following seven functions.
2796 * - Home from workspace
2797 * - from center screen
2798 * - from other screens
2799 * - Home from all apps
2800 * - from center screen
2801 * - from other screens
2802 * - Back from all apps
2803 * - from center screen
2804 * - from other screens
2805 * - Launch app from workspace and quit
2806 * - with back
2807 * - with home
2808 * - Launch app from all apps and quit
2809 * - with back
2810 * - with home
2811 * - Go to a screen that's not the default, then all
2812 * apps, and launch and app, and go back
2813 * - with back
2814 * -with home
2815 * - On workspace, long press power and go back
2816 * - with back
2817 * - with home
2818 * - On all apps, long press power and go back
2819 * - with back
2820 * - with home
2821 * - On workspace, power off
2822 * - On all apps, power off
2823 * - Launch an app and turn off the screen while in that app
2824 * - Go back with home key
2825 * - Go back with back key TODO: make this not go to workspace
2826 * - From all apps
2827 * - From workspace
2828 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2829 * - From all apps
2830 * - From the center workspace
2831 * - From another workspace
2832 */
2833
2834 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002835 * Zoom the camera out from the workspace to reveal 'toView'.
2836 * Assumes that the view to show is anchored at either the very top or very bottom
2837 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002838 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002839 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002840 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2841 showAppsCustomizeHelper(animated, springLoaded, contentType);
2842 }
2843 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2844 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002845 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002846 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002847 mStateAnimation.cancel();
2848 mStateAnimation = null;
2849 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002850 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002851
Winson Chungf0ea4d32011-06-06 14:27:16 -07002852 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2853 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2854 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002855 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002856 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002857 final int startDelay =
2858 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002859
Michael Jurkab3e22d92011-10-31 15:58:33 -07002860 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002861
Michael Jurkad74c9842011-07-10 12:44:21 -07002862 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002863 Animator workspaceAnim =
2864 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002865 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2866 // Set the content type for the all apps space
2867 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2868 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002869
2870 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002871 toView.setScaleX(scale);
2872 toView.setScaleY(scale);
2873 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2874 scaleAnim.
2875 scaleX(1f).scaleY(1f).
2876 setDuration(duration).
2877 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002878
Winson Chungf0ea4d32011-06-06 14:27:16 -07002879 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002880 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002881 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002882 .ofFloat(toView, "alpha", 0f, 1f)
2883 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002884 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002885 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2886 @Override
2887 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002888 if (animation == null) {
2889 throw new RuntimeException("animation is null");
2890 }
Winson Chung70442722012-02-10 15:43:22 -08002891 float t = (Float) animation.getAnimatedValue();
2892 dispatchOnLauncherTransitionStep(fromView, t);
2893 dispatchOnLauncherTransitionStep(toView, t);
2894 }
2895 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002896
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002897 // toView should appear right at the end of the workspace shrink
2898 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002899 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002900 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002901 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002902
2903 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002904 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002905 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002906 // Prepare the position
2907 toView.setTranslationX(0.0f);
2908 toView.setTranslationY(0.0f);
2909 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002910 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002911 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002912 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002913 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002914 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2915 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002916
Winson Chungc7d2b602012-05-16 17:02:20 -07002917 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002918 if (mSearchDropTargetBar != null) {
2919 mSearchDropTargetBar.hideSearchBar(false);
2920 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002921 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002922 });
2923
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002924 if (workspaceAnim != null) {
2925 mStateAnimation.play(workspaceAnim);
2926 }
Michael Jurka1899a362011-11-03 13:50:45 -07002927
2928 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002929
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002930 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2931 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002932
2933 // If any of the objects being animated haven't been measured/laid out
2934 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002935 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002936 (mWorkspace.getMeasuredWidth() == 0) ||
2937 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002938 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002939 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002940
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002941 final AnimatorSet stateAnimation = mStateAnimation;
2942 final Runnable startAnimRunnable = new Runnable() {
2943 public void run() {
2944 // Check that mStateAnimation hasn't changed while
2945 // we waited for a layout/draw pass
2946 if (mStateAnimation != stateAnimation)
2947 return;
2948 setPivotsForZoom(toView, scale);
2949 dispatchOnLauncherTransitionStart(fromView, animated, false);
2950 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002951 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002952 }
2953 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002954 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002955 final ViewTreeObserver observer = toView.getViewTreeObserver();
2956 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2957 public void onGlobalLayout() {
2958 startAnimRunnable.run();
2959 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2960 }
2961 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002962 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002963 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002964 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002965 } else {
2966 toView.setTranslationX(0.0f);
2967 toView.setTranslationY(0.0f);
2968 toView.setScaleX(1.0f);
2969 toView.setScaleY(1.0f);
2970 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002971 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002972
Daniel Sandlere4f98912013-06-25 15:13:26 -04002973 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002974 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002975 if (mSearchDropTargetBar != null) {
2976 mSearchDropTargetBar.hideSearchBar(false);
2977 }
Michael Jurkaabded662011-03-04 12:06:57 -08002978 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002979 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002980 dispatchOnLauncherTransitionStart(fromView, animated, false);
2981 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002982 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002983 dispatchOnLauncherTransitionStart(toView, animated, false);
2984 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002985 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002986 }
2987
2988 /**
2989 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002990 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002991 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002992 */
Adam Cohened307df2013-10-02 09:37:31 -07002993 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08002994 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002995
Michael Jurkab3e22d92011-10-31 15:58:33 -07002996 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002997 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002998 mStateAnimation.cancel();
2999 mStateAnimation = null;
3000 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003001 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003002
Winson Chungf0ea4d32011-06-06 14:27:16 -07003003 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003004 final int fadeOutDuration =
3005 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003006 final float scaleFactor = (float)
3007 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3008 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003009 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003010 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003011 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003012 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3013 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003014 toState, animated, stagger, -1);
3015 } else if (toState == Workspace.State.SPRING_LOADED ||
3016 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003017 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003018 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003019 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003020
Michael Jurkab3e22d92011-10-31 15:58:33 -07003021 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003022 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003023 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003024 final LauncherViewPropertyAnimator scaleAnim =
3025 new LauncherViewPropertyAnimator(fromView);
3026 scaleAnim.
3027 scaleX(scaleFactor).scaleY(scaleFactor).
3028 setDuration(duration).
3029 setInterpolator(new Workspace.ZoomInInterpolator());
3030
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003031 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003032 .ofFloat(fromView, "alpha", 1f, 0f)
3033 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003034 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003035 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3036 @Override
3037 public void onAnimationUpdate(ValueAnimator animation) {
3038 float t = 1f - (Float) animation.getAnimatedValue();
3039 dispatchOnLauncherTransitionStep(fromView, t);
3040 dispatchOnLauncherTransitionStep(toView, t);
3041 }
3042 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003043
Michael Jurka2ecf9952012-06-18 12:52:28 -07003044 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003045
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003046 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3047 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003048 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003049
3050 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003051 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003052 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003053 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003054 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3055 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003056 if (onCompleteRunnable != null) {
3057 onCompleteRunnable.run();
3058 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003059 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003060 }
3061 });
3062
Winson Chungf0ea4d32011-06-06 14:27:16 -07003063 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003064 if (workspaceAnim != null) {
3065 mStateAnimation.play(workspaceAnim);
3066 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003067 dispatchOnLauncherTransitionStart(fromView, animated, true);
3068 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003069 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003070 } else {
3071 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003072 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003073 dispatchOnLauncherTransitionStart(fromView, animated, true);
3074 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003075 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003076 dispatchOnLauncherTransitionStart(toView, animated, true);
3077 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003078 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003079 }
3080
Michael Jurkae326f182011-11-21 14:05:46 -08003081 @Override
3082 public void onTrimMemory(int level) {
3083 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003084 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003085 mAppsCustomizeTabHost.onTrimMemory();
3086 }
3087 }
3088
Adam Cohened307df2013-10-02 09:37:31 -07003089 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003090 showWorkspace(animated, null);
3091 }
3092
Adam Cohened307df2013-10-02 09:37:31 -07003093 protected void showWorkspace() {
3094 showWorkspace(true);
3095 }
3096
Adam Cohened66b2b2012-01-23 17:28:51 -08003097 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003098 if (mWorkspace.isInOverviewMode()) {
3099 mWorkspace.exitOverviewMode(animated);
3100 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003101 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003102 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003103 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003104 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003105
Winson Chungc7d2b602012-05-16 17:02:20 -07003106 // Show the search bar (only animate if we were showing the drop target bar in spring
3107 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003108 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003109 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003110 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003111
Michael Jurkab3e22d92011-10-31 15:58:33 -07003112 // Set focus to the AppsCustomize button
3113 if (mAllAppsButton != null) {
3114 mAllAppsButton.requestFocus();
3115 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003116 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003117
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003118 // Change the state *after* we've called all the transition code
3119 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003120
Winson Chung5fb63472011-02-02 17:03:37 -08003121 // Resume the auto-advance of widgets
3122 mUserPresent = true;
3123 updateRunning();
3124
alanv1d4fde62012-10-17 13:15:47 -07003125 // Send an accessibility event to announce the context change
3126 getWindow().getDecorView()
3127 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003128
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003129 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003130 }
3131
Adam Cohened307df2013-10-02 09:37:31 -07003132 void showOverviewMode(boolean animated) {
3133 mWorkspace.setVisibility(View.VISIBLE);
3134 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3135 mState = State.WORKSPACE;
3136 onWorkspaceShown(animated);
3137 }
3138
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003139 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003140 }
3141
Winson Chung82963d52013-10-09 11:20:57 -07003142 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3143 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003144 if (mState != State.WORKSPACE) return;
3145
Winson Chung82963d52013-10-09 11:20:57 -07003146 if (resetPageToZero) {
3147 mAppsCustomizeTabHost.reset();
3148 }
Winson Chungc58497e2013-09-03 17:48:37 -07003149 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003150 mAppsCustomizeTabHost.requestFocus();
3151
Michael Jurkab3e22d92011-10-31 15:58:33 -07003152 // Change the state *after* we've called all the transition code
3153 mState = State.APPS_CUSTOMIZE;
3154
3155 // Pause the auto-advance of widgets until we are out of AllApps
3156 mUserPresent = false;
3157 updateRunning();
3158 closeFolder();
3159
3160 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003161 getWindow().getDecorView()
3162 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003163 }
3164
Adam Cohen7777d962011-08-18 18:58:38 -07003165 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003166 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003167 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003168 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003169 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003170 }
Adam Cohen7777d962011-08-18 18:58:38 -07003171
Adam Cohenad4e15c2013-10-17 16:21:35 -07003172 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003173 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003174 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3175
Winson Chunge7a03942011-08-05 15:05:12 -07003176 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003177 @Override
3178 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003179 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003180 // Before we show workspace, hide all apps again because
3181 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3182 // clean up our state transition functions
3183 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003184 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003185 } else {
3186 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003187 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003188 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003189 }, delay);
3190
Winson Chung557d6ed2011-07-08 15:34:52 -07003191 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003192
Michael Jurkad3ef3062010-11-23 16:23:58 -08003193 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003194 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003195 final boolean animated = true;
3196 final boolean springLoaded = true;
3197 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003198 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003199 }
3200 // Otherwise, we are not in spring loaded mode, so don't do anything.
3201 }
3202
Michael Jurkab3e22d92011-10-31 15:58:33 -07003203 void lockAllApps() {
3204 // TODO
3205 }
3206
3207 void unlockAllApps() {
3208 // TODO
3209 }
3210
Winson Chungf0ea4d32011-06-06 14:27:16 -07003211 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003212 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003213 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003214 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003215 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003216 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003217 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003218 int duration = 0;
3219 if (mSearchDropTargetBar != null) {
3220 duration = mSearchDropTargetBar.getTransitionInDuration();
3221 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003222 mHotseat.animate().alpha(1f).setDuration(duration);
3223 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003224 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003225 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003226 }
3227 }
3228 }
3229
3230 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003231 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003232 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003233 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003234 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003235 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003236 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003237 int duration = 0;
3238 if (mSearchDropTargetBar != null) {
3239 duration = mSearchDropTargetBar.getTransitionOutDuration();
3240 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003241 mHotseat.animate().alpha(0f).setDuration(duration);
3242 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003243 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003244 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003245 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003246 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003247 }
3248
Patrick Dubroy5f445422011-02-18 14:35:21 -08003249 /**
3250 * Add an item from all apps or customize onto the given workspace screen.
3251 * If layout is null, add to the current screen.
3252 */
3253 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003254 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003255 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003256 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003257 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003258
Winson Chungdff8ebb2011-09-08 17:25:31 -07003259 /** Maps the current orientation to an index for referencing orientation correct global icons */
3260 private int getCurrentOrientationIndexForGlobalIcons() {
3261 // default - 0, landscape - 1
3262 switch (getResources().getConfiguration().orientation) {
3263 case Configuration.ORIENTATION_LANDSCAPE:
3264 return 1;
3265 default:
3266 return 0;
3267 }
3268 }
3269
Michael Jurkaae65ee32012-04-30 11:45:54 -07003270 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003271 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003272 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003273 // Look for the toolbar icon specified in the activity meta-data
3274 Bundle metaData = packageManager.getActivityInfo(
3275 activityName, PackageManager.GET_META_DATA).metaData;
3276 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003277 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003278 if (iconResId != 0) {
3279 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003280 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003281 }
3282 }
3283 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003284 // This can happen if the activity defines an invalid drawable
3285 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3286 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003287 } catch (Resources.NotFoundException nfe) {
3288 // This can happen if the activity defines an invalid drawable
3289 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3290 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003291 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003292 return null;
3293 }
3294
3295 // if successful in getting icon, return it; otherwise, set button to use default drawable
3296 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003297 int buttonId, ComponentName activityName, int fallbackDrawableId,
3298 String toolbarResourceName) {
3299 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003300 Resources r = getResources();
3301 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3302 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003303
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003304 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003305 // If we were unable to find the icon via the meta-data, use a generic one
3306 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003307 toolbarIcon = r.getDrawable(fallbackDrawableId);
3308 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003309 if (button != null) {
3310 button.setCompoundDrawables(toolbarIcon, null, null, null);
3311 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003312 return null;
3313 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003314 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003315 if (button != null) {
3316 button.setCompoundDrawables(toolbarIcon, null, null, null);
3317 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003318 return toolbarIcon.getConstantState();
3319 }
3320 }
3321
3322 // if successful in getting icon, return it; otherwise, set button to use default drawable
3323 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003324 int buttonId, ComponentName activityName, int fallbackDrawableId,
3325 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003326 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003327 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003328
Michael Jurka19e0fc52011-07-22 18:00:21 -07003329 if (button != null) {
3330 // If we were unable to find the icon via the meta-data, use a
3331 // generic one
3332 if (toolbarIcon == null) {
3333 button.setImageResource(fallbackDrawableId);
3334 } else {
3335 button.setImageDrawable(toolbarIcon);
3336 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003337 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003338
3339 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3340
Michael Jurka0423dcf2010-10-05 14:56:18 -07003341 }
3342
Winson Chung1b884092012-06-08 17:13:02 -07003343 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003344 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003345 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003346 }
3347
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003348 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003349 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003350 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003351 }
3352
Winson Chungbb185bd2011-11-21 12:31:42 -08003353 private void invalidatePressedFocusedStates(View container, View button) {
3354 if (container instanceof HolographicLinearLayout) {
3355 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3356 layout.invalidatePressedFocusedStates();
3357 } else if (button instanceof HolographicImageView) {
3358 HolographicImageView view = (HolographicImageView) button;
3359 view.invalidatePressedFocusedStates();
3360 }
3361 }
3362
Cristina Stancu476493b2013-08-07 17:20:14 +01003363 public View getQsbBar() {
3364 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003365 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3366 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003367 }
3368 return mQsbBar;
3369 }
3370
3371 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003372 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003373 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003374 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003375 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003376
Winson Chung1cad91e2011-05-25 17:41:01 -07003377 final SearchManager searchManager =
3378 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3379 ComponentName activityName = searchManager.getGlobalSearchActivity();
3380 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003381 int coi = getCurrentOrientationIndexForGlobalIcons();
3382 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003383 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3384 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3385 if (sGlobalSearchIcon[coi] == null) {
3386 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3387 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3388 TOOLBAR_ICON_METADATA_NAME);
3389 }
3390
Winson Chungc51db6a2011-10-05 11:44:49 -07003391 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3392 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003393 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003394 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003395 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003396 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003397 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3398 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003399 if (searchButton != null) searchButton.setVisibility(View.GONE);
3400 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003401 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003402 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003403 }
3404 }
3405
Cristina Stancu476493b2013-08-07 17:20:14 +01003406 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003407 final View searchButtonContainer = findViewById(R.id.search_button_container);
3408 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003409 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003410 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003411 }
3412
Cristina Stancu476493b2013-08-07 17:20:14 +01003413 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003414 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003415 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003416
Winson Chungc51db6a2011-10-05 11:44:49 -07003417 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003418 final SearchManager searchManager =
3419 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3420 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3421
3422 ComponentName activityName = null;
3423 if (globalSearchActivity != null) {
3424 // Check if the global search activity handles voice search
3425 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3426 intent.setPackage(globalSearchActivity.getPackageName());
3427 activityName = intent.resolveActivity(getPackageManager());
3428 }
3429
3430 if (activityName == null) {
3431 // Fallback: check if an activity other than the global search activity
3432 // resolves this
3433 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3434 activityName = intent.resolveActivity(getPackageManager());
3435 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003436 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003437 int coi = getCurrentOrientationIndexForGlobalIcons();
3438 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003439 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3440 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3441 if (sVoiceSearchIcon[coi] == null) {
3442 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3443 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3444 TOOLBAR_ICON_METADATA_NAME);
3445 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003446 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003447 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003448 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003449 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003450 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003451 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003452 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003453 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003454 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003455 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003456 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003457 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003458
Cristina Stancu476493b2013-08-07 17:20:14 +01003459 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003460 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3461 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003462 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003463 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003464 }
3465
Winson Chung5841efa2013-09-30 18:06:44 -07003466 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003467 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3468 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003469 boolean visible = !forceDisableVoiceButtonProxy &&
3470 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003471 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003472 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003473 }
3474 }
Winson Chung5841efa2013-09-30 18:06:44 -07003475
3476 /**
3477 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3478 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3479 */
3480 public void disableVoiceButtonProxy(boolean disabled) {
3481 updateVoiceButtonProxyVisible(disabled);
3482 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003483 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003484 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003485 */
3486 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003487 if (!DISABLE_MARKET_BUTTON) {
3488 final View marketButton = findViewById(R.id.market_button);
3489 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3490 // Find the app market activity by resolving an intent.
3491 // (If multiple app markets are installed, it will return the ResolverActivity.)
3492 ComponentName activityName = intent.resolveActivity(getPackageManager());
3493 if (activityName != null) {
3494 int coi = getCurrentOrientationIndexForGlobalIcons();
3495 mAppMarketIntent = intent;
3496 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3497 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3498 TOOLBAR_ICON_METADATA_NAME);
3499 marketButton.setVisibility(View.VISIBLE);
3500 } else {
3501 // We should hide and disable the view so that we don't try and restore the visibility
3502 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3503 marketButton.setVisibility(View.GONE);
3504 marketButton.setEnabled(false);
3505 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003506 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003507 }
3508
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003509 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003510 if (!DISABLE_MARKET_BUTTON) {
3511 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3512 Resources r = getResources();
3513 Drawable marketIconDrawable = d.newDrawable(r);
3514 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3515 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3516 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003517
Winson Chungd64a6662013-09-30 11:06:59 -07003518 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3519 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003520 }
3521
Michael Jurkad7c28052012-04-27 15:43:36 -07003522 @Override
3523 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003524 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003525 final List<CharSequence> text = event.getText();
3526 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003527 // Populate event with a fake title based on the current state.
3528 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003529 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003530 } else {
3531 text.add(getString(R.string.all_apps_home_button_label));
3532 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003533 return result;
3534 }
3535
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003536 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003537 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003538 */
3539 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3540 @Override
3541 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003542 closeSystemDialogs();
3543 }
3544 }
3545
3546 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003547 * Receives notifications whenever the appwidgets are reset.
3548 */
3549 private class AppWidgetResetObserver extends ContentObserver {
3550 public AppWidgetResetObserver() {
3551 super(new Handler());
3552 }
3553
3554 @Override
3555 public void onChange(boolean selfChange) {
3556 onAppWidgetReset();
3557 }
3558 }
3559
3560 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003561 * If the activity is currently paused, signal that we need to run the passed Runnable
3562 * in onResume.
3563 *
3564 * This needs to be called from incoming places where resources might have been loaded
3565 * while we are paused. That is becaues the Configuration might be wrong
3566 * when we're not running, and if it comes back to what it was when we
3567 * were paused, we are not restarted.
3568 *
3569 * Implementation of the method from LauncherModel.Callbacks.
3570 *
3571 * @return true if we are currently paused. The caller might be able to
3572 * skip some work in that case since we will come back again.
3573 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003574 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003575 if (mPaused) {
3576 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003577 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003578 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003579 }
3580 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003581 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003582 return true;
3583 } else {
3584 return false;
3585 }
3586 }
3587
Michael Jurkac402cd92013-05-20 15:49:32 +02003588 private boolean waitUntilResume(Runnable run) {
3589 return waitUntilResume(run, false);
3590 }
3591
Michael Jurka1e2f4652013-07-08 18:03:46 -07003592 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003593 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003594 }
3595
Michael Jurka7607c2f2013-04-03 14:33:19 -07003596 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003597 * If the activity is currently paused, signal that we need to re-run the loader
3598 * in onResume.
3599 *
3600 * This needs to be called from incoming places where resources might have been loaded
3601 * while we are paused. That is becaues the Configuration might be wrong
3602 * when we're not running, and if it comes back to what it was when we
3603 * were paused, we are not restarted.
3604 *
3605 * Implementation of the method from LauncherModel.Callbacks.
3606 *
3607 * @return true if we are currently paused. The caller might be able to
3608 * skip some work in that case since we will come back again.
3609 */
3610 public boolean setLoadOnResume() {
3611 if (mPaused) {
3612 Log.i(TAG, "setLoadOnResume");
3613 mOnResumeNeedsLoad = true;
3614 return true;
3615 } else {
3616 return false;
3617 }
3618 }
3619
3620 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003622 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003624 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003625 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003626 } else {
3627 return SCREEN_COUNT / 2;
3628 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003629 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003630
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 /**
3632 * Refreshes the shortcuts shown on the workspace.
3633 *
3634 * Implementation of the method from LauncherModel.Callbacks.
3635 */
3636 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003637 // If we're starting binding all over again, clear any bind calls we'd postponed in
3638 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3639 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003640 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003641
Winson Chungd64d1762013-08-20 14:37:16 -07003642 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003643 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003644 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003645
Michael Jurka05bf6442011-11-30 20:28:53 -08003646 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003647 if (mHotseat != null) {
3648 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003649 }
3650 }
3651
Adam Cohendcd297f2013-06-18 13:13:40 -07003652 @Override
3653 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003654 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003655
Adam Cohen5084cba2013-09-03 12:01:16 -07003656 // If there are no screens, we need to have an empty screen
3657 if (orderedScreenIds.size() == 0) {
3658 mWorkspace.addExtraEmptyScreen();
3659 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003660
3661 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003662 // setCurrentPage() so ensure that all pages are added before calling this).
3663 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003664 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3665 mWorkspace.createCustomContentPage();
3666 }
Winson Chung64359a52013-07-08 17:17:08 -07003667 }
3668
3669 @Override
3670 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003671 int count = orderedScreenIds.size();
3672 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003673 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003674 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003675 }
3676
Adam Cohen39a06042013-07-19 14:30:12 -07003677 private boolean shouldShowWeightWatcher() {
3678 String spKey = LauncherAppState.getSharedPreferencesKey();
3679 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003680 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003681
3682 return show;
3683 }
3684
3685 private void toggleShowWeightWatcher() {
3686 String spKey = LauncherAppState.getSharedPreferencesKey();
3687 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3688 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3689
3690 show = !show;
3691
3692 SharedPreferences.Editor editor = sp.edit();
3693 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3694 editor.commit();
3695
3696 if (mWeightWatcher != null) {
3697 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3698 }
3699 }
3700
Winson Chungd64d1762013-08-20 14:37:16 -07003701 public void bindAppsAdded(final ArrayList<Long> newScreens,
3702 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003703 final ArrayList<ItemInfo> addAnimated,
3704 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003705 Runnable r = new Runnable() {
3706 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003707 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003708 }
3709 };
3710 if (waitUntilResume(r)) {
3711 return;
3712 }
3713
Winson Chungd64d1762013-08-20 14:37:16 -07003714 // Add the new screens
3715 bindAddScreens(newScreens);
3716
3717 // We add the items without animation on non-visible pages, and with
3718 // animations on the new page (which we will try and snap to).
3719 if (!addNotAnimated.isEmpty()) {
3720 bindItems(addNotAnimated, 0,
3721 addNotAnimated.size(), false);
3722 }
3723 if (!addAnimated.isEmpty()) {
3724 bindItems(addAnimated, 0,
3725 addAnimated.size(), true);
3726 }
Winson Chungc58497e2013-09-03 17:48:37 -07003727
Winson Chung87412982013-10-03 18:34:14 -07003728 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003729 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003730
Winson Chungc58497e2013-09-03 17:48:37 -07003731 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3732 addedApps != null && mAppsCustomizeContent != null) {
3733 mAppsCustomizeContent.addApps(addedApps);
3734 }
Winson Chungd64d1762013-08-20 14:37:16 -07003735 }
3736
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003737 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003738 * Bind the items start-end from the list.
3739 *
3740 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003741 */
Winson Chung64359a52013-07-08 17:17:08 -07003742 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3743 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003744 Runnable r = new Runnable() {
3745 public void run() {
3746 bindItems(shortcuts, start, end, forceAnimateIcons);
3747 }
3748 };
3749 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003750 return;
3751 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003752
Winson Chungf0c6ae02012-03-21 16:10:31 -07003753 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003754 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3755 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003756 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003757 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003758 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003759 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003760 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003761
3762 // Short circuit if we are loading dock items for a configuration which has no dock
3763 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3764 mHotseat == null) {
3765 continue;
3766 }
3767
Joe Onorato9c1289c2009-08-17 11:03:03 -04003768 switch (item.itemType) {
3769 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3770 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003771 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003772 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003773
Winson Chung64359a52013-07-08 17:17:08 -07003774 /*
3775 * TODO: FIX collision case
3776 */
Winson Chungce376632013-07-11 16:12:41 -07003777 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3778 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3779 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3780 throw new RuntimeException("OCCUPIED");
3781 }
Winson Chung64359a52013-07-08 17:17:08 -07003782 }
3783
Adam Cohendcd297f2013-06-18 13:13:40 -07003784 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3785 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003786 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003787 // Animate all the applications up now
3788 shortcut.setAlpha(0f);
3789 shortcut.setScaleX(0f);
3790 shortcut.setScaleY(0f);
3791 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003792 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003794 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003795 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003796 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003797 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003798 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003799 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3800 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003801 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003802 default:
3803 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003805 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003806
Winson Chung997a9232013-07-24 15:33:46 -07003807 if (animateIcons) {
3808 // Animate to the correct page
3809 if (newShortcutsScreenId > -1) {
3810 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003811 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003812 final Runnable startBounceAnimRunnable = new Runnable() {
3813 public void run() {
3814 anim.playTogether(bounceAnims);
3815 anim.start();
3816 }
3817 };
Winson Chung997a9232013-07-24 15:33:46 -07003818 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003819 // We post the animation slightly delayed to prevent slowdowns
3820 // when we are loading right after we return to launcher.
3821 mWorkspace.postDelayed(new Runnable() {
3822 public void run() {
3823 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003824 mWorkspace.postDelayed(startBounceAnimRunnable,
3825 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003826 }
3827 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003828 } else {
3829 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003830 }
3831 }
Winson Chung64359a52013-07-08 17:17:08 -07003832 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003833 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003834 }
3835
Joe Onorato9c1289c2009-08-17 11:03:03 -04003836 /**
3837 * Implementation of the method from LauncherModel.Callbacks.
3838 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003839 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003840 Runnable r = new Runnable() {
3841 public void run() {
3842 bindFolders(folders);
3843 }
3844 };
3845 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003846 return;
3847 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003848 sFolders.clear();
3849 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003850 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851
3852 /**
3853 * Add the views for a widget to the workspace.
3854 *
3855 * Implementation of the method from LauncherModel.Callbacks.
3856 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003857 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003858 Runnable r = new Runnable() {
3859 public void run() {
3860 bindAppWidget(item);
3861 }
3862 };
3863 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003864 return;
3865 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003866
Daniel Sandler843e8602010-06-07 14:59:01 -04003867 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3868 if (DEBUG_WIDGETS) {
3869 Log.d(TAG, "bindAppWidget: " + item);
3870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003871 final Workspace workspace = mWorkspace;
3872
3873 final int appWidgetId = item.appWidgetId;
3874 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003875 if (DEBUG_WIDGETS) {
3876 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3877 }
3878
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3880
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003882 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003883
Adam Cohendcd297f2013-06-18 13:13:40 -07003884 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003885 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003886 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3887
Joe Onorato9c1289c2009-08-17 11:03:03 -04003888 workspace.requestLayout();
3889
Daniel Sandler843e8602010-06-07 14:59:01 -04003890 if (DEBUG_WIDGETS) {
3891 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3892 + (SystemClock.uptimeMillis()-start) + "ms");
3893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003894 }
3895
Adam Cohen1462de32012-07-24 22:34:36 -07003896 public void onPageBoundSynchronously(int page) {
3897 mSynchronouslyBoundPages.add(page);
3898 }
3899
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900 /**
3901 * Callback saying that there aren't any more items to bind.
3902 *
3903 * Implementation of the method from LauncherModel.Callbacks.
3904 */
Adam Cohene25af792013-06-06 23:08:25 -07003905 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003906 Runnable r = new Runnable() {
3907 public void run() {
3908 finishBindingItems(upgradePath);
3909 }
3910 };
3911 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003912 return;
3913 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003914 if (mSavedState != null) {
3915 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003916 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003917 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003918 mSavedState = null;
3919 }
3920
Adam Cohen1462de32012-07-24 22:34:36 -07003921 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003923 // If we received the result of any pending adds while the loader was running (e.g. the
3924 // widget configuration forced an orientation change), process them now.
3925 for (int i = 0; i < sPendingAddList.size(); i++) {
3926 completeAdd(sPendingAddList.get(i));
3927 }
3928 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003929
Winson Chungc51db6a2011-10-05 11:44:49 -07003930 // Update the market app icon as necessary (the other icons will be managed in response to
3931 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003932 if (!DISABLE_MARKET_BUTTON) {
3933 updateAppMarketIcon();
3934 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003935
Winson Chungf0c6ae02012-03-21 16:10:31 -07003936 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003937 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003938 mWorkspace.getUniqueComponents(true, null);
3939 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003940 }
Adam Cohen99894d92013-06-14 11:22:59 -07003941
Adam Cohen66a01fd2013-06-11 12:48:00 -07003942 mWorkspace.post(new Runnable() {
3943 @Override
3944 public void run() {
3945 onFinishBindingItems();
3946 }
3947 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003948 }
3949
Winson Chunga0b7e862013-09-05 16:03:15 -07003950 public boolean isAllAppsButtonRank(int rank) {
3951 if (mHotseat != null) {
3952 return mHotseat.isAllAppsButtonRank(rank);
3953 }
3954 return false;
3955 }
3956
Winson Chunga2413752012-04-03 14:22:34 -07003957 private boolean canRunNewAppsAnimation() {
3958 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3959 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3960 }
3961
Winson Chungc9168342013-06-26 14:54:55 -07003962 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3963 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3964 PropertyValuesHolder.ofFloat("alpha", 1f),
3965 PropertyValuesHolder.ofFloat("scaleX", 1f),
3966 PropertyValuesHolder.ofFloat("scaleY", 1f));
3967 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3968 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3969 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3970 return bounceAnim;
3971 }
3972
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003973 @Override
3974 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003975 boolean searchVisible = updateGlobalSearchIcon();
3976 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003977 if (mSearchDropTargetBar != null) {
3978 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3979 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003980 }
3981
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003982 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003983 * Add the icons for all apps.
3984 *
3985 * Implementation of the method from LauncherModel.Callbacks.
3986 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003987 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003988 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3989 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3990 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3991 getHotseat().addAllAppsFolder(mIconCache, apps,
3992 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3993 }
3994 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003995 }
Winson Chungc58497e2013-09-03 17:48:37 -07003996 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07003997 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07003998 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07003999 mAppsCustomizeContent.onPackagesUpdated(
4000 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004001 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004002 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003 }
4004
4005 /**
4006 * A package was updated.
4007 *
4008 * Implementation of the method from LauncherModel.Callbacks.
4009 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004010 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004011 Runnable r = new Runnable() {
4012 public void run() {
4013 bindAppsUpdated(apps);
4014 }
4015 };
4016 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004017 return;
4018 }
4019
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004020 if (mWorkspace != null) {
4021 mWorkspace.updateShortcuts(apps);
4022 }
Winson Chungc58497e2013-09-03 17:48:37 -07004023
4024 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4025 mAppsCustomizeContent != null) {
4026 mAppsCustomizeContent.updateApps(apps);
4027 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004028 }
4029
4030 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004031 * A package was uninstalled. We take both the super set of packageNames
4032 * in addition to specific applications to remove, the reason being that
4033 * this can be called when a package is updated as well. In that scenario,
4034 * we only remove specific components from the workspace, where as
4035 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036 *
4037 * Implementation of the method from LauncherModel.Callbacks.
4038 */
Winson Chung83892cc2013-05-01 16:53:33 -07004039 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004040 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004041 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004042 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004043 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004044 }
Winson Chungd64d1762013-08-20 14:37:16 -07004045 };
4046 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004047 return;
4048 }
4049
Winson Chungdf95eb12013-10-16 14:57:07 -07004050 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004051 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004052 }
4053 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004054 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004055 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004056
Winson Chunga1820962011-10-03 16:31:06 -07004057 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004058 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004059
Winson Chungdf95eb12013-10-16 14:57:07 -07004060 // Update AllApps
Winson Chungc58497e2013-09-03 17:48:37 -07004061 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4062 mAppsCustomizeContent != null) {
4063 mAppsCustomizeContent.removeApps(appInfos);
4064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 }
Joe Onoratobe386092009-11-17 17:32:16 -08004066
4067 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004068 * A number of packages were updated.
4069 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004070 private ArrayList<Object> mWidgetsAndShortcuts;
4071 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004072 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004073 bindPackagesUpdated(mWidgetsAndShortcuts);
4074 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004075 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004076 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004077 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4078 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4079 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004080 return;
4081 }
4082
Winson Chung64359a52013-07-08 17:17:08 -07004083 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004084 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4085 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004086 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004087 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004088 }
4089
Winson Chung400438b2011-01-16 17:53:48 -08004090 private int mapConfigurationOriActivityInfoOri(int configOri) {
4091 final Display d = getWindowManager().getDefaultDisplay();
4092 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4093 switch (d.getRotation()) {
4094 case Surface.ROTATION_0:
4095 case Surface.ROTATION_180:
4096 // We are currently in the same basic orientation as the natural orientation
4097 naturalOri = configOri;
4098 break;
4099 case Surface.ROTATION_90:
4100 case Surface.ROTATION_270:
4101 // We are currently in the other basic orientation to the natural orientation
4102 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4103 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4104 break;
4105 }
4106
4107 int[] oriMap = {
4108 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4109 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4110 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4111 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4112 };
4113 // Since the map starts at portrait, we need to offset if this device's natural orientation
4114 // is landscape.
4115 int indexOffset = 0;
4116 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4117 indexOffset = 1;
4118 }
4119 return oriMap[(d.getRotation() + indexOffset) % 4];
4120 }
Adam Cohen446e9402011-09-15 18:21:21 -07004121
Winson Chung4b919f82012-05-01 10:44:08 -07004122 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004123 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004124 getResources().getBoolean(R.bool.allow_rotation);
4125 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004126 }
Winson Chung4b919f82012-05-01 10:44:08 -07004127 public void lockScreenOrientation() {
4128 if (isRotationEnabled()) {
4129 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4130 .getConfiguration().orientation));
4131 }
4132 }
4133 public void unlockScreenOrientation(boolean immediate) {
4134 if (isRotationEnabled()) {
4135 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004136 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004137 } else {
4138 mHandler.postDelayed(new Runnable() {
4139 public void run() {
4140 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4141 }
4142 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004143 }
Winson Chung4b919f82012-05-01 10:44:08 -07004144 }
Winson Chung400438b2011-01-16 17:53:48 -08004145 }
4146
Winson Chung82f55532011-08-09 14:14:23 -07004147 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004148 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004149 if (DISABLE_CLINGS) {
4150 return false;
4151 }
4152
Winson Chung3a6e7f32013-10-09 15:50:52 -07004153 // For now, limit only to phones
4154 LauncherAppState app = LauncherAppState.getInstance();
4155 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4156 if (grid.isTablet()) {
4157 return false;
4158 }
Winson Chung13dbfe12013-10-31 11:42:39 -07004159 if (grid.isLandscape) {
4160 return false;
4161 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004162
Brett Chabot2a9e2282011-08-23 15:03:13 -07004163 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004164 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004165
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004166 // Disable clings for accessibility when explore by touch is enabled
4167 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4168 ACCESSIBILITY_SERVICE);
4169 if (a11yManager.isTouchExplorationEnabled()) {
4170 return false;
4171 }
4172
Michael Jurkae233a8b2013-03-19 13:49:20 +01004173 // Restricted secondary users (child mode) will potentially have very few apps
4174 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004175// boolean supportsLimitedUsers =
4176// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4177// Account[] accounts = AccountManager.get(this).getAccounts();
4178// if (supportsLimitedUsers && accounts.length == 0) {
4179// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4180// Bundle restrictions = um.getUserRestrictions();
4181// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4182// return false;
4183// }
4184// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004185 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004186 }
Michael Jurka22143132012-10-04 17:42:38 +02004187
Winson Chungfa545132013-09-26 17:45:32 -07004188 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004189 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004190 Cling cling = (Cling) findViewById(clingId);
4191 View scrim = null;
4192 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004193 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004194 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004195 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004196 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004197 cling.show(animate, SHOW_CLING_DURATION);
4198
4199 if (dimNavBarVisibilty) {
4200 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4201 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004202 }
4203 }
4204 return cling;
4205 }
Michael Jurka22143132012-10-04 17:42:38 +02004206
Winson Chungaf40f202013-09-18 18:26:31 -07004207 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4208 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004209 // To catch cases where siblings of top-level views are made invisible, just check whether
4210 // the cling is directly set to GONE before dismissing it.
4211 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004212 final Runnable cleanUpClingCb = new Runnable() {
4213 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004214 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004215 // We should update the shared preferences on a background thread
4216 new Thread("dismissClingThread") {
4217 public void run() {
4218 SharedPreferences.Editor editor = mSharedPrefs.edit();
4219 editor.putBoolean(flag, true);
4220 editor.commit();
4221 }
4222 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004223 if (postAnimationCb != null) {
4224 postAnimationCb.run();
4225 }
4226 }
4227 };
4228 if (duration <= 0) {
4229 cleanUpClingCb.run();
4230 } else {
4231 cling.hide(duration, cleanUpClingCb);
4232 }
Michael Jurka22143132012-10-04 17:42:38 +02004233 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004234
4235 if (restoreNavBarVisibilty) {
4236 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4237 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4238 }
Winson Chung82f55532011-08-09 14:14:23 -07004239 }
4240 }
Michael Jurka22143132012-10-04 17:42:38 +02004241
Winson Chung9d9d74f2011-09-19 11:49:12 -07004242 private void removeCling(int id) {
4243 final View cling = findViewById(id);
4244 if (cling != null) {
4245 final ViewGroup parent = (ViewGroup) cling.getParent();
4246 parent.post(new Runnable() {
4247 @Override
4248 public void run() {
4249 parent.removeView(cling);
4250 }
4251 });
Michael Jurka22143132012-10-04 17:42:38 +02004252 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004253 }
4254 }
Michael Jurka974c3862012-05-22 22:00:31 -07004255
4256 private boolean skipCustomClingIfNoAccounts() {
4257 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4258 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4259 if (customCling) {
4260 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004261 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004262 Account[] accounts = am.getAccountsByType("com.google");
4263 return accounts.length == 0;
4264 }
4265 return false;
4266 }
4267
Winson Chung82e5c982013-10-09 12:04:50 -07004268 public void updateCustomContentHintVisibility() {
4269 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4270 String ccHintStr = getFirstRunCustomContentHint();
4271
4272 if (mWorkspace.hasCustomContent()) {
4273 // Show the custom content hint if ccHintStr is not empty
4274 if (cling != null) {
4275 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4276 }
4277 } else {
4278 // Hide the custom content hint
4279 if (cling != null) {
4280 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4281 }
4282 }
4283 }
4284
4285 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4286 boolean animate) {
4287 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4288 if (ccHint != null) {
4289 if (visible && !ccHintStr.isEmpty()) {
4290 ccHint.setText(ccHintStr);
4291 ccHint.setVisibility(View.VISIBLE);
4292 if (animate) {
4293 ccHint.setAlpha(0f);
4294 ccHint.animate().alpha(1f)
4295 .setDuration(SHOW_CLING_DURATION)
4296 .start();
4297 } else {
4298 ccHint.setAlpha(1f);
4299 }
4300 } else {
4301 if (animate) {
4302 ccHint.animate().alpha(0f)
4303 .setDuration(SHOW_CLING_DURATION)
4304 .setListener(new AnimatorListenerAdapter() {
4305 @Override
4306 public void onAnimationEnd(Animator animation) {
4307 ccHint.setVisibility(View.GONE);
4308 }
4309 })
4310 .start();
4311 } else {
4312 ccHint.setAlpha(0f);
4313 ccHint.setVisibility(View.GONE);
4314 }
4315 }
4316 }
4317 }
4318
Winson Chungaf40f202013-09-18 18:26:31 -07004319 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004320 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004321 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004322 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004323 // If we're not using the default workspace layout, replace workspace cling
4324 // with a custom workspace cling (usually specified in an overlay)
4325 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004326 if (!DISABLE_CUSTOM_CLINGS) {
4327 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4328 getResources().getBoolean(R.bool.config_useCustomClings)) {
4329 // Use a custom cling
4330 View cling = findViewById(R.id.workspace_cling);
4331 ViewGroup clingParent = (ViewGroup) cling.getParent();
4332 int clingIndex = clingParent.indexOfChild(cling);
4333 clingParent.removeViewAt(clingIndex);
4334 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4335 clingParent.addView(customCling, clingIndex);
4336 customCling.setId(R.id.workspace_cling);
4337 }
4338 }
4339 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4340 if (cling != null) {
4341 String sbHintStr = getFirstRunClingSearchBarHint();
4342 String ccHintStr = getFirstRunCustomContentHint();
4343 if (!sbHintStr.isEmpty()) {
4344 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4345 sbHint.setText(sbHintStr);
4346 sbHint.setVisibility(View.VISIBLE);
4347 }
Winson Chung82e5c982013-10-09 12:04:50 -07004348 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004349 }
Winson Chungfa545132013-09-26 17:45:32 -07004350 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004351 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004352 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004353 }
4354 }
Winson Chungaf40f202013-09-18 18:26:31 -07004355
Winson Chunge6eabff2013-09-24 11:01:23 -07004356 protected String getFirstRunClingSearchBarHint() {
4357 return "";
4358 }
4359 protected String getFirstRunCustomContentHint() {
4360 return "";
4361 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004362 protected int getFirstRunFocusedHotseatAppDrawableId() {
4363 return -1;
4364 }
4365 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4366 return null;
4367 }
4368 protected int getFirstRunFocusedHotseatAppRank() {
4369 return -1;
4370 }
4371 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4372 return "";
4373 }
4374 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4375 return "";
4376 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004377
Winson Chungaf40f202013-09-18 18:26:31 -07004378 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004379 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004380 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004381 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004382 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4383
4384 // Set the focused hotseat app if there is one
4385 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4386 getFirstRunFocusedHotseatAppRank(),
4387 getFirstRunFocusedHotseatAppComponentName(),
4388 getFirstRunFocusedHotseatAppBubbleTitle(),
4389 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004390 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004391 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004392 }
4393 }
4394 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004395 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004396 if (isClingsEnabled() &&
4397 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004398 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4399 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004400 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004401 } else {
4402 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004403 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004404 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004405 }
Michael Jurka104c4562013-07-08 18:03:46 -07004406 protected SharedPreferences getSharedPrefs() {
4407 return mSharedPrefs;
4408 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004409 public boolean isFolderClingVisible() {
4410 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004411 if (cling != null) {
4412 return cling.getVisibility() == View.VISIBLE;
4413 }
4414 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004415 }
Winson Chungaf40f202013-09-18 18:26:31 -07004416 public void dismissFirstRunCling(View v) {
4417 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4418 Runnable cb = new Runnable() {
4419 public void run() {
4420 // Show the workspace cling next
4421 showFirstRunWorkspaceCling();
4422 }
4423 };
4424 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4425 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004426
4427 // Fade out the search bar for the workspace cling coming up
4428 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004429 }
Winson Chung82f55532011-08-09 14:14:23 -07004430 public void dismissWorkspaceCling(View v) {
4431 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004432 Runnable cb = null;
4433 if (v == null) {
4434 cb = new Runnable() {
4435 public void run() {
4436 mWorkspace.enterOverviewMode();
4437 }
4438 };
4439 }
4440 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4441 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004442
4443 // Fade in the search bar
4444 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004445 }
4446 public void dismissFolderCling(View v) {
4447 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004448 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4449 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004450 }
4451
Winson Chung80baf5a2010-08-09 16:03:15 -07004452 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004453 * Prints out out state for debugging.
4454 */
4455 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004456 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004457 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004458 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4459 Log.d(TAG, "mRestoring=" + mRestoring);
4460 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4461 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004462 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004463 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004464
Winson Chung785d2eb2011-04-14 16:08:02 -07004465 if (mAppsCustomizeContent != null) {
4466 mAppsCustomizeContent.dumpState();
4467 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004468 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004469 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004470
4471 @Override
4472 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4473 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004474 synchronized (sDumpLogs) {
4475 writer.println(" ");
4476 writer.println("Debug logs: ");
4477 for (int i = 0; i < sDumpLogs.size(); i++) {
4478 writer.println(" " + sDumpLogs.get(i));
4479 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004480 }
4481 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004482
4483 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004484 if (DEBUG_DUMP_LOG) {
4485 synchronized (sDumpLogs) {
4486 Log.d(TAG, "");
4487 Log.d(TAG, "*********************");
4488 Log.d(TAG, "Launcher debug logs: ");
4489 for (int i = 0; i < sDumpLogs.size(); i++) {
4490 Log.d(TAG, " " + sDumpLogs.get(i));
4491 }
4492 Log.d(TAG, "*********************");
4493 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004494 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004495 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004496 }
4497
4498 public static void addDumpLog(String tag, String log, boolean debugLog) {
4499 if (debugLog) {
4500 Log.d(tag, log);
4501 }
Winson Chungede41292013-09-19 16:27:36 -07004502 if (DEBUG_DUMP_LOG) {
4503 sDateStamp.setTime(System.currentTimeMillis());
4504 synchronized (sDumpLogs) {
4505 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004506 }
Winson Chungede41292013-09-19 16:27:36 -07004507 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004508 }
4509
Winson Chungede41292013-09-19 16:27:36 -07004510 public void dumpLogsToLocalData() {
4511 if (DEBUG_DUMP_LOG) {
4512 new Thread("DumpLogsToLocalData") {
4513 @Override
4514 public void run() {
4515 boolean success = false;
4516 sDateStamp.setTime(sRunStart);
4517 String FILENAME = sDateStamp.getMonth() + "-"
4518 + sDateStamp.getDay() + "_"
4519 + sDateStamp.getHours() + "-"
4520 + sDateStamp.getMinutes() + "_"
4521 + sDateStamp.getSeconds() + ".txt";
4522
4523 FileOutputStream fos = null;
4524 File outFile = null;
4525 try {
4526 outFile = new File(getFilesDir(), FILENAME);
4527 outFile.createNewFile();
4528 fos = new FileOutputStream(outFile);
4529 } catch (Exception e) {
4530 e.printStackTrace();
4531 }
4532 if (fos != null) {
4533 PrintWriter writer = new PrintWriter(fos);
4534
4535 writer.println(" ");
4536 writer.println("Debug logs: ");
4537 synchronized (sDumpLogs) {
4538 for (int i = 0; i < sDumpLogs.size(); i++) {
4539 writer.println(" " + sDumpLogs.get(i));
4540 }
4541 }
4542 writer.close();
4543 }
4544 try {
4545 if (fos != null) {
4546 fos.close();
4547 success = true;
4548 }
4549 } catch (IOException e) {
4550 e.printStackTrace();
4551 }
4552 }
4553 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004554 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004555 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004556}
Michael Jurka2763be32011-02-24 11:19:57 -08004557
Michael Jurkaabded662011-03-04 12:06:57 -08004558interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004559 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004560 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004561 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004562 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004563 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004564}