blob: 0c14ed93cea33632c3a5b73fac4f77979a37e035 [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 Chung8e15fdf2013-11-11 15:47:45 -08001194 if (!mWorkspace.isSwitchingState()) {
1195 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1196 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001197 }
1198 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001199 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1200
1201 View wallpaperButton = findViewById(R.id.wallpaper_button);
1202 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001203 @Override
1204 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001205 if (!mWorkspace.isSwitchingState()) {
1206 startWallpaper();
1207 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001208 }
1209 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001210 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1211
1212 View settingsButton = findViewById(R.id.settings_button);
1213 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001214 @Override
1215 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001216 if (!mWorkspace.isSwitchingState()) {
1217 startSettings();
1218 }
Adam Cohen30bacb22013-08-29 14:25:25 -07001219 }
1220 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001221 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001222 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001223
Winson Chung4c98d922011-05-31 16:50:48 -07001224 // Setup the workspace
1225 mWorkspace.setHapticFeedbackEnabled(false);
1226 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001227 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001228 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001229
Winson Chungf0ea4d32011-06-06 14:27:16 -07001230 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001231 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001232
Winson Chungf0ea4d32011-06-06 14:27:16 -07001233 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001234 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001235 mAppsCustomizeContent = (AppsCustomizePagedView)
1236 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1237 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001238
Winson Chung3d503fb2011-07-13 17:25:49 -07001239 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001240 dragController.setDragScoller(mWorkspace);
1241 dragController.setScrollView(mDragLayer);
1242 dragController.setMoveTarget(mWorkspace);
1243 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001244 if (mSearchDropTargetBar != null) {
1245 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001246 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001247
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001248 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001249 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001250 mWeightWatcher = new WeightWatcher(this);
1251 mWeightWatcher.setAlpha(0.5f);
1252 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001253 new FrameLayout.LayoutParams(
1254 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001255 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001256 Gravity.BOTTOM)
1257 );
Adam Cohen39a06042013-07-19 14:30:12 -07001258
1259 boolean show = shouldShowWeightWatcher();
1260 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
1263
1264 /**
1265 * Creates a view representing a shortcut.
1266 *
1267 * @param info The data structure describing the shortcut.
1268 *
1269 * @return A View inflated from R.layout.application.
1270 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001271 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001273 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
1275
1276 /**
1277 * Creates a view representing a shortcut inflated from the specified resource.
1278 *
1279 * @param layoutResId The id of the XML layout used to create the shortcut.
1280 * @param parent The group the shortcut belongs to.
1281 * @param info The data structure describing the shortcut.
1282 *
1283 * @return A View inflated from layoutResId.
1284 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001285 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001286 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1287 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 return favorite;
1290 }
1291
1292 /**
1293 * Add an application shortcut to the workspace.
1294 *
1295 * @param data The intent describing the application.
1296 * @param cellInfo The position on screen where to create the shortcut.
1297 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001298 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001299 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001300 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001301
Adam Cohenfbba09b2011-07-18 21:43:05 -07001302 // First we check if we already know the exact location where we want to add this item.
1303 if (cellX >= 0 && cellY >= 0) {
1304 cellXY[0] = cellX;
1305 cellXY[1] = cellY;
1306 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001307 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001308 return;
1309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001311 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001312
Romain Guy73b979d2009-06-09 12:57:21 -07001313 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001314 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001316 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001317 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001318 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001319 } else {
1320 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322 }
Romain Guycbb89e42009-06-08 15:52:54 -07001323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 /**
1325 * Add a shortcut to the workspace.
1326 *
1327 * @param data The intent describing the shortcut.
1328 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001330 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001331 int cellY) {
1332 int[] cellXY = mTmpAddItemCellCoordinates;
1333 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001334 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001335
Michael Jurkad3ef3062010-11-23 16:23:58 -08001336 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001337
Adam Cohen558baaf2011-08-15 15:22:57 -07001338 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001339 if (info == null) {
1340 return;
1341 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001342 final View view = createShortcut(info);
1343
Adam Cohenfbba09b2011-07-18 21:43:05 -07001344 // First we check if we already know the exact location where we want to add this item.
1345 if (cellX >= 0 && cellY >= 0) {
1346 cellXY[0] = cellX;
1347 cellXY[1] = cellY;
1348 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001349
1350 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001351 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001352 true, null,null)) {
1353 return;
1354 }
1355 DragObject dragObject = new DragObject();
1356 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001357 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1358 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001359 return;
1360 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001361 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001362 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001364 foundCellSpan = (result != null);
1365 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001366 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001367 }
1368
1369 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001370 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001371 return;
1372 }
1373
Adam Cohendcd297f2013-06-18 13:13:40 -07001374 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375
1376 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001377 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001378 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
1380 }
1381
Adam Cohen2f093b62012-04-30 18:59:53 -07001382 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1383 int minHeight) {
1384 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001385 // We want to account for the extra amount of padding that we are adding to the widget
1386 // to ensure that it gets the full amount of space that it has requested
1387 int requiredWidth = minWidth + padding.left + padding.right;
1388 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001389 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001390 }
1391
Adam Cohen2f093b62012-04-30 18:59:53 -07001392 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1393 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001394 }
1395
Adam Cohen2f093b62012-04-30 18:59:53 -07001396 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1397 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001398 }
1399
Adam Cohen2f093b62012-04-30 18:59:53 -07001400 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1401 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001402 }
1403
Adam Cohen2f093b62012-04-30 18:59:53 -07001404 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1405 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001406 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001407 }
1408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001410 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001412 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001413 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001415 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001416 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1417 if (appWidgetInfo == null) {
1418 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1419 }
Romain Guycbb89e42009-06-08 15:52:54 -07001420
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001421 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001422 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001423
Adam Cohen2f093b62012-04-30 18:59:53 -07001424 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1425 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001428 // We have saved the position to which the widget was dragged-- this really only matters
1429 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001430 int[] cellXY = mTmpAddItemCellCoordinates;
1431 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001432 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001433 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001434 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1435 cellXY[0] = mPendingAddInfo.cellX;
1436 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001437 spanXY[0] = mPendingAddInfo.spanX;
1438 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001439 foundCellSpan = true;
1440 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001441 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001442 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001443 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1444 spanXY[1], cellXY, finalSpan);
1445 spanXY[0] = finalSpan[0];
1446 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001447 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001448 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001449 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001450 }
1451
Michael Jurka0280c3b2010-09-17 15:00:07 -07001452 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001453 if (appWidgetId != -1) {
1454 // Deleting an app widget ID is a void call but writes to disk before returning
1455 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001456 new Thread("deleteAppWidgetId") {
1457 public void run() {
1458 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1459 }
1460 }.start();
1461 }
Winson Chung93eef082012-03-23 15:59:27 -07001462 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001463 return;
1464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001466 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001467 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1468 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001469 launcherInfo.spanX = spanXY[0];
1470 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001471 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1472 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001475 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476
1477 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001478 if (hostView == null) {
1479 // Perform actual inflation because we're live
1480 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1481 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1482 } else {
1483 // The AppWidgetHostView has already been inflated and instantiated
1484 launcherInfo.hostView = hostView;
1485 }
Romain Guycbb89e42009-06-08 15:52:54 -07001486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001488 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001489 launcherInfo.notifyWidgetSizeChanged(this);
1490
Adam Cohendcd297f2013-06-18 13:13:40 -07001491 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001492 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001493
1494 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001496 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
Romain Guycbb89e42009-06-08 15:52:54 -07001498
Adam Cohended9f8d2010-11-03 13:25:16 -07001499 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1500 @Override
1501 public void onReceive(Context context, Intent intent) {
1502 final String action = intent.getAction();
1503 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1504 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001505 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001506 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001507
Winson Chungc100e8e2011-08-09 16:02:43 -07001508 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001509 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001510 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001511 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001512 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001513 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1514 mUserPresent = true;
1515 updateRunning();
1516 }
1517 }
1518 };
1519
1520 @Override
1521 public void onAttachedToWindow() {
1522 super.onAttachedToWindow();
1523
1524 // Listen for broadcasts related to user-presence
1525 final IntentFilter filter = new IntentFilter();
1526 filter.addAction(Intent.ACTION_SCREEN_OFF);
1527 filter.addAction(Intent.ACTION_USER_PRESENT);
1528 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001529 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001530 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001531 mVisible = true;
1532 }
1533
1534 @Override
1535 public void onDetachedFromWindow() {
1536 super.onDetachedFromWindow();
1537 mVisible = false;
1538
Adam Cohend113e0c2010-11-11 10:48:05 -08001539 if (mAttached) {
1540 unregisterReceiver(mReceiver);
1541 mAttached = false;
1542 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001543 updateRunning();
1544 }
1545
1546 public void onWindowVisibilityChanged(int visibility) {
1547 mVisible = visibility == View.VISIBLE;
1548 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001549 // The following code used to be in onResume, but it turns out onResume is called when
1550 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1551 // is a more appropriate event to handle
1552 if (mVisible) {
1553 mAppsCustomizeTabHost.onWindowVisible();
1554 if (!mWorkspaceLoading) {
1555 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001556 // We want to let Launcher draw itself at least once before we force it to build
1557 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001558 // apps is nice and speedy.
1559 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001560 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001561 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001562 if (mStarted) return;
1563 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001564 // We delay the layer building a bit in order to give
1565 // other message processing a time to run. In particular
1566 // this avoids a delay in hiding the IME if it was
1567 // currently shown, because doing that may involve
1568 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001569 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001570 final ViewTreeObserver.OnDrawListener listener = this;
1571 mWorkspace.post(new Runnable() {
1572 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001573 if (mWorkspace != null &&
1574 mWorkspace.getViewTreeObserver() != null) {
1575 mWorkspace.getViewTreeObserver().
1576 removeOnDrawListener(listener);
1577 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001578 }
1579 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001580 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001581 }
1582 });
1583 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001584 // When Launcher comes back to foreground, a different Activity might be responsible for
1585 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001586 if (!DISABLE_MARKET_BUTTON) {
1587 updateAppMarketIcon();
1588 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001589 clearTypedText();
1590 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001591 }
1592
1593 private void sendAdvanceMessage(long delay) {
1594 mHandler.removeMessages(ADVANCE_MSG);
1595 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1596 mHandler.sendMessageDelayed(msg, delay);
1597 mAutoAdvanceSentTime = System.currentTimeMillis();
1598 }
1599
1600 private void updateRunning() {
1601 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1602 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1603 mAutoAdvanceRunning = autoAdvanceRunning;
1604 if (autoAdvanceRunning) {
1605 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1606 sendAdvanceMessage(delay);
1607 } else {
1608 if (!mWidgetsToAdvance.isEmpty()) {
1609 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1610 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1611 }
1612 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001613 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 }
1615 }
1616 }
1617
1618 private final Handler mHandler = new Handler() {
1619 @Override
1620 public void handleMessage(Message msg) {
1621 if (msg.what == ADVANCE_MSG) {
1622 int i = 0;
1623 for (View key: mWidgetsToAdvance.keySet()) {
1624 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1625 final int delay = mAdvanceStagger * i;
1626 if (v instanceof Advanceable) {
1627 postDelayed(new Runnable() {
1628 public void run() {
1629 ((Advanceable) v).advance();
1630 }
1631 }, delay);
1632 }
1633 i++;
1634 }
1635 sendAdvanceMessage(mAdvanceInterval);
1636 }
1637 }
1638 };
1639
1640 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001641 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001642 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1643 if (v instanceof Advanceable) {
1644 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001645 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001646 updateRunning();
1647 }
1648 }
1649
1650 void removeWidgetToAutoAdvance(View hostView) {
1651 if (mWidgetsToAdvance.containsKey(hostView)) {
1652 mWidgetsToAdvance.remove(hostView);
1653 updateRunning();
1654 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001655 }
1656
Joe Onorato9c1289c2009-08-17 11:03:03 -04001657 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001658 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 launcherInfo.hostView = null;
1660 }
1661
Winson Chung93eef082012-03-23 15:59:27 -07001662 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1663 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1664 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 }
1666
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001667 public LauncherAppWidgetHost getAppWidgetHost() {
1668 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 }
Romain Guycbb89e42009-06-08 15:52:54 -07001670
Winson Chunga9abd0e2010-10-27 17:18:37 -07001671 public LauncherModel getModel() {
1672 return mModel;
1673 }
1674
Bjorn Bringertc459e522013-06-07 19:36:01 +01001675 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001676 getWindow().closeAllPanels();
1677
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001678 // Whatever we were doing is hereby canceled.
1679 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001680 }
1681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 @Override
1683 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001684 long startTime = 0;
1685 if (DEBUG_RESUME_TIME) {
1686 startTime = System.currentTimeMillis();
1687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 super.onNewIntent(intent);
1689
1690 // Close the menu
1691 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001692 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001693 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001694
Adam Cohened307df2013-10-02 09:37:31 -07001695 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1696 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1697 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001698
Adam Cohen6fecd412013-10-02 17:41:50 -07001699 if (mWorkspace == null) {
1700 // Can be cases where mWorkspace is null, this prevents a NPE
1701 return;
1702 }
1703 Folder openFolder = mWorkspace.getOpenFolder();
1704 // In all these cases, only animate if we're already on home
1705 mWorkspace.exitWidgetResizeMode();
1706 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1707 openFolder == null) {
1708 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001709 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001710
Adam Cohen6fecd412013-10-02 17:41:50 -07001711 closeFolder();
1712 exitSpringLoadedDragMode();
1713
1714 // If we are already on home, then just animate back to the workspace,
1715 // otherwise, just wait until onResume to set the state back to Workspace
1716 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001717 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001718 } else {
1719 mOnResumeState = State.WORKSPACE;
1720 }
1721
1722 final View v = getWindow().peekDecorView();
1723 if (v != null && v.getWindowToken() != null) {
1724 InputMethodManager imm = (InputMethodManager)getSystemService(
1725 INPUT_METHOD_SERVICE);
1726 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1727 }
1728
1729 // Reset the apps customize page
1730 if (mAppsCustomizeTabHost != null) {
1731 mAppsCustomizeTabHost.reset();
1732 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 }
Adam Cohened307df2013-10-02 09:37:31 -07001734
Michael Jurka447bf842013-05-15 14:52:15 +02001735 if (DEBUG_RESUME_TIME) {
1736 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 }
1739
1740 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001741 public void onRestoreInstanceState(Bundle state) {
1742 super.onRestoreInstanceState(state);
1743 for (int page: mSynchronouslyBoundPages) {
1744 mWorkspace.restoreInstanceStateForChild(page);
1745 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 }
1747
1748 @Override
1749 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001750 if (mWorkspace.getChildCount() > 0) {
1751 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getRestorePage());
1752 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001753 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754
Michael Jurka883f55b2010-10-21 15:47:14 -07001755 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001756 // We close any open folder since it will not be re-opened, and we need to make sure
1757 // this state is reflected.
1758 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759
Adam Cohendcd297f2013-06-18 13:13:40 -07001760 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001761 mWaitingForResult) {
1762 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001763 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001764 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1765 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001766 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1767 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1768 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001769 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 }
1771
1772 if (mFolderInfo != null && mWaitingForResult) {
1773 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1774 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1775 }
Michael Jurka946ad472010-07-09 18:05:18 -07001776
Winson Chung785d2eb2011-04-14 16:08:02 -07001777 // Save the current AppsCustomize tab
1778 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001779 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1780 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001781 if (currentTabTag != null) {
1782 outState.putString("apps_customize_currentTab", currentTabTag);
1783 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001784 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1785 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001786 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 }
1788
1789 @Override
1790 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001792
Winson Chunge7a03942011-08-05 15:05:12 -07001793 // Remove all pending runnables
1794 mHandler.removeMessages(ADVANCE_MSG);
1795 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001796 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001797
Winson Chungcd2b0142011-06-08 16:02:26 -07001798 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001799 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001800 mModel.stopLoader();
1801 app.setLauncher(null);
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001804 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001806 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001808 mAppWidgetHost = null;
1809
1810 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811
1812 TextKeyListener.getInstance().release();
1813
Winson Chung81b52252012-08-27 15:34:29 -07001814 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1815 // to prevent leaking Launcher activities on orientation change.
1816 if (mModel != null) {
1817 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1818 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001819
1820 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001821 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001822
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001823 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001824 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1825 mWorkspace.removeAllViews();
1826 mWorkspace = null;
1827 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001828
Michael Jurka2ecf9952012-06-18 12:52:28 -07001829 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 }
1831
Adam Cohena9cf38f2011-05-02 15:36:58 -07001832 public DragController getDragController() {
1833 return mDragController;
1834 }
1835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 @Override
1837 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001838 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 super.startActivityForResult(intent, requestCode);
1840 }
1841
Winson Chung70d72102011-08-12 11:24:35 -07001842 /**
1843 * Indicates that we want global search for this activity by setting the globalSearch
1844 * argument for {@link #startSearch} to true.
1845 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001847 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001849
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001850 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001851
Karl Rosaen138a0412009-04-23 19:00:21 -07001852 if (initialQuery == null) {
1853 // Use any text typed in the launcher as the initial query
1854 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 if (appSearchData == null) {
1857 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001858 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
Winson Chungadf0c182012-08-23 14:59:07 -07001860 Rect sourceBounds = new Rect();
1861 if (mSearchDropTargetBar != null) {
1862 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1863 }
Romain Guycbb89e42009-06-08 15:52:54 -07001864
Bjorn Bringertc459e522013-06-07 19:36:01 +01001865 startSearch(initialQuery, selectInitialQuery,
1866 appSearchData, sourceBounds);
1867 }
1868
1869 public void startSearch(String initialQuery,
1870 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001871 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001872 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001873 }
1874
1875 /**
1876 * Starts the global search activity. This code is a copied from SearchManager
1877 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001878 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001879 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001880 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001881 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1882 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1883 if (globalSearchActivity == null) {
1884 Log.w(TAG, "No global search activity found.");
1885 return;
1886 }
1887 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1888 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1889 intent.setComponent(globalSearchActivity);
1890 // Make sure that we have a Bundle to put source in
1891 if (appSearchData == null) {
1892 appSearchData = new Bundle();
1893 } else {
1894 appSearchData = new Bundle(appSearchData);
1895 }
1896 // Set source to package name of app that starts global search, if not set already.
1897 if (!appSearchData.containsKey("source")) {
1898 appSearchData.putString("source", getPackageName());
1899 }
1900 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1901 if (!TextUtils.isEmpty(initialQuery)) {
1902 intent.putExtra(SearchManager.QUERY, initialQuery);
1903 }
1904 if (selectInitialQuery) {
1905 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1906 }
1907 intent.setSourceBounds(sourceBounds);
1908 try {
1909 startActivity(intent);
1910 } catch (ActivityNotFoundException ex) {
1911 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1912 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
1914
Winson Chung70d72102011-08-12 11:24:35 -07001915 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001916 public boolean onPrepareOptionsMenu(Menu menu) {
1917 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001918 if (!mWorkspace.isInOverviewMode()) {
1919 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001920 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001921 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001924 @Override
1925 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001926 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001927 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001928 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001929 }
1930
Joe Onorato9c1289c2009-08-17 11:03:03 -04001931 public boolean isWorkspaceLocked() {
1932 return mWorkspaceLoading || mWaitingForResult;
1933 }
1934
Michael Jurka0280c3b2010-09-17 15:00:07 -07001935 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001936 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001937 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001938 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1939 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001940 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001941 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001942 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001943
Adam Cohenad4e15c2013-10-17 16:21:35 -07001944 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1945 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
1946 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1947 }
1948
1949 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1950 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
1951 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001952 if (appWidgetInfo.configure != null) {
1953 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08001954 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07001955
Bjorn Bringert7984c942009-12-09 15:38:25 +00001956 // Launch over to configure widget, if needed
1957 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001958 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001959 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001960 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001962 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001963 Runnable onComplete = new Runnable() {
1964 @Override
1965 public void run() {
1966 // Exit spring loaded mode if necessary after adding the widget
1967 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1968 null);
1969 }
1970 };
Adam Cohendcd297f2013-06-18 13:13:40 -07001971 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001972 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001973 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975 }
Romain Guycbb89e42009-06-08 15:52:54 -07001976
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001977 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001978 // Close any folders that may be open.
1979 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001980 mWorkspace.moveToCustomContentScreen(animate);
1981 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001982 /**
1983 * Process a shortcut drop.
1984 *
1985 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001986 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001987 * @param cell The cell it should be added to, optional
1988 * @param position The location on the screen where it was dropped, optional
1989 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001992 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001993 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001994 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001995 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001996
1997 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001998 mPendingAddInfo.cellX = cell[0];
1999 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002000 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002001
2002 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2003 createShortcutIntent.setComponent(componentName);
2004 processShortcut(createShortcutIntent);
2005 }
2006
Adam Cohenfbba09b2011-07-18 21:43:05 -07002007 /**
2008 * Process a widget drop.
2009 *
2010 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002011 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002012 * @param cell The cell it should be added to, optional
2013 * @param position The location on the screen where it was dropped, optional
2014 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002015 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002016 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002017 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002018 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002019 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002020 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002021 mPendingAddInfo.minSpanX = info.minSpanX;
2022 mPendingAddInfo.minSpanY = info.minSpanY;
2023
Adam Cohenfbba09b2011-07-18 21:43:05 -07002024 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002025 mPendingAddInfo.cellX = cell[0];
2026 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002027 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002028 if (span != null) {
2029 mPendingAddInfo.spanX = span[0];
2030 mPendingAddInfo.spanY = span[1];
2031 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002032
Adam Cohened66b2b2012-01-23 17:28:51 -08002033 AppWidgetHostView hostView = info.boundWidget;
2034 int appWidgetId;
2035 if (hostView != null) {
2036 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002037 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002038 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002039 // In this case, we either need to start an activity to get permission to bind
2040 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002041 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002042 Bundle options = info.bindOptions;
2043
2044 boolean success = false;
2045 if (options != null) {
2046 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2047 info.componentName, options);
2048 } else {
2049 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2050 info.componentName);
2051 }
2052 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002053 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002054 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002055 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002056 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2057 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2058 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002059 // TODO: we need to make sure that this accounts for the options bundle.
2060 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002061 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2062 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002063 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002064 }
2065
Joe Onoratodeb98af2010-02-19 14:59:39 -08002066 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002067 // Handle case where user selected "Applications"
2068 String applicationName = getResources().getString(R.string.group_applications);
2069 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002070
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002071 if (applicationName != null && applicationName.equals(shortcutName)) {
2072 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2073 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002074
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002075 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2076 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002077 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002078 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002079 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002080 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 }
2083
Winson Chung24ab2f12010-09-16 14:10:47 -07002084 void processWallpaper(Intent intent) {
2085 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2086 }
2087
Adam Cohendcd297f2013-06-18 13:13:40 -07002088 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002089 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002090 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 folderInfo.title = getText(R.string.folder_name);
2092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002094 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002095 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002096 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097
2098 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002099 FolderIcon newFolder =
2100 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002101 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002102 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002103 // Force measure the new folder icon
2104 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2105 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002106 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
Romain Guycbb89e42009-06-08 15:52:54 -07002108
Joe Onorato9c1289c2009-08-17 11:03:03 -04002109 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002110 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002111 }
2112
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002113 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002115 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002116 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
2118
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002119 protected ComponentName getWallpaperPickerComponent() {
2120 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2121 }
2122
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002123 /**
2124 * Registers various content observers. The current implementation registers
2125 * only a favorites observer to keep track of the favorites applications.
2126 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002127 private void registerContentObservers() {
2128 ContentResolver resolver = getContentResolver();
2129 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2130 true, mWidgetObserver);
2131 }
2132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 @Override
2134 public boolean dispatchKeyEvent(KeyEvent event) {
2135 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2136 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002138 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002139 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002140 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002141 dumpState();
2142 return true;
2143 }
2144 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002145 }
2146 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2147 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002148 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 return true;
2150 }
2151 }
2152
2153 return super.dispatchKeyEvent(event);
2154 }
2155
Joe Onorato88ec0992009-11-19 13:16:06 -08002156 @Override
2157 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002158 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002159 if (mAppsCustomizeContent.getContentType() ==
2160 AppsCustomizePagedView.ContentType.Applications) {
2161 showWorkspace(true);
2162 } else {
2163 showOverviewMode(true);
2164 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002165 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002166 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002167 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002168 Folder openFolder = mWorkspace.getOpenFolder();
2169 if (openFolder.isEditingName()) {
2170 openFolder.dismissEditingName();
2171 } else {
2172 closeFolder();
2173 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002174 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002175 mWorkspace.exitWidgetResizeMode();
2176
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002177 // Back button is a no-op here, but give at least some feedback for the button press
2178 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002179 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002180 }
2181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002183 * Re-listen when widgets are reset.
2184 */
2185 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002186 if (mAppWidgetHost != null) {
2187 mAppWidgetHost.startListening();
2188 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002189 }
2190
2191 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 * Launches the intent referred by the clicked shortcut.
2193 *
2194 * @param v The view representing the clicked shortcut.
2195 */
2196 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002197 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2198 // view has detached (it's possible for this to happen if the view is removed mid touch).
2199 if (v.getWindowToken() == null) {
2200 return;
2201 }
2202
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002203 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002204 return;
2205 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002206
Adam Cohen1697b792013-09-17 19:08:21 -07002207 if (v instanceof Workspace) {
2208 if (mWorkspace.isInOverviewMode()) {
2209 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002210 }
2211 return;
2212 }
2213
2214 if (v instanceof CellLayout) {
2215 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002216 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002217 }
2218 }
2219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002221 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002223 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2224 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002225
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002226 // Check for special shortcuts
2227 if (intent.getComponent() != null) {
2228 final String shortcutClass = intent.getComponent().getClassName();
2229
2230 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002231 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002232 return;
2233 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2234 MemoryDumpActivity.startDump(this);
2235 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002236 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2237 toggleShowWeightWatcher();
2238 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002239 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002240 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002241
2242 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002243 int[] pos = new int[2];
2244 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002245 intent.setSourceBounds(new Rect(pos[0], pos[1],
2246 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002247
2248 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002249
Daniel Sandlerff02d492013-08-05 02:12:05 -04002250 mStats.recordLaunch(intent, shortcut);
2251
Michael Jurkaddd62e92011-02-16 17:49:14 -08002252 if (success && v instanceof BubbleTextView) {
2253 mWaitingForResume = (BubbleTextView) v;
2254 mWaitingForResume.setStayPressed(true);
2255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002257 if (v instanceof FolderIcon) {
2258 FolderIcon fi = (FolderIcon) v;
2259 handleFolderClick(fi);
2260 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002261 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002262 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002263 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002264 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002265 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002266 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 }
2268 }
2269
Michael Jurka0e260592010-06-30 17:07:39 -07002270 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002271 return false;
2272 }
2273
Michael Jurkaaf442092010-06-10 17:01:57 -07002274 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002275 * Event handler for the search button
2276 *
2277 * @param v The view that was clicked.
2278 */
2279 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002280 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2281
Amith Yamasania135ba82011-08-09 17:42:01 -07002282 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002283 }
2284
2285 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002286 * Event handler for the voice button
2287 *
2288 * @param v The view that was clicked.
2289 */
2290 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002291 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002292
Bjorn Bringertc459e522013-06-07 19:36:01 +01002293 startVoice();
2294 }
2295
2296 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002297 try {
2298 final SearchManager searchManager =
2299 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2300 ComponentName activityName = searchManager.getGlobalSearchActivity();
2301 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002302 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002303 if (activityName != null) {
2304 intent.setPackage(activityName.getPackageName());
2305 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002306 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002307 } catch (ActivityNotFoundException e) {
2308 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002309 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002310 startActivitySafely(null, intent, "onClickVoiceButton");
2311 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002312 }
2313
2314 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002315 * Event handler for the "grid" button that appears on the home screen, which
2316 * enters all apps mode.
2317 *
2318 * @param v The view that was clicked.
2319 */
2320 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002321 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002322 }
2323
2324 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002325 // Provide the same haptic feedback that the system offers for virtual keys.
2326 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002327 }
2328
Adam Cohen61f560d2013-09-30 15:58:20 -07002329 public void performHapticFeedbackOnTouchDown(View v) {
2330 // Provide the same haptic feedback that the system offers for virtual keys.
2331 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2332 }
2333
2334 public View.OnTouchListener getHapticFeedbackTouchListener() {
2335 if (mHapticFeedbackTouchListener == null) {
2336 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2337 @Override
2338 public boolean onTouch(View v, MotionEvent event) {
2339 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2340 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2341 }
2342 return false;
2343 }
2344 };
2345 }
2346 return mHapticFeedbackTouchListener;
2347 }
2348
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002349 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002350 if (!DISABLE_MARKET_BUTTON) {
2351 if (mAppMarketIntent != null) {
2352 startActivitySafely(v, mAppMarketIntent, "app market");
2353 } else {
2354 Log.e(TAG, "Invalid app market intent.");
2355 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002356 }
2357 }
2358
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002359 /**
2360 * Called when the user stops interacting with the launcher.
2361 * This implies that the user is now on the homescreen and is not doing housekeeping.
2362 */
2363 protected void onInteractionEnd() {}
2364
2365 /**
2366 * Called when the user starts interacting with the launcher.
2367 * The possible interactions are:
2368 * - open all apps
2369 * - reorder an app shortcut, or a widget
2370 * - open the overview mode.
2371 * This is a good time to stop doing things that only make sense
2372 * when the user is on the homescreen and not doing housekeeping.
2373 */
2374 protected void onInteractionBegin() {}
2375
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002376 void startApplicationDetailsActivity(ComponentName componentName) {
2377 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002378 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2379 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002380 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002381 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002382 }
2383
Michael Jurka1e2f4652013-07-08 18:03:46 -07002384 // returns true if the activity was started
2385 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002386 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002387 // System applications cannot be installed. For now, show a toast explaining that.
2388 // We may give them the option of disabling apps this way.
2389 int messageId = R.string.uninstall_system_app_text;
2390 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002391 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002392 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002393 String packageName = componentName.getPackageName();
2394 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002395 Intent intent = new Intent(
2396 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002397 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2398 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002399 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002400 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002401 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002402 }
2403
Michael Jurka86a720e2012-05-09 11:23:23 -07002404 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002408 // Only launch using the new animation if the shortcut has not opted out (this is a
2409 // private contract between launcher and may be ignored in the future).
2410 boolean useLaunchAnimation = (v != null) &&
2411 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2412 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002413 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2414 v.getMeasuredWidth(), v.getMeasuredHeight());
2415
2416 startActivity(intent, opts.toBundle());
2417 } else {
2418 startActivity(intent);
2419 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002420 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 } catch (SecurityException e) {
2422 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002423 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002425 "or use the exported attribute for this activity. "
2426 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002428 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002430
Michael Jurka86a720e2012-05-09 11:23:23 -07002431 boolean startActivitySafely(View v, Intent intent, Object tag) {
2432 boolean success = false;
2433 try {
2434 success = startActivity(v, intent, tag);
2435 } catch (ActivityNotFoundException e) {
2436 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2437 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2438 }
2439 return success;
2440 }
2441
Adam Cohena9cf38f2011-05-02 15:36:58 -07002442 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002443 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002444 Folder openFolder = mWorkspace.getFolderForTag(info);
2445
2446 // If the folder info reports that the associated folder is open, then verify that
2447 // it is actually opened. There have been a few instances where this gets out of sync.
2448 if (info.opened && openFolder == null) {
2449 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002450 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002451 info.opened = false;
2452 }
2453
Adam Cohenfb91f302012-06-11 15:45:18 -07002454 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 // Close any open folder
2456 closeFolder();
2457 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002458 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 } else {
2460 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 int folderScreen;
2462 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002463 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 // .. and close it
2465 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002466 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 // Close any folder open on the current screen
2468 closeFolder();
2469 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002470 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471 }
2472 }
2473 }
2474 }
2475
Adam Cohen268c4752012-06-06 17:47:33 -07002476 /**
2477 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2478 * in the DragLayer in the exact absolute location of the original FolderIcon.
2479 */
2480 private void copyFolderIconToImage(FolderIcon fi) {
2481 final int width = fi.getMeasuredWidth();
2482 final int height = fi.getMeasuredHeight();
2483
2484 // Lazy load ImageView, Bitmap and Canvas
2485 if (mFolderIconImageView == null) {
2486 mFolderIconImageView = new ImageView(this);
2487 }
2488 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2489 mFolderIconBitmap.getHeight() != height) {
2490 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2491 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2492 }
2493
2494 DragLayer.LayoutParams lp;
2495 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2496 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2497 } else {
2498 lp = new DragLayer.LayoutParams(width, height);
2499 }
2500
Adam Cohen307fe232012-08-16 17:55:58 -07002501 // The layout from which the folder is being opened may be scaled, adjust the starting
2502 // view size by this scale factor.
2503 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002504 lp.customPosition = true;
2505 lp.x = mRectForFolderAnimation.left;
2506 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002507 lp.width = (int) (scale * width);
2508 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002509
2510 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2511 fi.draw(mFolderIconCanvas);
2512 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002513 if (fi.getFolder() != null) {
2514 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2515 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002516 }
Adam Cohen268c4752012-06-06 17:47:33 -07002517 // Just in case this image view is still in the drag layer from a previous animation,
2518 // we remove it and re-add it.
2519 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2520 mDragLayer.removeView(mFolderIconImageView);
2521 }
2522 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002523 if (fi.getFolder() != null) {
2524 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002525 }
Adam Cohen268c4752012-06-06 17:47:33 -07002526 }
2527
Adam Cohen2801caf2011-05-13 20:57:39 -07002528 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002529 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002530 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2531 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2532 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2533
Adam Cohenc51934b2011-07-26 21:07:43 -07002534 FolderInfo info = (FolderInfo) fi.getTag();
2535 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2536 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002537 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2538 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002539 }
2540
Adam Cohen268c4752012-06-06 17:47:33 -07002541 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2542 copyFolderIconToImage(fi);
2543 fi.setVisibility(View.INVISIBLE);
2544
Michael Jurka2ecf9952012-06-18 12:52:28 -07002545 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002546 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002547 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2548 oa.start();
2549 }
2550
Adam Cohen268c4752012-06-06 17:47:33 -07002551 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002552 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002553 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2554 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2555 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2556
Adam Cohen268c4752012-06-06 17:47:33 -07002557 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002558
Adam Cohen268c4752012-06-06 17:47:33 -07002559 // We remove and re-draw the FolderIcon in-case it has changed
2560 mDragLayer.removeView(mFolderIconImageView);
2561 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002562 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002563 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002564 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002565 oa.addListener(new AnimatorListenerAdapter() {
2566 @Override
2567 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002568 if (cl != null) {
2569 cl.clearFolderLeaveBehind();
2570 // Remove the ImageView copy of the FolderIcon and make the original visible.
2571 mDragLayer.removeView(mFolderIconImageView);
2572 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002573 }
2574 }
2575 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002576 oa.start();
2577 }
2578
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002580 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 * is animated relative to the specified View. If the View is null, no animation
2582 * is played.
2583 *
2584 * @param folderInfo The FolderInfo describing the folder to open.
2585 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002586 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002587 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002588 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589
Adam Cohena9cf38f2011-05-02 15:36:58 -07002590 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591
Adam Cohen4554ee12011-08-03 16:13:21 -07002592 // Just verify that the folder hasn't already been added to the DragLayer.
2593 // There was a one-off crash where the folder had a parent already.
2594 if (folder.getParent() == null) {
2595 mDragLayer.addView(folder);
2596 mDragController.addDropTarget((DropTarget) folder);
2597 } else {
2598 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2599 folder.getParent() + ").");
2600 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002601 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002602 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002603
2604 // Notify the accessibility manager that this folder "window" has appeared and occluded
2605 // the workspace items
2606 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2607 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002608 }
2609
2610 public void closeFolder() {
2611 Folder folder = mWorkspace.getOpenFolder();
2612 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002613 if (folder.isEditingName()) {
2614 folder.dismissEditingName();
2615 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002616 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002617
2618 // Dismiss the folder cling
2619 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002620 }
2621 }
2622
2623 void closeFolder(Folder folder) {
2624 folder.getInfo().opened = false;
2625
2626 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2627 if (parent != null) {
2628 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2629 shrinkAndFadeInFolderIcon(fi);
2630 }
2631 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002632
2633 // Notify the accessibility manager that this folder "window" has disappeard and no
2634 // longer occludeds the workspace items
2635 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002636 }
2637
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002638 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002639 if (!isDraggingEnabled()) return false;
2640 if (isWorkspaceLocked()) return false;
2641 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002642
Adam Cohen1697b792013-09-17 19:08:21 -07002643 if (v instanceof Workspace) {
2644 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002645 if (mWorkspace.enterOverviewMode()) {
2646 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2647 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2648 return true;
2649 } else {
2650 return false;
2651 }
Adam Cohen1697b792013-09-17 19:08:21 -07002652 }
Adam Cohen1697b792013-09-17 19:08:21 -07002653 }
2654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002655 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002656 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657 }
2658
Michael Jurka0280c3b2010-09-17 15:00:07 -07002659 resetAddInfo();
2660 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002662 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002663 return true;
2664 }
2665
Winson Chung3d503fb2011-07-13 17:25:49 -07002666 // The hotseat touch handling does not go through Workspace, and we always allow long press
2667 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002668 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002669 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2670 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002671 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002672 // User long pressed on empty space
2673 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2674 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2675 // Disabling reordering until we sort out some issues.
2676 if (mWorkspace.isInOverviewMode()) {
2677 mWorkspace.startReordering(v);
2678 } else {
2679 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002680 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002682 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002683 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002684 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002685 }
2686 }
2687 }
2688 return true;
2689 }
2690
Winson Chung3d503fb2011-07-13 17:25:49 -07002691 boolean isHotseatLayout(View layout) {
2692 return mHotseat != null && layout != null &&
2693 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2694 }
2695 Hotseat getHotseat() {
2696 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002697 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002698 View getOverviewPanel() {
2699 return mOverviewPanel;
2700 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002701 SearchDropTargetBar getSearchBar() {
2702 return mSearchDropTargetBar;
2703 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002704
Winson Chung3d503fb2011-07-13 17:25:49 -07002705 /**
2706 * Returns the CellLayout of the specified container at the specified screen.
2707 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002708 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002709 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2710 if (mHotseat != null) {
2711 return mHotseat.getLayout();
2712 } else {
2713 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002714 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002715 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002716 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002717 }
2718 }
2719
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 Workspace getWorkspace() {
2721 return mWorkspace;
2722 }
2723
Daniel Sandler843e8602010-06-07 14:59:01 -04002724 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002725 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002726 }
2727
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002728 /**
2729 * Helper method for the cameraZoomIn/cameraZoomOut animations
2730 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002731 * @param scaleFactor The scale factor used for the zoom
2732 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002733 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002734 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002735 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002736 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002737
Craig Mautner360310b2012-10-26 15:13:08 -07002738 private void setWorkspaceBackground(boolean workspace) {
2739 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002740 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002741 }
2742
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002743 void updateWallpaperVisibility(boolean visible) {
2744 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2745 int curflags = getWindow().getAttributes().flags
2746 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2747 if (wpflags != curflags) {
2748 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2749 }
Craig Mautner360310b2012-10-26 15:13:08 -07002750 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002751 }
2752
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002753 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2754 if (v instanceof LauncherTransitionable) {
2755 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2756 }
2757 }
2758
Michael Jurkabed61d22012-02-14 22:51:29 -08002759 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2760 if (v instanceof LauncherTransitionable) {
2761 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2762 }
Winson Chung70442722012-02-10 15:43:22 -08002763
2764 // Update the workspace transition step as well
2765 dispatchOnLauncherTransitionStep(v, 0f);
2766 }
2767
2768 private void dispatchOnLauncherTransitionStep(View v, float t) {
2769 if (v instanceof LauncherTransitionable) {
2770 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2771 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002772 }
2773
2774 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2775 if (v instanceof LauncherTransitionable) {
2776 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2777 }
Winson Chung70442722012-02-10 15:43:22 -08002778
2779 // Update the workspace transition step as well
2780 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002781 }
2782
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002783 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002784 * Things to test when changing the following seven functions.
2785 * - Home from workspace
2786 * - from center screen
2787 * - from other screens
2788 * - Home from all apps
2789 * - from center screen
2790 * - from other screens
2791 * - Back from all apps
2792 * - from center screen
2793 * - from other screens
2794 * - Launch app from workspace and quit
2795 * - with back
2796 * - with home
2797 * - Launch app from all apps and quit
2798 * - with back
2799 * - with home
2800 * - Go to a screen that's not the default, then all
2801 * apps, and launch and app, and go back
2802 * - with back
2803 * -with home
2804 * - On workspace, long press power and go back
2805 * - with back
2806 * - with home
2807 * - On all apps, long press power and go back
2808 * - with back
2809 * - with home
2810 * - On workspace, power off
2811 * - On all apps, power off
2812 * - Launch an app and turn off the screen while in that app
2813 * - Go back with home key
2814 * - Go back with back key TODO: make this not go to workspace
2815 * - From all apps
2816 * - From workspace
2817 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2818 * - From all apps
2819 * - From the center workspace
2820 * - From another workspace
2821 */
2822
2823 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002824 * Zoom the camera out from the workspace to reveal 'toView'.
2825 * Assumes that the view to show is anchored at either the very top or very bottom
2826 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002827 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002828 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002829 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2830 showAppsCustomizeHelper(animated, springLoaded, contentType);
2831 }
2832 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2833 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002834 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002835 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002836 mStateAnimation.cancel();
2837 mStateAnimation = null;
2838 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002839 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002840
Winson Chungf0ea4d32011-06-06 14:27:16 -07002841 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2842 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2843 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002844 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002845 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002846 final int startDelay =
2847 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002848
Michael Jurkab3e22d92011-10-31 15:58:33 -07002849 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002850
Michael Jurkad74c9842011-07-10 12:44:21 -07002851 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002852 Animator workspaceAnim =
2853 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002854 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2855 // Set the content type for the all apps space
2856 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2857 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002858
2859 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002860 toView.setScaleX(scale);
2861 toView.setScaleY(scale);
2862 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2863 scaleAnim.
2864 scaleX(1f).scaleY(1f).
2865 setDuration(duration).
2866 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002867
Winson Chungf0ea4d32011-06-06 14:27:16 -07002868 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002869 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002870 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002871 .ofFloat(toView, "alpha", 0f, 1f)
2872 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002873 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002874 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2875 @Override
2876 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002877 if (animation == null) {
2878 throw new RuntimeException("animation is null");
2879 }
Winson Chung70442722012-02-10 15:43:22 -08002880 float t = (Float) animation.getAnimatedValue();
2881 dispatchOnLauncherTransitionStep(fromView, t);
2882 dispatchOnLauncherTransitionStep(toView, t);
2883 }
2884 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002885
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002886 // toView should appear right at the end of the workspace shrink
2887 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002888 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002889 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002890 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002891
2892 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002893 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002894 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002895 // Prepare the position
2896 toView.setTranslationX(0.0f);
2897 toView.setTranslationY(0.0f);
2898 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002899 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002900 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002901 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002902 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002903 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2904 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002905
Winson Chungc7d2b602012-05-16 17:02:20 -07002906 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002907 if (mSearchDropTargetBar != null) {
2908 mSearchDropTargetBar.hideSearchBar(false);
2909 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002910 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002911 });
2912
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002913 if (workspaceAnim != null) {
2914 mStateAnimation.play(workspaceAnim);
2915 }
Michael Jurka1899a362011-11-03 13:50:45 -07002916
2917 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002918
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002919 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2920 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002921
2922 // If any of the objects being animated haven't been measured/laid out
2923 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002924 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002925 (mWorkspace.getMeasuredWidth() == 0) ||
2926 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002927 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002928 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002929
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002930 final AnimatorSet stateAnimation = mStateAnimation;
2931 final Runnable startAnimRunnable = new Runnable() {
2932 public void run() {
2933 // Check that mStateAnimation hasn't changed while
2934 // we waited for a layout/draw pass
2935 if (mStateAnimation != stateAnimation)
2936 return;
2937 setPivotsForZoom(toView, scale);
2938 dispatchOnLauncherTransitionStart(fromView, animated, false);
2939 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002940 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002941 }
2942 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002943 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002944 final ViewTreeObserver observer = toView.getViewTreeObserver();
2945 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2946 public void onGlobalLayout() {
2947 startAnimRunnable.run();
2948 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2949 }
2950 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002951 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002952 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002953 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002954 } else {
2955 toView.setTranslationX(0.0f);
2956 toView.setTranslationY(0.0f);
2957 toView.setScaleX(1.0f);
2958 toView.setScaleY(1.0f);
2959 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002960 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002961
Daniel Sandlere4f98912013-06-25 15:13:26 -04002962 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002963 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002964 if (mSearchDropTargetBar != null) {
2965 mSearchDropTargetBar.hideSearchBar(false);
2966 }
Michael Jurkaabded662011-03-04 12:06:57 -08002967 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002968 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002969 dispatchOnLauncherTransitionStart(fromView, animated, false);
2970 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002971 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002972 dispatchOnLauncherTransitionStart(toView, animated, false);
2973 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002974 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002975 }
2976
2977 /**
2978 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002979 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002980 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002981 */
Adam Cohened307df2013-10-02 09:37:31 -07002982 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08002983 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002984
Michael Jurkab3e22d92011-10-31 15:58:33 -07002985 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002986 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002987 mStateAnimation.cancel();
2988 mStateAnimation = null;
2989 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002990 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002991
Winson Chungf0ea4d32011-06-06 14:27:16 -07002992 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002993 final int fadeOutDuration =
2994 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002995 final float scaleFactor = (float)
2996 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2997 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002998 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002999 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003000 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003001 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3002 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003003 toState, animated, stagger, -1);
3004 } else if (toState == Workspace.State.SPRING_LOADED ||
3005 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003006 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003007 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003008 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003009
Michael Jurkab3e22d92011-10-31 15:58:33 -07003010 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003011 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003012 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003013 final LauncherViewPropertyAnimator scaleAnim =
3014 new LauncherViewPropertyAnimator(fromView);
3015 scaleAnim.
3016 scaleX(scaleFactor).scaleY(scaleFactor).
3017 setDuration(duration).
3018 setInterpolator(new Workspace.ZoomInInterpolator());
3019
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003020 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003021 .ofFloat(fromView, "alpha", 1f, 0f)
3022 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003023 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003024 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3025 @Override
3026 public void onAnimationUpdate(ValueAnimator animation) {
3027 float t = 1f - (Float) animation.getAnimatedValue();
3028 dispatchOnLauncherTransitionStep(fromView, t);
3029 dispatchOnLauncherTransitionStep(toView, t);
3030 }
3031 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003032
Michael Jurka2ecf9952012-06-18 12:52:28 -07003033 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003034
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003035 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3036 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003037 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003038
3039 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003040 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003041 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003042 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003043 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3044 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003045 if (onCompleteRunnable != null) {
3046 onCompleteRunnable.run();
3047 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003048 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003049 }
3050 });
3051
Winson Chungf0ea4d32011-06-06 14:27:16 -07003052 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003053 if (workspaceAnim != null) {
3054 mStateAnimation.play(workspaceAnim);
3055 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003056 dispatchOnLauncherTransitionStart(fromView, animated, true);
3057 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003058 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003059 } else {
3060 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003061 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003062 dispatchOnLauncherTransitionStart(fromView, animated, true);
3063 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003064 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003065 dispatchOnLauncherTransitionStart(toView, animated, true);
3066 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003067 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003068 }
3069
Michael Jurkae326f182011-11-21 14:05:46 -08003070 @Override
3071 public void onTrimMemory(int level) {
3072 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003073 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003074 mAppsCustomizeTabHost.onTrimMemory();
3075 }
3076 }
3077
Adam Cohened307df2013-10-02 09:37:31 -07003078 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003079 showWorkspace(animated, null);
3080 }
3081
Adam Cohened307df2013-10-02 09:37:31 -07003082 protected void showWorkspace() {
3083 showWorkspace(true);
3084 }
3085
Adam Cohened66b2b2012-01-23 17:28:51 -08003086 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003087 if (mWorkspace.isInOverviewMode()) {
3088 mWorkspace.exitOverviewMode(animated);
3089 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003090 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003091 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003092 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003093 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003094
Winson Chungc7d2b602012-05-16 17:02:20 -07003095 // Show the search bar (only animate if we were showing the drop target bar in spring
3096 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003097 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003098 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003099 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003100
Michael Jurkab3e22d92011-10-31 15:58:33 -07003101 // Set focus to the AppsCustomize button
3102 if (mAllAppsButton != null) {
3103 mAllAppsButton.requestFocus();
3104 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003105 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003106
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003107 // Change the state *after* we've called all the transition code
3108 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003109
Winson Chung5fb63472011-02-02 17:03:37 -08003110 // Resume the auto-advance of widgets
3111 mUserPresent = true;
3112 updateRunning();
3113
alanv1d4fde62012-10-17 13:15:47 -07003114 // Send an accessibility event to announce the context change
3115 getWindow().getDecorView()
3116 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003117
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003118 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003119 }
3120
Adam Cohened307df2013-10-02 09:37:31 -07003121 void showOverviewMode(boolean animated) {
3122 mWorkspace.setVisibility(View.VISIBLE);
3123 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3124 mState = State.WORKSPACE;
3125 onWorkspaceShown(animated);
3126 }
3127
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003128 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003129 }
3130
Winson Chung82963d52013-10-09 11:20:57 -07003131 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3132 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003133 if (mState != State.WORKSPACE) return;
3134
Winson Chung82963d52013-10-09 11:20:57 -07003135 if (resetPageToZero) {
3136 mAppsCustomizeTabHost.reset();
3137 }
Winson Chungc58497e2013-09-03 17:48:37 -07003138 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003139 mAppsCustomizeTabHost.requestFocus();
3140
Michael Jurkab3e22d92011-10-31 15:58:33 -07003141 // Change the state *after* we've called all the transition code
3142 mState = State.APPS_CUSTOMIZE;
3143
3144 // Pause the auto-advance of widgets until we are out of AllApps
3145 mUserPresent = false;
3146 updateRunning();
3147 closeFolder();
3148
3149 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003150 getWindow().getDecorView()
3151 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003152 }
3153
Adam Cohen7777d962011-08-18 18:58:38 -07003154 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003155 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003156 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003157 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003158 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003159 }
Adam Cohen7777d962011-08-18 18:58:38 -07003160
Adam Cohenad4e15c2013-10-17 16:21:35 -07003161 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003162 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003163 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3164
Winson Chunge7a03942011-08-05 15:05:12 -07003165 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003166 @Override
3167 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003168 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003169 // Before we show workspace, hide all apps again because
3170 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3171 // clean up our state transition functions
3172 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003173 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003174 } else {
3175 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003176 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003177 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003178 }, delay);
3179
Winson Chung557d6ed2011-07-08 15:34:52 -07003180 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003181
Michael Jurkad3ef3062010-11-23 16:23:58 -08003182 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003183 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003184 final boolean animated = true;
3185 final boolean springLoaded = true;
3186 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003187 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003188 }
3189 // Otherwise, we are not in spring loaded mode, so don't do anything.
3190 }
3191
Michael Jurkab3e22d92011-10-31 15:58:33 -07003192 void lockAllApps() {
3193 // TODO
3194 }
3195
3196 void unlockAllApps() {
3197 // TODO
3198 }
3199
Winson Chungf0ea4d32011-06-06 14:27:16 -07003200 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003201 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003202 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003203 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003204 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003205 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003206 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003207 int duration = 0;
3208 if (mSearchDropTargetBar != null) {
3209 duration = mSearchDropTargetBar.getTransitionInDuration();
3210 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003211 mHotseat.animate().alpha(1f).setDuration(duration);
3212 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003213 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003214 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003215 }
3216 }
3217 }
3218
3219 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003220 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003221 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003222 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003223 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003224 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003225 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003226 int duration = 0;
3227 if (mSearchDropTargetBar != null) {
3228 duration = mSearchDropTargetBar.getTransitionOutDuration();
3229 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003230 mHotseat.animate().alpha(0f).setDuration(duration);
3231 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003232 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003233 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003234 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003235 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003236 }
3237
Patrick Dubroy5f445422011-02-18 14:35:21 -08003238 /**
3239 * Add an item from all apps or customize onto the given workspace screen.
3240 * If layout is null, add to the current screen.
3241 */
3242 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003243 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003244 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003245 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003246 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003247
Winson Chungdff8ebb2011-09-08 17:25:31 -07003248 /** Maps the current orientation to an index for referencing orientation correct global icons */
3249 private int getCurrentOrientationIndexForGlobalIcons() {
3250 // default - 0, landscape - 1
3251 switch (getResources().getConfiguration().orientation) {
3252 case Configuration.ORIENTATION_LANDSCAPE:
3253 return 1;
3254 default:
3255 return 0;
3256 }
3257 }
3258
Michael Jurkaae65ee32012-04-30 11:45:54 -07003259 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003260 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003261 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003262 // Look for the toolbar icon specified in the activity meta-data
3263 Bundle metaData = packageManager.getActivityInfo(
3264 activityName, PackageManager.GET_META_DATA).metaData;
3265 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003266 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003267 if (iconResId != 0) {
3268 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003269 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003270 }
3271 }
3272 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003273 // This can happen if the activity defines an invalid drawable
3274 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3275 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003276 } catch (Resources.NotFoundException nfe) {
3277 // This can happen if the activity defines an invalid drawable
3278 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3279 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003280 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003281 return null;
3282 }
3283
3284 // if successful in getting icon, return it; otherwise, set button to use default drawable
3285 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003286 int buttonId, ComponentName activityName, int fallbackDrawableId,
3287 String toolbarResourceName) {
3288 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003289 Resources r = getResources();
3290 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3291 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003292
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003293 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003294 // If we were unable to find the icon via the meta-data, use a generic one
3295 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003296 toolbarIcon = r.getDrawable(fallbackDrawableId);
3297 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003298 if (button != null) {
3299 button.setCompoundDrawables(toolbarIcon, null, null, null);
3300 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003301 return null;
3302 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003303 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003304 if (button != null) {
3305 button.setCompoundDrawables(toolbarIcon, null, null, null);
3306 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003307 return toolbarIcon.getConstantState();
3308 }
3309 }
3310
3311 // if successful in getting icon, return it; otherwise, set button to use default drawable
3312 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003313 int buttonId, ComponentName activityName, int fallbackDrawableId,
3314 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003315 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003316 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003317
Michael Jurka19e0fc52011-07-22 18:00:21 -07003318 if (button != null) {
3319 // If we were unable to find the icon via the meta-data, use a
3320 // generic one
3321 if (toolbarIcon == null) {
3322 button.setImageResource(fallbackDrawableId);
3323 } else {
3324 button.setImageDrawable(toolbarIcon);
3325 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003326 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003327
3328 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3329
Michael Jurka0423dcf2010-10-05 14:56:18 -07003330 }
3331
Winson Chung1b884092012-06-08 17:13:02 -07003332 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003333 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003334 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003335 }
3336
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003337 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003338 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003339 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003340 }
3341
Winson Chungbb185bd2011-11-21 12:31:42 -08003342 private void invalidatePressedFocusedStates(View container, View button) {
3343 if (container instanceof HolographicLinearLayout) {
3344 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3345 layout.invalidatePressedFocusedStates();
3346 } else if (button instanceof HolographicImageView) {
3347 HolographicImageView view = (HolographicImageView) button;
3348 view.invalidatePressedFocusedStates();
3349 }
3350 }
3351
Cristina Stancu476493b2013-08-07 17:20:14 +01003352 public View getQsbBar() {
3353 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003354 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3355 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003356 }
3357 return mQsbBar;
3358 }
3359
3360 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003361 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003362 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003363 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003364 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003365
Winson Chung1cad91e2011-05-25 17:41:01 -07003366 final SearchManager searchManager =
3367 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3368 ComponentName activityName = searchManager.getGlobalSearchActivity();
3369 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003370 int coi = getCurrentOrientationIndexForGlobalIcons();
3371 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003372 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3373 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3374 if (sGlobalSearchIcon[coi] == null) {
3375 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3376 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3377 TOOLBAR_ICON_METADATA_NAME);
3378 }
3379
Winson Chungc51db6a2011-10-05 11:44:49 -07003380 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3381 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003382 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003383 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003384 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003385 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003386 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3387 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003388 if (searchButton != null) searchButton.setVisibility(View.GONE);
3389 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003390 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003391 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003392 }
3393 }
3394
Cristina Stancu476493b2013-08-07 17:20:14 +01003395 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003396 final View searchButtonContainer = findViewById(R.id.search_button_container);
3397 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003398 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003399 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003400 }
3401
Cristina Stancu476493b2013-08-07 17:20:14 +01003402 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003403 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003404 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003405
Winson Chungc51db6a2011-10-05 11:44:49 -07003406 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003407 final SearchManager searchManager =
3408 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3409 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3410
3411 ComponentName activityName = null;
3412 if (globalSearchActivity != null) {
3413 // Check if the global search activity handles voice search
3414 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3415 intent.setPackage(globalSearchActivity.getPackageName());
3416 activityName = intent.resolveActivity(getPackageManager());
3417 }
3418
3419 if (activityName == null) {
3420 // Fallback: check if an activity other than the global search activity
3421 // resolves this
3422 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3423 activityName = intent.resolveActivity(getPackageManager());
3424 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003425 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003426 int coi = getCurrentOrientationIndexForGlobalIcons();
3427 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003428 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3429 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3430 if (sVoiceSearchIcon[coi] == null) {
3431 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3432 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3433 TOOLBAR_ICON_METADATA_NAME);
3434 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003435 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003436 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003437 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003438 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003439 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003440 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003441 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003442 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003443 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003444 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003445 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003446 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003447
Cristina Stancu476493b2013-08-07 17:20:14 +01003448 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003449 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3450 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003451 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003452 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003453 }
3454
Winson Chung5841efa2013-09-30 18:06:44 -07003455 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003456 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3457 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003458 boolean visible = !forceDisableVoiceButtonProxy &&
3459 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003460 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003461 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003462 }
3463 }
Winson Chung5841efa2013-09-30 18:06:44 -07003464
3465 /**
3466 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3467 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3468 */
3469 public void disableVoiceButtonProxy(boolean disabled) {
3470 updateVoiceButtonProxyVisible(disabled);
3471 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003472 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003473 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003474 */
3475 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003476 if (!DISABLE_MARKET_BUTTON) {
3477 final View marketButton = findViewById(R.id.market_button);
3478 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3479 // Find the app market activity by resolving an intent.
3480 // (If multiple app markets are installed, it will return the ResolverActivity.)
3481 ComponentName activityName = intent.resolveActivity(getPackageManager());
3482 if (activityName != null) {
3483 int coi = getCurrentOrientationIndexForGlobalIcons();
3484 mAppMarketIntent = intent;
3485 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3486 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3487 TOOLBAR_ICON_METADATA_NAME);
3488 marketButton.setVisibility(View.VISIBLE);
3489 } else {
3490 // We should hide and disable the view so that we don't try and restore the visibility
3491 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3492 marketButton.setVisibility(View.GONE);
3493 marketButton.setEnabled(false);
3494 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003495 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003496 }
3497
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003498 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003499 if (!DISABLE_MARKET_BUTTON) {
3500 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3501 Resources r = getResources();
3502 Drawable marketIconDrawable = d.newDrawable(r);
3503 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3504 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3505 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003506
Winson Chungd64a6662013-09-30 11:06:59 -07003507 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3508 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003509 }
3510
Michael Jurkad7c28052012-04-27 15:43:36 -07003511 @Override
3512 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003513 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003514 final List<CharSequence> text = event.getText();
3515 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003516 // Populate event with a fake title based on the current state.
3517 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003518 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003519 } else {
3520 text.add(getString(R.string.all_apps_home_button_label));
3521 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003522 return result;
3523 }
3524
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003525 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003526 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003527 */
3528 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3529 @Override
3530 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003531 closeSystemDialogs();
3532 }
3533 }
3534
3535 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003536 * Receives notifications whenever the appwidgets are reset.
3537 */
3538 private class AppWidgetResetObserver extends ContentObserver {
3539 public AppWidgetResetObserver() {
3540 super(new Handler());
3541 }
3542
3543 @Override
3544 public void onChange(boolean selfChange) {
3545 onAppWidgetReset();
3546 }
3547 }
3548
3549 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003550 * If the activity is currently paused, signal that we need to run the passed Runnable
3551 * in onResume.
3552 *
3553 * This needs to be called from incoming places where resources might have been loaded
3554 * while we are paused. That is becaues the Configuration might be wrong
3555 * when we're not running, and if it comes back to what it was when we
3556 * were paused, we are not restarted.
3557 *
3558 * Implementation of the method from LauncherModel.Callbacks.
3559 *
3560 * @return true if we are currently paused. The caller might be able to
3561 * skip some work in that case since we will come back again.
3562 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003563 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003564 if (mPaused) {
3565 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003566 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003567 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003568 }
3569 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003570 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003571 return true;
3572 } else {
3573 return false;
3574 }
3575 }
3576
Michael Jurkac402cd92013-05-20 15:49:32 +02003577 private boolean waitUntilResume(Runnable run) {
3578 return waitUntilResume(run, false);
3579 }
3580
Michael Jurka1e2f4652013-07-08 18:03:46 -07003581 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003582 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003583 }
3584
Michael Jurka7607c2f2013-04-03 14:33:19 -07003585 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003586 * If the activity is currently paused, signal that we need to re-run the loader
3587 * in onResume.
3588 *
3589 * This needs to be called from incoming places where resources might have been loaded
3590 * while we are paused. That is becaues the Configuration might be wrong
3591 * when we're not running, and if it comes back to what it was when we
3592 * were paused, we are not restarted.
3593 *
3594 * Implementation of the method from LauncherModel.Callbacks.
3595 *
3596 * @return true if we are currently paused. The caller might be able to
3597 * skip some work in that case since we will come back again.
3598 */
3599 public boolean setLoadOnResume() {
3600 if (mPaused) {
3601 Log.i(TAG, "setLoadOnResume");
3602 mOnResumeNeedsLoad = true;
3603 return true;
3604 } else {
3605 return false;
3606 }
3607 }
3608
3609 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003610 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003611 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003612 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003613 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003614 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003615 } else {
3616 return SCREEN_COUNT / 2;
3617 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003619
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620 /**
3621 * Refreshes the shortcuts shown on the workspace.
3622 *
3623 * Implementation of the method from LauncherModel.Callbacks.
3624 */
3625 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003626 // If we're starting binding all over again, clear any bind calls we'd postponed in
3627 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3628 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003629 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003630
Winson Chungd64d1762013-08-20 14:37:16 -07003631 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003632 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003633 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003634
Michael Jurka05bf6442011-11-30 20:28:53 -08003635 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003636 if (mHotseat != null) {
3637 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003638 }
3639 }
3640
Adam Cohendcd297f2013-06-18 13:13:40 -07003641 @Override
3642 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003643 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003644
Adam Cohen5084cba2013-09-03 12:01:16 -07003645 // If there are no screens, we need to have an empty screen
3646 if (orderedScreenIds.size() == 0) {
3647 mWorkspace.addExtraEmptyScreen();
3648 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003649
3650 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003651 // setCurrentPage() so ensure that all pages are added before calling this).
3652 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003653 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3654 mWorkspace.createCustomContentPage();
3655 }
Winson Chung64359a52013-07-08 17:17:08 -07003656 }
3657
3658 @Override
3659 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003660 int count = orderedScreenIds.size();
3661 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003662 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003663 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003664 }
3665
Adam Cohen39a06042013-07-19 14:30:12 -07003666 private boolean shouldShowWeightWatcher() {
3667 String spKey = LauncherAppState.getSharedPreferencesKey();
3668 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003669 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003670
3671 return show;
3672 }
3673
3674 private void toggleShowWeightWatcher() {
3675 String spKey = LauncherAppState.getSharedPreferencesKey();
3676 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3677 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3678
3679 show = !show;
3680
3681 SharedPreferences.Editor editor = sp.edit();
3682 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3683 editor.commit();
3684
3685 if (mWeightWatcher != null) {
3686 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3687 }
3688 }
3689
Winson Chungd64d1762013-08-20 14:37:16 -07003690 public void bindAppsAdded(final ArrayList<Long> newScreens,
3691 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003692 final ArrayList<ItemInfo> addAnimated,
3693 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003694 Runnable r = new Runnable() {
3695 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003696 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003697 }
3698 };
3699 if (waitUntilResume(r)) {
3700 return;
3701 }
3702
Winson Chungd64d1762013-08-20 14:37:16 -07003703 // Add the new screens
3704 bindAddScreens(newScreens);
3705
3706 // We add the items without animation on non-visible pages, and with
3707 // animations on the new page (which we will try and snap to).
3708 if (!addNotAnimated.isEmpty()) {
3709 bindItems(addNotAnimated, 0,
3710 addNotAnimated.size(), false);
3711 }
3712 if (!addAnimated.isEmpty()) {
3713 bindItems(addAnimated, 0,
3714 addAnimated.size(), true);
3715 }
Winson Chungc58497e2013-09-03 17:48:37 -07003716
Winson Chung87412982013-10-03 18:34:14 -07003717 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003718 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003719
Winson Chungc58497e2013-09-03 17:48:37 -07003720 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3721 addedApps != null && mAppsCustomizeContent != null) {
3722 mAppsCustomizeContent.addApps(addedApps);
3723 }
Winson Chungd64d1762013-08-20 14:37:16 -07003724 }
3725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003726 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 * Bind the items start-end from the list.
3728 *
3729 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003730 */
Winson Chung64359a52013-07-08 17:17:08 -07003731 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3732 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003733 Runnable r = new Runnable() {
3734 public void run() {
3735 bindItems(shortcuts, start, end, forceAnimateIcons);
3736 }
3737 };
3738 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003739 return;
3740 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003741
Winson Chungf0c6ae02012-03-21 16:10:31 -07003742 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003743 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3744 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003745 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003746 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003747 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003748 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003749 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003750
3751 // Short circuit if we are loading dock items for a configuration which has no dock
3752 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3753 mHotseat == null) {
3754 continue;
3755 }
3756
Joe Onorato9c1289c2009-08-17 11:03:03 -04003757 switch (item.itemType) {
3758 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3759 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003760 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003761 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003762
Winson Chung64359a52013-07-08 17:17:08 -07003763 /*
3764 * TODO: FIX collision case
3765 */
Winson Chungce376632013-07-11 16:12:41 -07003766 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3767 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3768 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3769 throw new RuntimeException("OCCUPIED");
3770 }
Winson Chung64359a52013-07-08 17:17:08 -07003771 }
3772
Adam Cohendcd297f2013-06-18 13:13:40 -07003773 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3774 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003775 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003776 // Animate all the applications up now
3777 shortcut.setAlpha(0f);
3778 shortcut.setScaleX(0f);
3779 shortcut.setScaleY(0f);
3780 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003781 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003782 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003783 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003784 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003785 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003786 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003787 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003788 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3789 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003790 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003791 default:
3792 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003794 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003795
Winson Chung997a9232013-07-24 15:33:46 -07003796 if (animateIcons) {
3797 // Animate to the correct page
3798 if (newShortcutsScreenId > -1) {
3799 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003800 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003801 final Runnable startBounceAnimRunnable = new Runnable() {
3802 public void run() {
3803 anim.playTogether(bounceAnims);
3804 anim.start();
3805 }
3806 };
Winson Chung997a9232013-07-24 15:33:46 -07003807 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003808 // We post the animation slightly delayed to prevent slowdowns
3809 // when we are loading right after we return to launcher.
3810 mWorkspace.postDelayed(new Runnable() {
3811 public void run() {
3812 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003813 mWorkspace.postDelayed(startBounceAnimRunnable,
3814 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003815 }
3816 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003817 } else {
3818 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003819 }
3820 }
Winson Chung64359a52013-07-08 17:17:08 -07003821 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003822 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003823 }
3824
Joe Onorato9c1289c2009-08-17 11:03:03 -04003825 /**
3826 * Implementation of the method from LauncherModel.Callbacks.
3827 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003828 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003829 Runnable r = new Runnable() {
3830 public void run() {
3831 bindFolders(folders);
3832 }
3833 };
3834 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003835 return;
3836 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003837 sFolders.clear();
3838 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003840
3841 /**
3842 * Add the views for a widget to the workspace.
3843 *
3844 * Implementation of the method from LauncherModel.Callbacks.
3845 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003846 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003847 Runnable r = new Runnable() {
3848 public void run() {
3849 bindAppWidget(item);
3850 }
3851 };
3852 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003853 return;
3854 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003855
Daniel Sandler843e8602010-06-07 14:59:01 -04003856 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3857 if (DEBUG_WIDGETS) {
3858 Log.d(TAG, "bindAppWidget: " + item);
3859 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 final Workspace workspace = mWorkspace;
3861
3862 final int appWidgetId = item.appWidgetId;
3863 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003864 if (DEBUG_WIDGETS) {
3865 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3866 }
3867
Joe Onorato9c1289c2009-08-17 11:03:03 -04003868 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3869
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003871 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003872
Adam Cohendcd297f2013-06-18 13:13:40 -07003873 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003874 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003875 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3876
Joe Onorato9c1289c2009-08-17 11:03:03 -04003877 workspace.requestLayout();
3878
Daniel Sandler843e8602010-06-07 14:59:01 -04003879 if (DEBUG_WIDGETS) {
3880 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3881 + (SystemClock.uptimeMillis()-start) + "ms");
3882 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003883 }
3884
Adam Cohen1462de32012-07-24 22:34:36 -07003885 public void onPageBoundSynchronously(int page) {
3886 mSynchronouslyBoundPages.add(page);
3887 }
3888
Joe Onorato9c1289c2009-08-17 11:03:03 -04003889 /**
3890 * Callback saying that there aren't any more items to bind.
3891 *
3892 * Implementation of the method from LauncherModel.Callbacks.
3893 */
Adam Cohene25af792013-06-06 23:08:25 -07003894 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003895 Runnable r = new Runnable() {
3896 public void run() {
3897 finishBindingItems(upgradePath);
3898 }
3899 };
3900 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003901 return;
3902 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003903 if (mSavedState != null) {
3904 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003905 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003906 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003907 mSavedState = null;
3908 }
3909
Adam Cohen1462de32012-07-24 22:34:36 -07003910 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003911
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003912 // If we received the result of any pending adds while the loader was running (e.g. the
3913 // widget configuration forced an orientation change), process them now.
3914 for (int i = 0; i < sPendingAddList.size(); i++) {
3915 completeAdd(sPendingAddList.get(i));
3916 }
3917 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003918
Winson Chungc51db6a2011-10-05 11:44:49 -07003919 // Update the market app icon as necessary (the other icons will be managed in response to
3920 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003921 if (!DISABLE_MARKET_BUTTON) {
3922 updateAppMarketIcon();
3923 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003924
Winson Chungf0c6ae02012-03-21 16:10:31 -07003925 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003926 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003927 mWorkspace.getUniqueComponents(true, null);
3928 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003929 }
Adam Cohen99894d92013-06-14 11:22:59 -07003930
Adam Cohen66a01fd2013-06-11 12:48:00 -07003931 mWorkspace.post(new Runnable() {
3932 @Override
3933 public void run() {
3934 onFinishBindingItems();
3935 }
3936 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003937 }
3938
Winson Chunga0b7e862013-09-05 16:03:15 -07003939 public boolean isAllAppsButtonRank(int rank) {
3940 if (mHotseat != null) {
3941 return mHotseat.isAllAppsButtonRank(rank);
3942 }
3943 return false;
3944 }
3945
Winson Chunga2413752012-04-03 14:22:34 -07003946 private boolean canRunNewAppsAnimation() {
3947 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3948 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3949 }
3950
Winson Chungc9168342013-06-26 14:54:55 -07003951 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3952 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3953 PropertyValuesHolder.ofFloat("alpha", 1f),
3954 PropertyValuesHolder.ofFloat("scaleX", 1f),
3955 PropertyValuesHolder.ofFloat("scaleY", 1f));
3956 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3957 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3958 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3959 return bounceAnim;
3960 }
3961
Adam Cohen27772732013-11-11 14:00:56 +00003962 public boolean useVerticalBarLayout() {
3963 return LauncherAppState.getInstance().getDynamicGrid().
3964 getDeviceProfile().isVerticalBarLayout();
3965 }
3966
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003967 @Override
3968 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003969 boolean searchVisible = updateGlobalSearchIcon();
3970 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003971 if (mSearchDropTargetBar != null) {
3972 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3973 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003974 }
3975
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003976 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977 * Add the icons for all apps.
3978 *
3979 * Implementation of the method from LauncherModel.Callbacks.
3980 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003981 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003982 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3983 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3984 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3985 getHotseat().addAllAppsFolder(mIconCache, apps,
3986 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3987 }
3988 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003989 }
Winson Chungc58497e2013-09-03 17:48:37 -07003990 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07003991 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07003992 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07003993 mAppsCustomizeContent.onPackagesUpdated(
3994 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07003995 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003997 }
3998
3999 /**
4000 * A package was updated.
4001 *
4002 * Implementation of the method from LauncherModel.Callbacks.
4003 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004004 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004005 Runnable r = new Runnable() {
4006 public void run() {
4007 bindAppsUpdated(apps);
4008 }
4009 };
4010 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004011 return;
4012 }
4013
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004014 if (mWorkspace != null) {
4015 mWorkspace.updateShortcuts(apps);
4016 }
Winson Chungc58497e2013-09-03 17:48:37 -07004017
4018 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4019 mAppsCustomizeContent != null) {
4020 mAppsCustomizeContent.updateApps(apps);
4021 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004022 }
4023
4024 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004025 * A package was uninstalled. We take both the super set of packageNames
4026 * in addition to specific applications to remove, the reason being that
4027 * this can be called when a package is updated as well. In that scenario,
4028 * we only remove specific components from the workspace, where as
4029 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030 *
4031 * Implementation of the method from LauncherModel.Callbacks.
4032 */
Winson Chung83892cc2013-05-01 16:53:33 -07004033 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004034 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004035 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004036 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004037 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004038 }
Winson Chungd64d1762013-08-20 14:37:16 -07004039 };
4040 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004041 return;
4042 }
4043
Winson Chungdf95eb12013-10-16 14:57:07 -07004044 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004045 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004046 }
4047 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004048 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004049 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004050
Winson Chunga1820962011-10-03 16:31:06 -07004051 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004052 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004053
Winson Chungdf95eb12013-10-16 14:57:07 -07004054 // Update AllApps
Winson Chungc58497e2013-09-03 17:48:37 -07004055 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4056 mAppsCustomizeContent != null) {
4057 mAppsCustomizeContent.removeApps(appInfos);
4058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004059 }
Joe Onoratobe386092009-11-17 17:32:16 -08004060
4061 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004062 * A number of packages were updated.
4063 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004064 private ArrayList<Object> mWidgetsAndShortcuts;
4065 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004066 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004067 bindPackagesUpdated(mWidgetsAndShortcuts);
4068 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004069 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004070 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004071 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4072 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4073 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004074 return;
4075 }
4076
Winson Chung64359a52013-07-08 17:17:08 -07004077 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004078 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4079 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004080 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004081 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004082 }
4083
Winson Chung400438b2011-01-16 17:53:48 -08004084 private int mapConfigurationOriActivityInfoOri(int configOri) {
4085 final Display d = getWindowManager().getDefaultDisplay();
4086 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4087 switch (d.getRotation()) {
4088 case Surface.ROTATION_0:
4089 case Surface.ROTATION_180:
4090 // We are currently in the same basic orientation as the natural orientation
4091 naturalOri = configOri;
4092 break;
4093 case Surface.ROTATION_90:
4094 case Surface.ROTATION_270:
4095 // We are currently in the other basic orientation to the natural orientation
4096 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4097 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4098 break;
4099 }
4100
4101 int[] oriMap = {
4102 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4103 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4104 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4105 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4106 };
4107 // Since the map starts at portrait, we need to offset if this device's natural orientation
4108 // is landscape.
4109 int indexOffset = 0;
4110 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4111 indexOffset = 1;
4112 }
4113 return oriMap[(d.getRotation() + indexOffset) % 4];
4114 }
Adam Cohen446e9402011-09-15 18:21:21 -07004115
Winson Chung4b919f82012-05-01 10:44:08 -07004116 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004117 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004118 getResources().getBoolean(R.bool.allow_rotation);
4119 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004120 }
Winson Chung4b919f82012-05-01 10:44:08 -07004121 public void lockScreenOrientation() {
4122 if (isRotationEnabled()) {
4123 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4124 .getConfiguration().orientation));
4125 }
4126 }
4127 public void unlockScreenOrientation(boolean immediate) {
4128 if (isRotationEnabled()) {
4129 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004130 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004131 } else {
4132 mHandler.postDelayed(new Runnable() {
4133 public void run() {
4134 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4135 }
4136 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004137 }
Winson Chung4b919f82012-05-01 10:44:08 -07004138 }
Winson Chung400438b2011-01-16 17:53:48 -08004139 }
4140
Winson Chung82f55532011-08-09 14:14:23 -07004141 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004142 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004143 if (DISABLE_CLINGS) {
4144 return false;
4145 }
4146
Winson Chung3a6e7f32013-10-09 15:50:52 -07004147 // For now, limit only to phones
4148 LauncherAppState app = LauncherAppState.getInstance();
4149 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4150 if (grid.isTablet()) {
4151 return false;
4152 }
Winson Chung13dbfe12013-10-31 11:42:39 -07004153 if (grid.isLandscape) {
4154 return false;
4155 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004156
Brett Chabot2a9e2282011-08-23 15:03:13 -07004157 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004158 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004159
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004160 // Disable clings for accessibility when explore by touch is enabled
4161 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4162 ACCESSIBILITY_SERVICE);
4163 if (a11yManager.isTouchExplorationEnabled()) {
4164 return false;
4165 }
4166
Michael Jurkae233a8b2013-03-19 13:49:20 +01004167 // Restricted secondary users (child mode) will potentially have very few apps
4168 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004169// boolean supportsLimitedUsers =
4170// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4171// Account[] accounts = AccountManager.get(this).getAccounts();
4172// if (supportsLimitedUsers && accounts.length == 0) {
4173// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4174// Bundle restrictions = um.getUserRestrictions();
4175// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4176// return false;
4177// }
4178// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004179 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004180 }
Michael Jurka22143132012-10-04 17:42:38 +02004181
Winson Chungfa545132013-09-26 17:45:32 -07004182 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004183 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004184 Cling cling = (Cling) findViewById(clingId);
4185 View scrim = null;
4186 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004187 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004188 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004189 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004190 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004191 cling.show(animate, SHOW_CLING_DURATION);
4192
4193 if (dimNavBarVisibilty) {
4194 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4195 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004196 }
4197 }
4198 return cling;
4199 }
Michael Jurka22143132012-10-04 17:42:38 +02004200
Winson Chungaf40f202013-09-18 18:26:31 -07004201 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4202 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004203 // To catch cases where siblings of top-level views are made invisible, just check whether
4204 // the cling is directly set to GONE before dismissing it.
4205 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004206 final Runnable cleanUpClingCb = new Runnable() {
4207 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004208 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004209 // We should update the shared preferences on a background thread
4210 new Thread("dismissClingThread") {
4211 public void run() {
4212 SharedPreferences.Editor editor = mSharedPrefs.edit();
4213 editor.putBoolean(flag, true);
4214 editor.commit();
4215 }
4216 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004217 if (postAnimationCb != null) {
4218 postAnimationCb.run();
4219 }
4220 }
4221 };
4222 if (duration <= 0) {
4223 cleanUpClingCb.run();
4224 } else {
4225 cling.hide(duration, cleanUpClingCb);
4226 }
Michael Jurka22143132012-10-04 17:42:38 +02004227 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004228
4229 if (restoreNavBarVisibilty) {
4230 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4231 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4232 }
Winson Chung82f55532011-08-09 14:14:23 -07004233 }
4234 }
Michael Jurka22143132012-10-04 17:42:38 +02004235
Winson Chung9d9d74f2011-09-19 11:49:12 -07004236 private void removeCling(int id) {
4237 final View cling = findViewById(id);
4238 if (cling != null) {
4239 final ViewGroup parent = (ViewGroup) cling.getParent();
4240 parent.post(new Runnable() {
4241 @Override
4242 public void run() {
4243 parent.removeView(cling);
4244 }
4245 });
Michael Jurka22143132012-10-04 17:42:38 +02004246 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004247 }
4248 }
Michael Jurka974c3862012-05-22 22:00:31 -07004249
4250 private boolean skipCustomClingIfNoAccounts() {
4251 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4252 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4253 if (customCling) {
4254 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004255 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004256 Account[] accounts = am.getAccountsByType("com.google");
4257 return accounts.length == 0;
4258 }
4259 return false;
4260 }
4261
Winson Chung82e5c982013-10-09 12:04:50 -07004262 public void updateCustomContentHintVisibility() {
4263 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4264 String ccHintStr = getFirstRunCustomContentHint();
4265
4266 if (mWorkspace.hasCustomContent()) {
4267 // Show the custom content hint if ccHintStr is not empty
4268 if (cling != null) {
4269 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4270 }
4271 } else {
4272 // Hide the custom content hint
4273 if (cling != null) {
4274 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4275 }
4276 }
4277 }
4278
4279 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4280 boolean animate) {
4281 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4282 if (ccHint != null) {
4283 if (visible && !ccHintStr.isEmpty()) {
4284 ccHint.setText(ccHintStr);
4285 ccHint.setVisibility(View.VISIBLE);
4286 if (animate) {
4287 ccHint.setAlpha(0f);
4288 ccHint.animate().alpha(1f)
4289 .setDuration(SHOW_CLING_DURATION)
4290 .start();
4291 } else {
4292 ccHint.setAlpha(1f);
4293 }
4294 } else {
4295 if (animate) {
4296 ccHint.animate().alpha(0f)
4297 .setDuration(SHOW_CLING_DURATION)
4298 .setListener(new AnimatorListenerAdapter() {
4299 @Override
4300 public void onAnimationEnd(Animator animation) {
4301 ccHint.setVisibility(View.GONE);
4302 }
4303 })
4304 .start();
4305 } else {
4306 ccHint.setAlpha(0f);
4307 ccHint.setVisibility(View.GONE);
4308 }
4309 }
4310 }
4311 }
4312
Winson Chungaf40f202013-09-18 18:26:31 -07004313 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004314 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004315 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004316 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004317 // If we're not using the default workspace layout, replace workspace cling
4318 // with a custom workspace cling (usually specified in an overlay)
4319 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004320 if (!DISABLE_CUSTOM_CLINGS) {
4321 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4322 getResources().getBoolean(R.bool.config_useCustomClings)) {
4323 // Use a custom cling
4324 View cling = findViewById(R.id.workspace_cling);
4325 ViewGroup clingParent = (ViewGroup) cling.getParent();
4326 int clingIndex = clingParent.indexOfChild(cling);
4327 clingParent.removeViewAt(clingIndex);
4328 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4329 clingParent.addView(customCling, clingIndex);
4330 customCling.setId(R.id.workspace_cling);
4331 }
4332 }
4333 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4334 if (cling != null) {
4335 String sbHintStr = getFirstRunClingSearchBarHint();
4336 String ccHintStr = getFirstRunCustomContentHint();
4337 if (!sbHintStr.isEmpty()) {
4338 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4339 sbHint.setText(sbHintStr);
4340 sbHint.setVisibility(View.VISIBLE);
4341 }
Winson Chung82e5c982013-10-09 12:04:50 -07004342 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004343 }
Winson Chungfa545132013-09-26 17:45:32 -07004344 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004345 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004346 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004347 }
4348 }
Winson Chungaf40f202013-09-18 18:26:31 -07004349
Winson Chunge6eabff2013-09-24 11:01:23 -07004350 protected String getFirstRunClingSearchBarHint() {
4351 return "";
4352 }
4353 protected String getFirstRunCustomContentHint() {
4354 return "";
4355 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004356 protected int getFirstRunFocusedHotseatAppDrawableId() {
4357 return -1;
4358 }
4359 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4360 return null;
4361 }
4362 protected int getFirstRunFocusedHotseatAppRank() {
4363 return -1;
4364 }
4365 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4366 return "";
4367 }
4368 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4369 return "";
4370 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004371
Winson Chungaf40f202013-09-18 18:26:31 -07004372 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004373 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004374 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004375 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004376 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4377
4378 // Set the focused hotseat app if there is one
4379 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4380 getFirstRunFocusedHotseatAppRank(),
4381 getFirstRunFocusedHotseatAppComponentName(),
4382 getFirstRunFocusedHotseatAppBubbleTitle(),
4383 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004384 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004385 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004386 }
4387 }
4388 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004389 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004390 if (isClingsEnabled() &&
4391 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004392 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4393 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004394 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004395 } else {
4396 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004397 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004398 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004399 }
Michael Jurka104c4562013-07-08 18:03:46 -07004400 protected SharedPreferences getSharedPrefs() {
4401 return mSharedPrefs;
4402 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004403 public boolean isFolderClingVisible() {
4404 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004405 if (cling != null) {
4406 return cling.getVisibility() == View.VISIBLE;
4407 }
4408 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004409 }
Winson Chungaf40f202013-09-18 18:26:31 -07004410 public void dismissFirstRunCling(View v) {
4411 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4412 Runnable cb = new Runnable() {
4413 public void run() {
4414 // Show the workspace cling next
4415 showFirstRunWorkspaceCling();
4416 }
4417 };
4418 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4419 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004420
4421 // Fade out the search bar for the workspace cling coming up
4422 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004423 }
Winson Chung82f55532011-08-09 14:14:23 -07004424 public void dismissWorkspaceCling(View v) {
4425 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004426 Runnable cb = null;
4427 if (v == null) {
4428 cb = new Runnable() {
4429 public void run() {
4430 mWorkspace.enterOverviewMode();
4431 }
4432 };
4433 }
4434 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4435 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004436
4437 // Fade in the search bar
4438 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004439 }
4440 public void dismissFolderCling(View v) {
4441 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004442 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4443 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004444 }
4445
Winson Chung80baf5a2010-08-09 16:03:15 -07004446 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004447 * Prints out out state for debugging.
4448 */
4449 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004450 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004451 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004452 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4453 Log.d(TAG, "mRestoring=" + mRestoring);
4454 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4455 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004456 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004457 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004458
Winson Chung785d2eb2011-04-14 16:08:02 -07004459 if (mAppsCustomizeContent != null) {
4460 mAppsCustomizeContent.dumpState();
4461 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004462 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004463 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004464
4465 @Override
4466 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4467 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004468 synchronized (sDumpLogs) {
4469 writer.println(" ");
4470 writer.println("Debug logs: ");
4471 for (int i = 0; i < sDumpLogs.size(); i++) {
4472 writer.println(" " + sDumpLogs.get(i));
4473 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004474 }
4475 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004476
4477 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004478 if (DEBUG_DUMP_LOG) {
4479 synchronized (sDumpLogs) {
4480 Log.d(TAG, "");
4481 Log.d(TAG, "*********************");
4482 Log.d(TAG, "Launcher debug logs: ");
4483 for (int i = 0; i < sDumpLogs.size(); i++) {
4484 Log.d(TAG, " " + sDumpLogs.get(i));
4485 }
4486 Log.d(TAG, "*********************");
4487 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004488 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004489 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004490 }
4491
4492 public static void addDumpLog(String tag, String log, boolean debugLog) {
4493 if (debugLog) {
4494 Log.d(tag, log);
4495 }
Winson Chungede41292013-09-19 16:27:36 -07004496 if (DEBUG_DUMP_LOG) {
4497 sDateStamp.setTime(System.currentTimeMillis());
4498 synchronized (sDumpLogs) {
4499 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004500 }
Winson Chungede41292013-09-19 16:27:36 -07004501 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004502 }
4503
Winson Chungede41292013-09-19 16:27:36 -07004504 public void dumpLogsToLocalData() {
4505 if (DEBUG_DUMP_LOG) {
4506 new Thread("DumpLogsToLocalData") {
4507 @Override
4508 public void run() {
4509 boolean success = false;
4510 sDateStamp.setTime(sRunStart);
4511 String FILENAME = sDateStamp.getMonth() + "-"
4512 + sDateStamp.getDay() + "_"
4513 + sDateStamp.getHours() + "-"
4514 + sDateStamp.getMinutes() + "_"
4515 + sDateStamp.getSeconds() + ".txt";
4516
4517 FileOutputStream fos = null;
4518 File outFile = null;
4519 try {
4520 outFile = new File(getFilesDir(), FILENAME);
4521 outFile.createNewFile();
4522 fos = new FileOutputStream(outFile);
4523 } catch (Exception e) {
4524 e.printStackTrace();
4525 }
4526 if (fos != null) {
4527 PrintWriter writer = new PrintWriter(fos);
4528
4529 writer.println(" ");
4530 writer.println("Debug logs: ");
4531 synchronized (sDumpLogs) {
4532 for (int i = 0; i < sDumpLogs.size(); i++) {
4533 writer.println(" " + sDumpLogs.get(i));
4534 }
4535 }
4536 writer.close();
4537 }
4538 try {
4539 if (fos != null) {
4540 fos.close();
4541 success = true;
4542 }
4543 } catch (IOException e) {
4544 e.printStackTrace();
4545 }
4546 }
4547 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004548 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004549 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004550}
Michael Jurka2763be32011-02-24 11:19:57 -08004551
Michael Jurkaabded662011-03-04 12:06:57 -08004552interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004553 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004554 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004555 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004556 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004557 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004558}