blob: 7b102b48f15adde85c70868af2155524574e3cca [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
Bjorn Bringert85f418d2013-09-06 12:50:05 +010048import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070054import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070072import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -070090import android.view.accessibility.AccessibilityManager;
Adam Cohencff6af82011-09-13 14:51:53 -070091import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700109import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700115import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100120public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Daniel Sandlerf061f822013-06-27 13:59:36 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400129 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700130 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 private static final int REQUEST_PICK_APPLICATION = 6;
135 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700137 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurka8b805b12012-04-18 14:23:14 -0700139 private static final int REQUEST_BIND_APPWIDGET = 11;
140
Mathew Inwood876a8462013-06-14 14:12:41 +0100141 /**
142 * IntentStarter uses request codes starting with this. This must be greater than all activity
143 * request codes used internally.
144 */
145 protected static final int REQUEST_LAST = 100;
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
148
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800149 static final int SCREEN_COUNT = 5;
150 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Romain Guy98d01652009-06-30 16:21:04 -0700152 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800153 // To turn on these properties, type
154 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
155 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
156 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Winson Chung2672ff92012-05-04 16:22:30 -0700158 // The Intent extra that defines whether to ignore the launch animation
159 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400160 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: int
163 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700164 // Type: int
165 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700167 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
168 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
170 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700171 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700173 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: boolean
175 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
176 // Type: long
177 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700178 // Type: int
179 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
180 // Type: int
181 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
182 // Type: parcelable
183 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100185 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700186 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100187 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700188 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100189 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700190
Adam Cohen39a06042013-07-19 14:30:12 -0700191 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700192 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700193
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700195 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700196 private State mState = State.WORKSPACE;
197 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700200 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
201 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Winson Chungaf40f202013-09-18 18:26:31 -0700202 private static final int SHOW_CLING_DURATION = 250;
203 private static final int DISMISS_CLING_DURATION = 200;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800206 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chunga2413752012-04-03 14:22:34 -0700208 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700209 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
210 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700211 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700212
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800213 private final BroadcastReceiver mCloseSystemDialogsReceiver
214 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800215 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private LayoutInflater mInflater;
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700220 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800221 private DragLayer mDragLayer;
222 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700223 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private AppWidgetManager mAppWidgetManager;
226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Michael Jurkac9d95c52011-08-29 14:03:34 -0700228 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700229 private AppWidgetProviderInfo mPendingAddWidgetInfo;
230
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231 private int[] mTmpAddItemCellCoordinates = new int[2];
232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private FolderInfo mFolderInfo;
234
Winson Chung3d503fb2011-07-13 17:25:49 -0700235 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700236 private View mOverviewPanel;
237
Michael Jurka838a4ca2011-02-07 13:33:06 -0800238 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700239
Winson Chungc51db6a2011-10-05 11:44:49 -0700240 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700241 private AppsCustomizeTabHost mAppsCustomizeTabHost;
242 private AppsCustomizePagedView mAppsCustomizeContent;
243 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100244 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
248 // scroll issues (because the workspace may not have been measured yet) and extra work.
249 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
250 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
252 private SpannableStringBuilder mDefaultKeySsb = null;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private boolean mWorkspaceLoading = true;
255
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800256 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private boolean mRestoring;
258 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700259 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurka1e2f4652013-07-08 18:03:46 -0700261 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
263
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // Keep track of whether the user has left launcher
265 private static boolean sPausedFromUserAction = false;
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedInstanceState;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800271 private boolean mUserPresent = true;
272 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700273 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800274 private boolean mAttached = false;
Winson Chungfa545132013-09-26 17:45:32 -0700275 private static final boolean DISABLE_CLINGS = false;
Winson Chunge6eabff2013-09-24 11:01:23 -0700276 private static final boolean DISABLE_CUSTOM_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700278 private static LocaleConfiguration sLocaleConfiguration = null;
279
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700280 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700281
Adam Cohen61f560d2013-09-30 15:58:20 -0700282 private View.OnTouchListener mHapticFeedbackTouchListener;
283
Adam Cohended9f8d2010-11-03 13:25:16 -0700284 // Related to the auto-advancing of widgets
285 private final int ADVANCE_MSG = 1;
286 private final int mAdvanceInterval = 20000;
287 private final int mAdvanceStagger = 250;
288 private long mAutoAdvanceSentTime;
289 private long mAutoAdvanceTimeLeft = -1;
290 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
291 new HashMap<View, AppWidgetProviderInfo>();
292
Winson Chung400438b2011-01-16 17:53:48 -0800293 // Determines how long to wait after a rotation before restoring the screen orientation to
294 // match the sensor state.
295 private final int mRestoreScreenOrientationDelay = 500;
296
Michael Jurka4ef207b2010-11-29 17:05:45 -0800297 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700298 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
299 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
300 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800301
Winson Chungd64a6662013-09-30 11:06:59 -0700302 private Intent mAppMarketIntent = null;
303 private static final boolean DISABLE_MARKET_BUTTON = true;
304
Craig Mautner360310b2012-10-26 15:13:08 -0700305 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700306
Adam Cohen1462de32012-07-24 22:34:36 -0700307 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700308 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700309
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700310 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700311 static Date sDateStamp = new Date();
312 static DateFormat sDateFormat =
313 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
314 static long sRunStart = System.currentTimeMillis();
315 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700316
Winson Chung46353de2012-02-16 14:05:10 -0800317 // We only want to get the SharedPreferences once since it does an FS stat each time we get
318 // it from the context.
319 private SharedPreferences mSharedPrefs;
320
Adam Cohene25af792013-06-06 23:08:25 -0700321 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
322
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 // Holds the page that we need to animate to, and the icon views that we need to animate up
324 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700325 private ImageView mFolderIconImageView;
326 private Bitmap mFolderIconBitmap;
327 private Canvas mFolderIconCanvas;
328 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329
Michael Jurkaddd62e92011-02-16 17:49:14 -0800330 private BubbleTextView mWaitingForResume;
331
Michael Jurka22143132012-10-04 17:42:38 +0200332 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
333 = new HideFromAccessibilityHelper();
334
Michael Jurkac1f5d262011-09-30 19:32:27 -0700335 private Runnable mBuildLayersRunnable = new Runnable() {
336 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700337 if (mWorkspace != null) {
338 mWorkspace.buildPageHardwareLayers();
339 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700340 }
341 };
342
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800343 private static ArrayList<PendingAddArguments> sPendingAddList
344 = new ArrayList<PendingAddArguments>();
345
Michael Jurka7267fa52013-09-26 15:29:57 -0700346 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800347
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800348 private static class PendingAddArguments {
349 int requestCode;
350 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700351 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700352 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353 int cellX;
354 int cellY;
355 }
356
Daniel Sandlerff02d492013-08-05 02:12:05 -0400357 private Stats mStats;
358
Michael Jurka0a457bf2012-11-19 14:05:05 -0800359 private static boolean isPropertyEnabled(String propertyName) {
360 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700361 }
362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 @Override
364 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700365 if (DEBUG_STRICT_MODE) {
366 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
367 .detectDiskReads()
368 .detectDiskWrites()
369 .detectNetwork() // or .detectAll() for all detectable problems
370 .penaltyLog()
371 .build());
372 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
373 .detectLeakedSqlLiteObjects()
374 .detectLeakedClosableObjects()
375 .penaltyLog()
376 .penaltyDeath()
377 .build());
378 }
379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400381
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400382 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400383 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700384
385 // Determine the dynamic grid properties
386 Point smallestSize = new Point();
387 Point largestSize = new Point();
388 Point realSize = new Point();
389 Display display = getWindowManager().getDefaultDisplay();
390 display.getCurrentSizeRange(smallestSize, largestSize);
391 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700392 DisplayMetrics dm = new DisplayMetrics();
393 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700394
Winson Chung5f8afe62013-08-12 16:19:28 -0700395 // Lazy-initialize the dynamic grid
396 DeviceProfile grid = app.initDynamicGrid(this,
397 Math.min(smallestSize.x, smallestSize.y),
398 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700399 realSize.x, realSize.y,
400 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700401
402 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400403 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700404 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800405 mModel = app.setLauncher(this);
406 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700407 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400408 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700410
Daniel Sandlerff02d492013-08-05 02:12:05 -0400411 mStats = new Stats(this);
412
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700413 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700414
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700415 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
416 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700417
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
419 // this also ensures that any synchronous binding below doesn't re-trigger another
420 // LauncherModel load.
421 mPaused = false;
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200424 android.os.Debug.startMethodTracing(
425 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 }
427
Adam Cohen53805212013-10-01 10:39:23 -0700428
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700431
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100433 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800435 registerContentObservers();
436
Joe Onorato7c312c12009-08-13 21:36:53 -0700437 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 mSavedState = savedInstanceState;
440 restoreState(mSavedState);
441
442 if (PROFILE_STARTUP) {
443 android.os.Debug.stopMethodTracing();
444 }
445
446 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700447 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700448 // If the user leaves launcher, then we should just load items asynchronously when
449 // they return.
450 mModel.startLoader(true, -1);
451 } else {
452 // We only load the page synchronously if the user rotates (or triggers a
453 // configuration change) while launcher is in the foreground
454 mModel.startLoader(true, mWorkspace.getCurrentPage());
455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457
458 // For handling default keys
459 mDefaultKeySsb = new SpannableStringBuilder();
460 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800461
462 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
463 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800464
Adam Cohenf9426d52012-06-04 17:26:21 -0700465 updateGlobalIcons();
466
467 // On large interfaces, we want the screen to auto-rotate based on the current orientation
468 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700469
470 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700471 }
472
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 protected void onUserLeaveHint() {
474 super.onUserLeaveHint();
475 sPausedFromUserAction = true;
476 }
477
Winson Chung98ca0f72013-07-29 12:58:51 -0700478 /** To be overriden by subclasses to hint to Launcher that we have custom content */
479 protected boolean hasCustomContentToLeft() {
480 return false;
481 }
482
Dave Hawkeya8881582013-09-17 15:55:33 -0600483 /**
484 * To be overridden by subclasses to create the custom content and call
485 * {@link #addToCustomContentPage}. This will only be invoked if
486 * {@link #hasCustomContentToLeft()} is {@code true}.
487 */
488 protected void addCustomContentToLeft() {
489 }
490
491 /**
492 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
493 * ensure the custom content page is added or removed if necessary.
494 */
495 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600496 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600497 // Not bound yet, wait for bindScreens to be called.
498 return;
499 }
500
501 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
502 // Create the custom content page and call the subclass to populate it.
503 mWorkspace.createCustomContentPage();
504 addCustomContentToLeft();
505 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
506 mWorkspace.removeCustomContentPage();
507 }
508 }
509
Adam Cohenf9426d52012-06-04 17:26:21 -0700510 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700511 boolean searchVisible = false;
512 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800513 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700514 int coi = getCurrentOrientationIndexForGlobalIcons();
515 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
516 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700517 if (!DISABLE_MARKET_BUTTON) {
518 updateAppMarketIcon();
519 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700520 searchVisible = updateGlobalSearchIcon();
521 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700522 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700523 if (sGlobalSearchIcon[coi] != null) {
524 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700525 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700526 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700527 if (sVoiceSearchIcon[coi] != null) {
528 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700529 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700530 }
Winson Chungd64a6662013-09-30 11:06:59 -0700531 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700532 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800533 }
Winson Chungadf0c182012-08-23 14:59:07 -0700534 if (mSearchDropTargetBar != null) {
535 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 }
Romain Guycbb89e42009-06-08 15:52:54 -0700538
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700540 if (sLocaleConfiguration == null) {
541 new AsyncTask<Void, Void, LocaleConfiguration>() {
542 @Override
543 protected LocaleConfiguration doInBackground(Void... unused) {
544 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
545 readConfiguration(Launcher.this, localeConfiguration);
546 return localeConfiguration;
547 }
548
549 @Override
550 protected void onPostExecute(LocaleConfiguration result) {
551 sLocaleConfiguration = result;
552 checkForLocaleChange(); // recursive, but now with a locale configuration
553 }
554 }.execute();
555 return;
556 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 final Configuration configuration = getResources().getConfiguration();
559
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700560 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 final String locale = configuration.locale.toString();
562
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700563 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 final int mcc = configuration.mcc;
565
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700566 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 final int mnc = configuration.mnc;
568
Romain Guy84f296c2009-11-04 15:00:44 -0800569 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570
Romain Guy84f296c2009-11-04 15:00:44 -0800571 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700572 sLocaleConfiguration.locale = locale;
573 sLocaleConfiguration.mcc = mcc;
574 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700575
Joe Onorato0589f0f2010-02-08 13:44:00 -0800576 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700577
578 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
579 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700580 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581 public void run() {
582 writeConfiguration(Launcher.this, localeConfiguration);
583 }
584 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700585 }
586 }
587
588 private static class LocaleConfiguration {
589 public String locale;
590 public int mcc = -1;
591 public int mnc = -1;
592 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700593
Romain Guy98d01652009-06-30 16:21:04 -0700594 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
595 DataInputStream in = null;
596 try {
597 in = new DataInputStream(context.openFileInput(PREFERENCES));
598 configuration.locale = in.readUTF();
599 configuration.mcc = in.readInt();
600 configuration.mnc = in.readInt();
601 } catch (FileNotFoundException e) {
602 // Ignore
603 } catch (IOException e) {
604 // Ignore
605 } finally {
606 if (in != null) {
607 try {
608 in.close();
609 } catch (IOException e) {
610 // Ignore
611 }
612 }
613 }
614 }
615
616 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
617 DataOutputStream out = null;
618 try {
619 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
620 out.writeUTF(configuration.locale);
621 out.writeInt(configuration.mcc);
622 out.writeInt(configuration.mnc);
623 out.flush();
624 } catch (FileNotFoundException e) {
625 // Ignore
626 } catch (IOException e) {
627 //noinspection ResultOfMethodCallIgnored
628 context.getFileStreamPath(PREFERENCES).delete();
629 } finally {
630 if (out != null) {
631 try {
632 out.close();
633 } catch (IOException e) {
634 // Ignore
635 }
636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
638 }
639
Anton Hanssona2f665f2013-09-26 12:18:32 +0100640 public Stats getStats() {
641 return mStats;
642 }
643
Bjorn Bringertc459e522013-06-07 19:36:01 +0100644 public LayoutInflater getInflater() {
645 return mInflater;
646 }
647
Adam Cohen716b51e2011-06-30 12:09:54 -0700648 public DragLayer getDragLayer() {
649 return mDragLayer;
650 }
651
Winson Chung36a62fe2012-05-06 18:04:42 -0700652 boolean isDraggingEnabled() {
653 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
654 // that is subsequently removed from the workspace in startBinding().
655 return !mModel.isLoadingWorkspace();
656 }
657
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 static int getScreen() {
659 synchronized (sLock) {
660 return sScreen;
661 }
662 }
663
664 static void setScreen(int screen) {
665 synchronized (sLock) {
666 sScreen = screen;
667 }
668 }
669
Winson Chung557d6ed2011-07-08 15:34:52 -0700670 /**
671 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
672 * a configuration step, this allows the proper animations to run after other transitions.
673 */
674 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700675 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800676 switch (args.requestCode) {
677 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700678 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700679 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 break;
681 case REQUEST_PICK_SHORTCUT:
682 processShortcut(args.intent);
683 break;
684 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700685 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700686 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700687 result = true;
688 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800689 case REQUEST_CREATE_APPWIDGET:
690 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700692 result = true;
693 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 }
Michael Jurka27614d22012-04-02 06:40:22 -0700695 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
696 // if you turned the screen off and then back while in All Apps, Launcher would not
697 // return to the workspace. Clearing mAddInfo.container here fixes this issue
698 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700699 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 }
701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 protected void onActivityResult(
704 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700705 // Reset the startActivity waiting flag
706 mWaitingForResult = false;
707
Adam Cohenad4e15c2013-10-17 16:21:35 -0700708 Runnable exitSpringLoaded = new Runnable() {
709 @Override
710 public void run() {
711 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
712 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
713 }
714 };
715
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700717 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
719 if (resultCode == RESULT_CANCELED) {
720 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
722 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
725 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700726 }
727 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200728 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
729 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
730 mWorkspace.exitOverviewMode(false);
731 }
732 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200734
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
736 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700737
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 // We have special handling for widgets
739 if (isWidgetDrop) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700740 final int appWidgetId = data != null ?
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700742 final int result;
743 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800744 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700745 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
746 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 result = RESULT_CANCELED;
748 completeTwoStageWidgetDrop(result, appWidgetId);
749 onComplete = new Runnable() {
750 @Override
751 public void run() {
752 exitSpringLoadedDragModeDelayed(false, 0, null);
753 }
754 };
Winson Chung5aaab772012-04-27 15:24:02 -0700755 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 result = resultCode;
757 onComplete = new Runnable() {
758 @Override
759 public void run() {
760 completeTwoStageWidgetDrop(result, appWidgetId);
761 }
762 };
Winson Chung5aaab772012-04-27 15:24:02 -0700763 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
765 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800766 return;
767 }
768
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 // The pattern used here is that a user PICKs a specific application,
770 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700771
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
773 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700774 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800775 final PendingAddArguments args = new PendingAddArguments();
776 args.requestCode = requestCode;
777 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700778 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700779 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700780 args.cellX = mPendingAddInfo.cellX;
781 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800782 if (isWorkspaceLocked()) {
783 sPendingAddList.add(args);
784 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700785 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
788 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700789 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
791 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800793 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800794 }
795
796 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700797 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800799 Runnable onCompleteRunnable = null;
800 int animationType = 0;
801
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700802 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800803 if (resultCode == RESULT_OK) {
804 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
805 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700806 mPendingAddWidgetInfo);
807 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 onCompleteRunnable = new Runnable() {
809 @Override
810 public void run() {
811 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700812 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700813 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
814 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800815 }
816 };
817 } else if (resultCode == RESULT_CANCELED) {
818 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800819 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700820 if (mDragLayer.getAnimatedView() != null) {
821 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
822 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
823 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700824 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700825 // The animated view may be null in the case of a rotation during widget configuration
826 onCompleteRunnable.run();
827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 }
829
830 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700831 protected void onStop() {
832 super.onStop();
833 FirstFrameAnimatorHelper.setIsVisible(false);
834 }
835
836 @Override
837 protected void onStart() {
838 super.onStart();
839 FirstFrameAnimatorHelper.setIsVisible(true);
840 }
841
842 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200844 long startTime = 0;
845 if (DEBUG_RESUME_TIME) {
846 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400847 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700850
Winson Chung4a2afa32012-07-19 14:53:05 -0700851 // Restore the previous launcher state
852 if (mOnResumeState == State.WORKSPACE) {
853 showWorkspace(false);
854 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung82963d52013-10-09 11:20:57 -0700855 showAllApps(false, AppsCustomizePagedView.ContentType.Applications, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700856 }
857 mOnResumeState = State.NONE;
858
Craig Mautner360310b2012-10-26 15:13:08 -0700859 // Background was set to gradient in onPause(), restore to black if in all apps.
860 setWorkspaceBackground(mState == State.WORKSPACE);
861
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800862 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700863 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700864 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400865 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700866 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400867 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700868 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700870 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200871 // We might have postponed some bind calls until onResume (see waitUntilResume) --
872 // execute them here
873 long startTimeCallbacks = 0;
874 if (DEBUG_RESUME_TIME) {
875 startTimeCallbacks = System.currentTimeMillis();
876 }
877
878 if (mAppsCustomizeContent != null) {
879 mAppsCustomizeContent.setBulkBind(true);
880 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700881 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
882 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200883 }
884 if (mAppsCustomizeContent != null) {
885 mAppsCustomizeContent.setBulkBind(false);
886 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700887 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200888 if (DEBUG_RESUME_TIME) {
889 Log.d(TAG, "Time spent processing callbacks in onResume: " +
890 (System.currentTimeMillis() - startTimeCallbacks));
891 }
Michael Jurka447bf842013-05-15 14:52:15 +0200892 }
Michael Jurka54554252013-08-01 12:52:23 +0200893 if (mOnResumeCallbacks.size() > 0) {
894 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
895 mOnResumeCallbacks.get(i).run();
896 }
897 mOnResumeCallbacks.clear();
898 }
Winson Chunge4e50662012-01-23 14:45:13 -0800899
900 // Reset the pressed state of icons that were locked in the press state while activities
901 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800902 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800903 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800904 mWaitingForResume.setStayPressed(false);
905 }
Winson Chunge4e50662012-01-23 14:45:13 -0800906 if (mAppsCustomizeContent != null) {
907 // Resets the previous all apps icon press state
908 mAppsCustomizeContent.resetDrawableState();
909 }
Winson Chung82963d52013-10-09 11:20:57 -0700910
Adam Cohen06dff352012-06-01 17:17:08 -0700911 // It is possible that widgets can receive updates while launcher is not in the foreground.
912 // Consequently, the widgets will be inflated in the orientation of the foreground activity
913 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
914 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700915 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700916
Winson Chung780fe592013-09-26 14:48:44 -0700917 // Process any items that were added while Launcher was away.
918 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
919
Winson Chung5841efa2013-09-30 18:06:44 -0700920 // Update the voice search button proxy
921 updateVoiceButtonProxyVisible(false);
922
Adam Cohenf9426d52012-06-04 17:26:21 -0700923 // Again, as with the above scenario, it's possible that one or more of the global icons
924 // were updated in the wrong orientation.
925 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200926 if (DEBUG_RESUME_TIME) {
927 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
928 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700929
930 if (mWorkspace.getCustomContentCallbacks() != null) {
931 // If we are resuming and the custom content is the current page, we call onShow().
932 // It is also poassible that onShow will instead be called slightly after first layout
933 // if PagedView#setRestorePage was set to the custom content page in onCreate().
934 if (mWorkspace.isOnOrMovingToCustomContent()) {
935 mWorkspace.getCustomContentCallbacks().onShow();
936 }
937 }
Adam Cohenedaaa302013-10-01 17:33:27 -0700938 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -0700939 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -0700940 }
941
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700943 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700944 // Ensure that items added to Launcher are queued until Launcher returns
945 InstallShortcutReceiver.enableInstallQueue();
946
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700947 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700948 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800949 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700950 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700951
952 // We call onHide() aggressively. The custom content callbacks should be able to
953 // debounce excess onHide calls.
954 if (mWorkspace.getCustomContentCallbacks() != null) {
955 mWorkspace.getCustomContentCallbacks().onHide();
956 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700957 }
Romain Guycbb89e42009-06-08 15:52:54 -0700958
Adam Cohen66a01fd2013-06-11 12:48:00 -0700959 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -0700960 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600961 addCustomContentToLeft();
962 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700963 }
964
Adam Cohenbffe7452013-07-22 18:21:45 -0700965 QSBScroller mQsbScroller = new QSBScroller() {
966 int scrollY = 0;
967
968 @Override
969 public void setScrollY(int scroll) {
970 scrollY = scroll;
971
972 if (mWorkspace.isOnOrMovingToCustomContent()) {
973 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +0100974 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -0700975 }
976 }
977 };
978
979 public void resetQSBScroll() {
980 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +0100981 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -0700982 }
983
984 public interface CustomContentCallbacks {
985 // Custom content is completely shown
986 public void onShow();
987
988 // Custom content is completely hidden
989 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700990
991 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
992 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700993 }
994
Adam Cohen30bacb22013-08-29 14:25:25 -0700995 protected void startSettings() {
996 }
997
Adam Cohenbffe7452013-07-22 18:21:45 -0700998 public interface QSBScroller {
999 public void setScrollY(int scrollY);
1000 }
1001
Winson Chung98ca0f72013-07-29 12:58:51 -07001002 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001003 CustomContentCallbacks callbacks, String description) {
1004 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001005 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001006 }
1007
Adam Cohenedb40762013-07-18 16:45:45 -07001008 // The custom content needs to offset its content to account for the QSB
1009 public int getTopOffsetForCustomContent() {
1010 return mWorkspace.getPaddingTop();
1011 }
1012
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001013 @Override
1014 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 // Flag the loader to stop early before switching
1016 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001017 if (mAppsCustomizeContent != null) {
1018 mAppsCustomizeContent.surrender();
1019 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001020 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001021 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001022
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001023 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001024 @Override
1025 public void onWindowFocusChanged(boolean hasFocus) {
1026 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001027 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001028 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 private boolean acceptFilter() {
1031 final InputMethodManager inputManager = (InputMethodManager)
1032 getSystemService(Context.INPUT_METHOD_SERVICE);
1033 return !inputManager.isFullscreenMode();
1034 }
1035
1036 @Override
1037 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001038 final int uniChar = event.getUnicodeChar();
1039 final boolean handled = super.onKeyDown(keyCode, event);
1040 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1041 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1043 keyCode, event);
1044 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001045 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001046 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001047 // showSearchDialog()
1048 // If there are multiple keystrokes before the search dialog takes focus,
1049 // onSearchRequested() will be called for every keystroke,
1050 // but it is idempotent, so it's fine.
1051 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
1053 }
1054
Joe Onorato8a9625e2010-01-28 15:55:35 -08001055 // Eat the long press event so the keyboard doesn't come up.
1056 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1057 return true;
1058 }
1059
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 return handled;
1061 }
1062
Karl Rosaen138a0412009-04-23 19:00:21 -07001063 private String getTypedText() {
1064 return mDefaultKeySsb.toString();
1065 }
1066
1067 private void clearTypedText() {
1068 mDefaultKeySsb.clear();
1069 mDefaultKeySsb.clearSpans();
1070 Selection.setSelection(mDefaultKeySsb, 0);
1071 }
1072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001074 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1075 * State
1076 */
1077 private static State intToState(int stateOrdinal) {
1078 State state = State.WORKSPACE;
1079 final State[] stateValues = State.values();
1080 for (int i = 0; i < stateValues.length; i++) {
1081 if (stateValues[i].ordinal() == stateOrdinal) {
1082 state = stateValues[i];
1083 break;
1084 }
1085 }
1086 return state;
1087 }
1088
1089 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 * Restores the previous state, if it exists.
1091 *
1092 * @param savedState The previous state.
1093 */
1094 private void restoreState(Bundle savedState) {
1095 if (savedState == null) {
1096 return;
1097 }
1098
Michael Jurka883f55b2010-10-21 15:47:14 -07001099 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001100 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001101 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001102 }
1103
Adam Cohen21cd0022013-10-09 18:57:02 -07001104 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1105 PagedView.INVALID_RESTORE_PAGE);
1106 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001107 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 }
1109
Winson Chung3d503fb2011-07-13 17:25:49 -07001110 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001111 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001112
Winson Chung3d503fb2011-07-13 17:25:49 -07001113 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1114 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001116 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1117 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001118 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1119 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1120 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001121 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 mRestoring = true;
1123 }
1124
1125 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1126 if (renameFolder) {
1127 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001128 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 mRestoring = true;
1130 }
Winson Chunga12a2502010-12-20 14:41:35 -08001131
Winson Chung785d2eb2011-04-14 16:08:02 -07001132 // Restore the AppsCustomize tab
1133 if (mAppsCustomizeTabHost != null) {
1134 String curTab = savedState.getString("apps_customize_currentTab");
1135 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001136 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001137 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001138 mAppsCustomizeContent.loadAssociatedPages(
1139 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001140 }
1141
Winson Chung5afbf7b2011-07-25 11:53:08 -07001142 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1143 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 }
1146
1147 /**
1148 * Finds all the views we need and configure them properly.
1149 */
1150 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001151 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001152
Craig Mautner360310b2012-10-26 15:13:08 -07001153 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001154 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1155 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001156
John Spurlock77e1f472013-09-11 10:09:51 -04001157 mLauncherView.setSystemUiVisibility(
1158 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001159 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160
Winson Chung4c98d922011-05-31 16:50:48 -07001161 // Setup the drag layer
1162 mDragLayer.setup(this, dragController);
1163
Winson Chung3d503fb2011-07-13 17:25:49 -07001164 // Setup the hotseat
1165 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1166 if (mHotseat != null) {
1167 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001168 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001169 }
1170
Adam Cohenf358a4b2013-07-23 16:47:31 -07001171 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001172 View widgetButton = findViewById(R.id.widget_button);
1173 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001174 @Override
1175 public void onClick(View arg0) {
Winson Chung82963d52013-10-09 11:20:57 -07001176 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001177 }
1178 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001179 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1180
1181 View wallpaperButton = findViewById(R.id.wallpaper_button);
1182 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001183 @Override
1184 public void onClick(View arg0) {
1185 startWallpaper();
1186 }
1187 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001188 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1189
1190 View settingsButton = findViewById(R.id.settings_button);
1191 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001192 @Override
1193 public void onClick(View arg0) {
1194 startSettings();
1195 }
1196 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001197 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001198 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001199
Winson Chung4c98d922011-05-31 16:50:48 -07001200 // Setup the workspace
1201 mWorkspace.setHapticFeedbackEnabled(false);
1202 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001203 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001204 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001205
Winson Chungf0ea4d32011-06-06 14:27:16 -07001206 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001207 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001208
Winson Chungf0ea4d32011-06-06 14:27:16 -07001209 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001210 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001211 mAppsCustomizeContent = (AppsCustomizePagedView)
1212 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1213 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001214
Winson Chung3d503fb2011-07-13 17:25:49 -07001215 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001216 dragController.setDragScoller(mWorkspace);
1217 dragController.setScrollView(mDragLayer);
1218 dragController.setMoveTarget(mWorkspace);
1219 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001220 if (mSearchDropTargetBar != null) {
1221 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001222 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001223
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001224 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001225 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001226 mWeightWatcher = new WeightWatcher(this);
1227 mWeightWatcher.setAlpha(0.5f);
1228 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001229 new FrameLayout.LayoutParams(
1230 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001231 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001232 Gravity.BOTTOM)
1233 );
Adam Cohen39a06042013-07-19 14:30:12 -07001234
1235 boolean show = shouldShowWeightWatcher();
1236 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001237 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 }
1239
1240 /**
1241 * Creates a view representing a shortcut.
1242 *
1243 * @param info The data structure describing the shortcut.
1244 *
1245 * @return A View inflated from R.layout.application.
1246 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001247 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001249 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
1251
1252 /**
1253 * Creates a view representing a shortcut inflated from the specified resource.
1254 *
1255 * @param layoutResId The id of the XML layout used to create the shortcut.
1256 * @param parent The group the shortcut belongs to.
1257 * @param info The data structure describing the shortcut.
1258 *
1259 * @return A View inflated from layoutResId.
1260 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001261 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001262 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1263 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 return favorite;
1266 }
1267
1268 /**
1269 * Add an application shortcut to the workspace.
1270 *
1271 * @param data The intent describing the application.
1272 * @param cellInfo The position on screen where to create the shortcut.
1273 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001274 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001275 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001276 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001277
Adam Cohenfbba09b2011-07-18 21:43:05 -07001278 // First we check if we already know the exact location where we want to add this item.
1279 if (cellX >= 0 && cellY >= 0) {
1280 cellXY[0] = cellX;
1281 cellXY[1] = cellY;
1282 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001283 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001284 return;
1285 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001287 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001288
Romain Guy73b979d2009-06-09 12:57:21 -07001289 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001290 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001292 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001293 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001294 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001295 } else {
1296 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
1298 }
Romain Guycbb89e42009-06-08 15:52:54 -07001299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 /**
1301 * Add a shortcut to the workspace.
1302 *
1303 * @param data The intent describing the shortcut.
1304 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001306 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001307 int cellY) {
1308 int[] cellXY = mTmpAddItemCellCoordinates;
1309 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001310 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001311
Michael Jurkad3ef3062010-11-23 16:23:58 -08001312 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001313
Adam Cohen558baaf2011-08-15 15:22:57 -07001314 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001315 if (info == null) {
1316 return;
1317 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001318 final View view = createShortcut(info);
1319
Adam Cohenfbba09b2011-07-18 21:43:05 -07001320 // First we check if we already know the exact location where we want to add this item.
1321 if (cellX >= 0 && cellY >= 0) {
1322 cellXY[0] = cellX;
1323 cellXY[1] = cellY;
1324 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001325
1326 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001327 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001328 true, null,null)) {
1329 return;
1330 }
1331 DragObject dragObject = new DragObject();
1332 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001333 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1334 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001335 return;
1336 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001337 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001338 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001340 foundCellSpan = (result != null);
1341 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001342 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001343 }
1344
1345 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001346 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001347 return;
1348 }
1349
Adam Cohendcd297f2013-06-18 13:13:40 -07001350 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351
1352 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001353 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356 }
1357
Adam Cohen2f093b62012-04-30 18:59:53 -07001358 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1359 int minHeight) {
1360 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001361 // We want to account for the extra amount of padding that we are adding to the widget
1362 // to ensure that it gets the full amount of space that it has requested
1363 int requiredWidth = minWidth + padding.left + padding.right;
1364 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001365 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001366 }
1367
Adam Cohen2f093b62012-04-30 18:59:53 -07001368 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1369 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001370 }
1371
Adam Cohen2f093b62012-04-30 18:59:53 -07001372 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1373 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001374 }
1375
Adam Cohen2f093b62012-04-30 18:59:53 -07001376 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1377 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001378 }
1379
Adam Cohen2f093b62012-04-30 18:59:53 -07001380 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1381 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001382 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001383 }
1384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001386 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001388 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001389 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001391 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001392 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1393 if (appWidgetInfo == null) {
1394 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1395 }
Romain Guycbb89e42009-06-08 15:52:54 -07001396
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001397 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001398 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001399
Adam Cohen2f093b62012-04-30 18:59:53 -07001400 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1401 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001404 // We have saved the position to which the widget was dragged-- this really only matters
1405 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001406 int[] cellXY = mTmpAddItemCellCoordinates;
1407 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001408 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001409 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001410 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1411 cellXY[0] = mPendingAddInfo.cellX;
1412 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001413 spanXY[0] = mPendingAddInfo.spanX;
1414 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001415 foundCellSpan = true;
1416 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001417 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001418 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001419 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1420 spanXY[1], cellXY, finalSpan);
1421 spanXY[0] = finalSpan[0];
1422 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001423 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001424 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001425 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001426 }
1427
Michael Jurka0280c3b2010-09-17 15:00:07 -07001428 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001429 if (appWidgetId != -1) {
1430 // Deleting an app widget ID is a void call but writes to disk before returning
1431 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001432 new Thread("deleteAppWidgetId") {
1433 public void run() {
1434 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1435 }
1436 }.start();
1437 }
Winson Chung93eef082012-03-23 15:59:27 -07001438 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001439 return;
1440 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001442 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001443 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1444 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001445 launcherInfo.spanX = spanXY[0];
1446 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001447 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1448 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001451 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452
1453 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001454 if (hostView == null) {
1455 // Perform actual inflation because we're live
1456 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1457 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1458 } else {
1459 // The AppWidgetHostView has already been inflated and instantiated
1460 launcherInfo.hostView = hostView;
1461 }
Romain Guycbb89e42009-06-08 15:52:54 -07001462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001464 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001465 launcherInfo.notifyWidgetSizeChanged(this);
1466
Adam Cohendcd297f2013-06-18 13:13:40 -07001467 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001468 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001469
1470 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001472 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
Romain Guycbb89e42009-06-08 15:52:54 -07001474
Adam Cohended9f8d2010-11-03 13:25:16 -07001475 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1476 @Override
1477 public void onReceive(Context context, Intent intent) {
1478 final String action = intent.getAction();
1479 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1480 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001481 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001482 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001483
Winson Chungc100e8e2011-08-09 16:02:43 -07001484 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001485 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001486 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001487 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001488 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001489 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1490 mUserPresent = true;
1491 updateRunning();
1492 }
1493 }
1494 };
1495
1496 @Override
1497 public void onAttachedToWindow() {
1498 super.onAttachedToWindow();
1499
1500 // Listen for broadcasts related to user-presence
1501 final IntentFilter filter = new IntentFilter();
1502 filter.addAction(Intent.ACTION_SCREEN_OFF);
1503 filter.addAction(Intent.ACTION_USER_PRESENT);
1504 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001505 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001506 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001507 mVisible = true;
1508 }
1509
1510 @Override
1511 public void onDetachedFromWindow() {
1512 super.onDetachedFromWindow();
1513 mVisible = false;
1514
Adam Cohend113e0c2010-11-11 10:48:05 -08001515 if (mAttached) {
1516 unregisterReceiver(mReceiver);
1517 mAttached = false;
1518 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001519 updateRunning();
1520 }
1521
1522 public void onWindowVisibilityChanged(int visibility) {
1523 mVisible = visibility == View.VISIBLE;
1524 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001525 // The following code used to be in onResume, but it turns out onResume is called when
1526 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1527 // is a more appropriate event to handle
1528 if (mVisible) {
1529 mAppsCustomizeTabHost.onWindowVisible();
1530 if (!mWorkspaceLoading) {
1531 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001532 // We want to let Launcher draw itself at least once before we force it to build
1533 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001534 // apps is nice and speedy.
1535 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001536 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001537 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001538 if (mStarted) return;
1539 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001540 // We delay the layer building a bit in order to give
1541 // other message processing a time to run. In particular
1542 // this avoids a delay in hiding the IME if it was
1543 // currently shown, because doing that may involve
1544 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001545 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001546 final ViewTreeObserver.OnDrawListener listener = this;
1547 mWorkspace.post(new Runnable() {
1548 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001549 if (mWorkspace != null &&
1550 mWorkspace.getViewTreeObserver() != null) {
1551 mWorkspace.getViewTreeObserver().
1552 removeOnDrawListener(listener);
1553 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001554 }
1555 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001556 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001557 }
1558 });
1559 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001560 // When Launcher comes back to foreground, a different Activity might be responsible for
1561 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001562 if (!DISABLE_MARKET_BUTTON) {
1563 updateAppMarketIcon();
1564 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001565 clearTypedText();
1566 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001567 }
1568
1569 private void sendAdvanceMessage(long delay) {
1570 mHandler.removeMessages(ADVANCE_MSG);
1571 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1572 mHandler.sendMessageDelayed(msg, delay);
1573 mAutoAdvanceSentTime = System.currentTimeMillis();
1574 }
1575
1576 private void updateRunning() {
1577 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1578 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1579 mAutoAdvanceRunning = autoAdvanceRunning;
1580 if (autoAdvanceRunning) {
1581 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1582 sendAdvanceMessage(delay);
1583 } else {
1584 if (!mWidgetsToAdvance.isEmpty()) {
1585 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1586 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1587 }
1588 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001589 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 }
1591 }
1592 }
1593
1594 private final Handler mHandler = new Handler() {
1595 @Override
1596 public void handleMessage(Message msg) {
1597 if (msg.what == ADVANCE_MSG) {
1598 int i = 0;
1599 for (View key: mWidgetsToAdvance.keySet()) {
1600 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1601 final int delay = mAdvanceStagger * i;
1602 if (v instanceof Advanceable) {
1603 postDelayed(new Runnable() {
1604 public void run() {
1605 ((Advanceable) v).advance();
1606 }
1607 }, delay);
1608 }
1609 i++;
1610 }
1611 sendAdvanceMessage(mAdvanceInterval);
1612 }
1613 }
1614 };
1615
1616 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001617 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001618 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1619 if (v instanceof Advanceable) {
1620 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001621 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001622 updateRunning();
1623 }
1624 }
1625
1626 void removeWidgetToAutoAdvance(View hostView) {
1627 if (mWidgetsToAdvance.containsKey(hostView)) {
1628 mWidgetsToAdvance.remove(hostView);
1629 updateRunning();
1630 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001631 }
1632
Joe Onorato9c1289c2009-08-17 11:03:03 -04001633 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001635 launcherInfo.hostView = null;
1636 }
1637
Winson Chung93eef082012-03-23 15:59:27 -07001638 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1639 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1640 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001641 }
1642
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001643 public LauncherAppWidgetHost getAppWidgetHost() {
1644 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
Romain Guycbb89e42009-06-08 15:52:54 -07001646
Winson Chunga9abd0e2010-10-27 17:18:37 -07001647 public LauncherModel getModel() {
1648 return mModel;
1649 }
1650
Bjorn Bringertc459e522013-06-07 19:36:01 +01001651 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001652 getWindow().closeAllPanels();
1653
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001654 // Whatever we were doing is hereby canceled.
1655 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001656 }
1657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 @Override
1659 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001660 long startTime = 0;
1661 if (DEBUG_RESUME_TIME) {
1662 startTime = System.currentTimeMillis();
1663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 super.onNewIntent(intent);
1665
1666 // Close the menu
1667 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001668 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001669 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001670
Adam Cohened307df2013-10-02 09:37:31 -07001671 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1672 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1673 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001674
Adam Cohen6fecd412013-10-02 17:41:50 -07001675 if (mWorkspace == null) {
1676 // Can be cases where mWorkspace is null, this prevents a NPE
1677 return;
1678 }
1679 Folder openFolder = mWorkspace.getOpenFolder();
1680 // In all these cases, only animate if we're already on home
1681 mWorkspace.exitWidgetResizeMode();
1682 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1683 openFolder == null) {
1684 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001685 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001686
Adam Cohen6fecd412013-10-02 17:41:50 -07001687 closeFolder();
1688 exitSpringLoadedDragMode();
1689
1690 // If we are already on home, then just animate back to the workspace,
1691 // otherwise, just wait until onResume to set the state back to Workspace
1692 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001693 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001694 } else {
1695 mOnResumeState = State.WORKSPACE;
1696 }
1697
1698 final View v = getWindow().peekDecorView();
1699 if (v != null && v.getWindowToken() != null) {
1700 InputMethodManager imm = (InputMethodManager)getSystemService(
1701 INPUT_METHOD_SERVICE);
1702 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1703 }
1704
1705 // Reset the apps customize page
1706 if (mAppsCustomizeTabHost != null) {
1707 mAppsCustomizeTabHost.reset();
1708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 }
Adam Cohened307df2013-10-02 09:37:31 -07001710
Michael Jurka447bf842013-05-15 14:52:15 +02001711 if (DEBUG_RESUME_TIME) {
1712 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 }
1715
1716 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001717 public void onRestoreInstanceState(Bundle state) {
1718 super.onRestoreInstanceState(state);
1719 for (int page: mSynchronouslyBoundPages) {
1720 mWorkspace.restoreInstanceStateForChild(page);
1721 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 }
1723
1724 @Override
1725 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001726 if (mWorkspace.getChildCount() > 0) {
1727 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getRestorePage());
1728 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001729 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730
Michael Jurka883f55b2010-10-21 15:47:14 -07001731 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001732 // We close any open folder since it will not be re-opened, and we need to make sure
1733 // this state is reflected.
1734 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735
Adam Cohendcd297f2013-06-18 13:13:40 -07001736 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001737 mWaitingForResult) {
1738 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001739 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001740 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1741 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001742 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1743 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1744 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 }
1746
1747 if (mFolderInfo != null && mWaitingForResult) {
1748 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1749 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1750 }
Michael Jurka946ad472010-07-09 18:05:18 -07001751
Winson Chung785d2eb2011-04-14 16:08:02 -07001752 // Save the current AppsCustomize tab
1753 if (mAppsCustomizeTabHost != null) {
1754 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1755 if (currentTabTag != null) {
1756 outState.putString("apps_customize_currentTab", currentTabTag);
1757 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001758 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1759 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 }
1762
1763 @Override
1764 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001766
Winson Chunge7a03942011-08-05 15:05:12 -07001767 // Remove all pending runnables
1768 mHandler.removeMessages(ADVANCE_MSG);
1769 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001770 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001771
Winson Chungcd2b0142011-06-08 16:02:26 -07001772 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001773 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001774 mModel.stopLoader();
1775 app.setLauncher(null);
1776
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001778 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001780 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001782 mAppWidgetHost = null;
1783
1784 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785
1786 TextKeyListener.getInstance().release();
1787
Winson Chung81b52252012-08-27 15:34:29 -07001788 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1789 // to prevent leaking Launcher activities on orientation change.
1790 if (mModel != null) {
1791 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1792 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001793
1794 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001795 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001796
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001797 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001798 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1799 mWorkspace.removeAllViews();
1800 mWorkspace = null;
1801 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001802
Michael Jurka2ecf9952012-06-18 12:52:28 -07001803 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 }
1805
Adam Cohena9cf38f2011-05-02 15:36:58 -07001806 public DragController getDragController() {
1807 return mDragController;
1808 }
1809
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 @Override
1811 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001812 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 super.startActivityForResult(intent, requestCode);
1814 }
1815
Winson Chung70d72102011-08-12 11:24:35 -07001816 /**
1817 * Indicates that we want global search for this activity by setting the globalSearch
1818 * argument for {@link #startSearch} to true.
1819 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001821 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001823
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001824 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001825
Karl Rosaen138a0412009-04-23 19:00:21 -07001826 if (initialQuery == null) {
1827 // Use any text typed in the launcher as the initial query
1828 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 if (appSearchData == null) {
1831 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001832 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 }
Winson Chungadf0c182012-08-23 14:59:07 -07001834 Rect sourceBounds = new Rect();
1835 if (mSearchDropTargetBar != null) {
1836 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1837 }
Romain Guycbb89e42009-06-08 15:52:54 -07001838
Bjorn Bringertc459e522013-06-07 19:36:01 +01001839 startSearch(initialQuery, selectInitialQuery,
1840 appSearchData, sourceBounds);
1841 }
1842
1843 public void startSearch(String initialQuery,
1844 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001845 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001846 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001847 }
1848
1849 /**
1850 * Starts the global search activity. This code is a copied from SearchManager
1851 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001852 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001853 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001854 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001855 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1856 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1857 if (globalSearchActivity == null) {
1858 Log.w(TAG, "No global search activity found.");
1859 return;
1860 }
1861 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1862 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1863 intent.setComponent(globalSearchActivity);
1864 // Make sure that we have a Bundle to put source in
1865 if (appSearchData == null) {
1866 appSearchData = new Bundle();
1867 } else {
1868 appSearchData = new Bundle(appSearchData);
1869 }
1870 // Set source to package name of app that starts global search, if not set already.
1871 if (!appSearchData.containsKey("source")) {
1872 appSearchData.putString("source", getPackageName());
1873 }
1874 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1875 if (!TextUtils.isEmpty(initialQuery)) {
1876 intent.putExtra(SearchManager.QUERY, initialQuery);
1877 }
1878 if (selectInitialQuery) {
1879 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1880 }
1881 intent.setSourceBounds(sourceBounds);
1882 try {
1883 startActivity(intent);
1884 } catch (ActivityNotFoundException ex) {
1885 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
1888
Winson Chung70d72102011-08-12 11:24:35 -07001889 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001890 public boolean onPrepareOptionsMenu(Menu menu) {
1891 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001892 if (!mWorkspace.isInOverviewMode()) {
1893 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001894 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001895 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001898 @Override
1899 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001900 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001901 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001902 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001903 }
1904
Joe Onorato9c1289c2009-08-17 11:03:03 -04001905 public boolean isWorkspaceLocked() {
1906 return mWorkspaceLoading || mWaitingForResult;
1907 }
1908
Michael Jurka0280c3b2010-09-17 15:00:07 -07001909 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001910 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001911 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001912 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1913 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001914 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001915 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001916 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001917
Adam Cohenad4e15c2013-10-17 16:21:35 -07001918 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1919 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
1920 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1921 }
1922
1923 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1924 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
1925 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001926 if (appWidgetInfo.configure != null) {
1927 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001928
Bjorn Bringert7984c942009-12-09 15:38:25 +00001929 // Launch over to configure widget, if needed
1930 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001931 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001932 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001933 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001935 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001936 Runnable onComplete = new Runnable() {
1937 @Override
1938 public void run() {
1939 // Exit spring loaded mode if necessary after adding the widget
1940 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1941 null);
1942 }
1943 };
Adam Cohendcd297f2013-06-18 13:13:40 -07001944 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001945 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001946 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948 }
Romain Guycbb89e42009-06-08 15:52:54 -07001949
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001950 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001951 // Close any folders that may be open.
1952 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001953 mWorkspace.moveToCustomContentScreen(animate);
1954 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001955 /**
1956 * Process a shortcut drop.
1957 *
1958 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001959 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001960 * @param cell The cell it should be added to, optional
1961 * @param position The location on the screen where it was dropped, optional
1962 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001964 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001965 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001968 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001969
1970 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001971 mPendingAddInfo.cellX = cell[0];
1972 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001973 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001974
1975 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1976 createShortcutIntent.setComponent(componentName);
1977 processShortcut(createShortcutIntent);
1978 }
1979
Adam Cohenfbba09b2011-07-18 21:43:05 -07001980 /**
1981 * Process a widget drop.
1982 *
1983 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001984 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001985 * @param cell The cell it should be added to, optional
1986 * @param position The location on the screen where it was dropped, optional
1987 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001989 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001990 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001992 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001993 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001994 mPendingAddInfo.minSpanX = info.minSpanX;
1995 mPendingAddInfo.minSpanY = info.minSpanY;
1996
Adam Cohenfbba09b2011-07-18 21:43:05 -07001997 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 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002001 if (span != null) {
2002 mPendingAddInfo.spanX = span[0];
2003 mPendingAddInfo.spanY = span[1];
2004 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002005
Adam Cohened66b2b2012-01-23 17:28:51 -08002006 AppWidgetHostView hostView = info.boundWidget;
2007 int appWidgetId;
2008 if (hostView != null) {
2009 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002010 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002011 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002012 // In this case, we either need to start an activity to get permission to bind
2013 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002014 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002015 Bundle options = info.bindOptions;
2016
2017 boolean success = false;
2018 if (options != null) {
2019 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2020 info.componentName, options);
2021 } else {
2022 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2023 info.componentName);
2024 }
2025 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002026 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002027 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002028 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002029 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2030 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2031 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002032 // TODO: we need to make sure that this accounts for the options bundle.
2033 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002034 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2035 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002036 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002037 }
2038
Joe Onoratodeb98af2010-02-19 14:59:39 -08002039 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002040 // Handle case where user selected "Applications"
2041 String applicationName = getResources().getString(R.string.group_applications);
2042 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002043
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002044 if (applicationName != null && applicationName.equals(shortcutName)) {
2045 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2046 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002047
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002048 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2049 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002050 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002051 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002052 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002053 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 }
2056
Winson Chung24ab2f12010-09-16 14:10:47 -07002057 void processWallpaper(Intent intent) {
2058 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2059 }
2060
Adam Cohendcd297f2013-06-18 13:13:40 -07002061 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002062 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002063 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 folderInfo.title = getText(R.string.folder_name);
2065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002067 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002068 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002069 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070
2071 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002072 FolderIcon newFolder =
2073 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002074 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002075 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002076 // Force measure the new folder icon
2077 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2078 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002079 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 }
Romain Guycbb89e42009-06-08 15:52:54 -07002081
Joe Onorato9c1289c2009-08-17 11:03:03 -04002082 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002083 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002084 }
2085
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002086 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002088 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002089 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 }
2091
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002092 protected ComponentName getWallpaperPickerComponent() {
2093 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2094 }
2095
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002096 /**
2097 * Registers various content observers. The current implementation registers
2098 * only a favorites observer to keep track of the favorites applications.
2099 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002100 private void registerContentObservers() {
2101 ContentResolver resolver = getContentResolver();
2102 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2103 true, mWidgetObserver);
2104 }
2105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 @Override
2107 public boolean dispatchKeyEvent(KeyEvent event) {
2108 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2109 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002111 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002112 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002113 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002114 dumpState();
2115 return true;
2116 }
2117 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002118 }
2119 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2120 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002121 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 return true;
2123 }
2124 }
2125
2126 return super.dispatchKeyEvent(event);
2127 }
2128
Joe Onorato88ec0992009-11-19 13:16:06 -08002129 @Override
2130 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002131 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002132 if (mAppsCustomizeContent.getContentType() ==
2133 AppsCustomizePagedView.ContentType.Applications) {
2134 showWorkspace(true);
2135 } else {
2136 showOverviewMode(true);
2137 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002138 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002139 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002140 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002141 Folder openFolder = mWorkspace.getOpenFolder();
2142 if (openFolder.isEditingName()) {
2143 openFolder.dismissEditingName();
2144 } else {
2145 closeFolder();
2146 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002147 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002148 mWorkspace.exitWidgetResizeMode();
2149
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002150 // Back button is a no-op here, but give at least some feedback for the button press
2151 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002152 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002153 }
2154
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002156 * Re-listen when widgets are reset.
2157 */
2158 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002159 if (mAppWidgetHost != null) {
2160 mAppWidgetHost.startListening();
2161 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002162 }
2163
2164 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 * Launches the intent referred by the clicked shortcut.
2166 *
2167 * @param v The view representing the clicked shortcut.
2168 */
2169 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002170 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2171 // view has detached (it's possible for this to happen if the view is removed mid touch).
2172 if (v.getWindowToken() == null) {
2173 return;
2174 }
2175
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002176 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002177 return;
2178 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002179
Adam Cohen1697b792013-09-17 19:08:21 -07002180 if (v instanceof Workspace) {
2181 if (mWorkspace.isInOverviewMode()) {
2182 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002183 }
2184 return;
2185 }
2186
2187 if (v instanceof CellLayout) {
2188 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002189 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002190 }
2191 }
2192
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002194 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002196 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2197 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002198
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002199 // Check for special shortcuts
2200 if (intent.getComponent() != null) {
2201 final String shortcutClass = intent.getComponent().getClassName();
2202
2203 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002204 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002205 return;
2206 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2207 MemoryDumpActivity.startDump(this);
2208 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002209 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2210 toggleShowWeightWatcher();
2211 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002212 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002213 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002214
2215 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002216 int[] pos = new int[2];
2217 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002218 intent.setSourceBounds(new Rect(pos[0], pos[1],
2219 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002220
2221 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002222
Daniel Sandlerff02d492013-08-05 02:12:05 -04002223 mStats.recordLaunch(intent, shortcut);
2224
Michael Jurkaddd62e92011-02-16 17:49:14 -08002225 if (success && v instanceof BubbleTextView) {
2226 mWaitingForResume = (BubbleTextView) v;
2227 mWaitingForResume.setStayPressed(true);
2228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002230 if (v instanceof FolderIcon) {
2231 FolderIcon fi = (FolderIcon) v;
2232 handleFolderClick(fi);
2233 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002234 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002235 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002236 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002237 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002238 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 }
2241 }
2242
Michael Jurka0e260592010-06-30 17:07:39 -07002243 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002244 return false;
2245 }
2246
Michael Jurkaaf442092010-06-10 17:01:57 -07002247 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002248 * Event handler for the search button
2249 *
2250 * @param v The view that was clicked.
2251 */
2252 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002253 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2254
Amith Yamasania135ba82011-08-09 17:42:01 -07002255 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002256 }
2257
2258 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002259 * Event handler for the voice button
2260 *
2261 * @param v The view that was clicked.
2262 */
2263 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002264 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002265
Bjorn Bringertc459e522013-06-07 19:36:01 +01002266 startVoice();
2267 }
2268
2269 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002270 try {
2271 final SearchManager searchManager =
2272 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2273 ComponentName activityName = searchManager.getGlobalSearchActivity();
2274 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002275 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002276 if (activityName != null) {
2277 intent.setPackage(activityName.getPackageName());
2278 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002279 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002280 } catch (ActivityNotFoundException e) {
2281 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002282 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002283 startActivitySafely(null, intent, "onClickVoiceButton");
2284 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002285 }
2286
2287 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002288 * Event handler for the "grid" button that appears on the home screen, which
2289 * enters all apps mode.
2290 *
2291 * @param v The view that was clicked.
2292 */
2293 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002294 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002295 }
2296
2297 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002298 // Provide the same haptic feedback that the system offers for virtual keys.
2299 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002300 }
2301
Adam Cohen61f560d2013-09-30 15:58:20 -07002302 public void performHapticFeedbackOnTouchDown(View v) {
2303 // Provide the same haptic feedback that the system offers for virtual keys.
2304 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2305 }
2306
2307 public View.OnTouchListener getHapticFeedbackTouchListener() {
2308 if (mHapticFeedbackTouchListener == null) {
2309 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2310 @Override
2311 public boolean onTouch(View v, MotionEvent event) {
2312 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2313 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2314 }
2315 return false;
2316 }
2317 };
2318 }
2319 return mHapticFeedbackTouchListener;
2320 }
2321
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002322 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002323 if (!DISABLE_MARKET_BUTTON) {
2324 if (mAppMarketIntent != null) {
2325 startActivitySafely(v, mAppMarketIntent, "app market");
2326 } else {
2327 Log.e(TAG, "Invalid app market intent.");
2328 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002329 }
2330 }
2331
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002332 /**
2333 * Called when the user stops interacting with the launcher.
2334 * This implies that the user is now on the homescreen and is not doing housekeeping.
2335 */
2336 protected void onInteractionEnd() {}
2337
2338 /**
2339 * Called when the user starts interacting with the launcher.
2340 * The possible interactions are:
2341 * - open all apps
2342 * - reorder an app shortcut, or a widget
2343 * - open the overview mode.
2344 * This is a good time to stop doing things that only make sense
2345 * when the user is on the homescreen and not doing housekeeping.
2346 */
2347 protected void onInteractionBegin() {}
2348
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002349 void startApplicationDetailsActivity(ComponentName componentName) {
2350 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002351 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2352 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002353 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002354 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002355 }
2356
Michael Jurka1e2f4652013-07-08 18:03:46 -07002357 // returns true if the activity was started
2358 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002359 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002360 // System applications cannot be installed. For now, show a toast explaining that.
2361 // We may give them the option of disabling apps this way.
2362 int messageId = R.string.uninstall_system_app_text;
2363 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002364 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002365 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002366 String packageName = componentName.getPackageName();
2367 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002368 Intent intent = new Intent(
2369 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002370 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2371 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002372 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002373 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002374 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002375 }
2376
Michael Jurka86a720e2012-05-09 11:23:23 -07002377 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002381 // Only launch using the new animation if the shortcut has not opted out (this is a
2382 // private contract between launcher and may be ignored in the future).
2383 boolean useLaunchAnimation = (v != null) &&
2384 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2385 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002386 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2387 v.getMeasuredWidth(), v.getMeasuredHeight());
2388
2389 startActivity(intent, opts.toBundle());
2390 } else {
2391 startActivity(intent);
2392 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002393 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 } catch (SecurityException e) {
2395 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002396 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002398 "or use the exported attribute for this activity. "
2399 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002401 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002403
Michael Jurka86a720e2012-05-09 11:23:23 -07002404 boolean startActivitySafely(View v, Intent intent, Object tag) {
2405 boolean success = false;
2406 try {
2407 success = startActivity(v, intent, tag);
2408 } catch (ActivityNotFoundException e) {
2409 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2410 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2411 }
2412 return success;
2413 }
2414
Adam Cohena9cf38f2011-05-02 15:36:58 -07002415 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002416 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002417 Folder openFolder = mWorkspace.getFolderForTag(info);
2418
2419 // If the folder info reports that the associated folder is open, then verify that
2420 // it is actually opened. There have been a few instances where this gets out of sync.
2421 if (info.opened && openFolder == null) {
2422 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002423 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002424 info.opened = false;
2425 }
2426
Adam Cohenfb91f302012-06-11 15:45:18 -07002427 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 // Close any open folder
2429 closeFolder();
2430 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002431 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 } else {
2433 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 int folderScreen;
2435 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002436 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 // .. and close it
2438 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002439 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 // Close any folder open on the current screen
2441 closeFolder();
2442 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002443 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 }
2445 }
2446 }
2447 }
2448
Adam Cohen268c4752012-06-06 17:47:33 -07002449 /**
2450 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2451 * in the DragLayer in the exact absolute location of the original FolderIcon.
2452 */
2453 private void copyFolderIconToImage(FolderIcon fi) {
2454 final int width = fi.getMeasuredWidth();
2455 final int height = fi.getMeasuredHeight();
2456
2457 // Lazy load ImageView, Bitmap and Canvas
2458 if (mFolderIconImageView == null) {
2459 mFolderIconImageView = new ImageView(this);
2460 }
2461 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2462 mFolderIconBitmap.getHeight() != height) {
2463 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2464 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2465 }
2466
2467 DragLayer.LayoutParams lp;
2468 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2469 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2470 } else {
2471 lp = new DragLayer.LayoutParams(width, height);
2472 }
2473
Adam Cohen307fe232012-08-16 17:55:58 -07002474 // The layout from which the folder is being opened may be scaled, adjust the starting
2475 // view size by this scale factor.
2476 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002477 lp.customPosition = true;
2478 lp.x = mRectForFolderAnimation.left;
2479 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002480 lp.width = (int) (scale * width);
2481 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002482
2483 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2484 fi.draw(mFolderIconCanvas);
2485 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002486 if (fi.getFolder() != null) {
2487 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2488 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002489 }
Adam Cohen268c4752012-06-06 17:47:33 -07002490 // Just in case this image view is still in the drag layer from a previous animation,
2491 // we remove it and re-add it.
2492 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2493 mDragLayer.removeView(mFolderIconImageView);
2494 }
2495 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002496 if (fi.getFolder() != null) {
2497 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002498 }
Adam Cohen268c4752012-06-06 17:47:33 -07002499 }
2500
Adam Cohen2801caf2011-05-13 20:57:39 -07002501 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002502 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002503 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2504 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2505 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2506
Adam Cohenc51934b2011-07-26 21:07:43 -07002507 FolderInfo info = (FolderInfo) fi.getTag();
2508 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2509 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002510 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2511 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002512 }
2513
Adam Cohen268c4752012-06-06 17:47:33 -07002514 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2515 copyFolderIconToImage(fi);
2516 fi.setVisibility(View.INVISIBLE);
2517
Michael Jurka2ecf9952012-06-18 12:52:28 -07002518 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002519 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002520 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2521 oa.start();
2522 }
2523
Adam Cohen268c4752012-06-06 17:47:33 -07002524 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002525 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002526 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2527 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2528 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2529
Adam Cohen268c4752012-06-06 17:47:33 -07002530 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002531
Adam Cohen268c4752012-06-06 17:47:33 -07002532 // We remove and re-draw the FolderIcon in-case it has changed
2533 mDragLayer.removeView(mFolderIconImageView);
2534 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002535 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002536 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002537 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002538 oa.addListener(new AnimatorListenerAdapter() {
2539 @Override
2540 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002541 if (cl != null) {
2542 cl.clearFolderLeaveBehind();
2543 // Remove the ImageView copy of the FolderIcon and make the original visible.
2544 mDragLayer.removeView(mFolderIconImageView);
2545 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002546 }
2547 }
2548 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002549 oa.start();
2550 }
2551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002553 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 * is animated relative to the specified View. If the View is null, no animation
2555 * is played.
2556 *
2557 * @param folderInfo The FolderInfo describing the folder to open.
2558 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002559 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002560 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002561 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002562
Adam Cohena9cf38f2011-05-02 15:36:58 -07002563 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564
Adam Cohen4554ee12011-08-03 16:13:21 -07002565 // Just verify that the folder hasn't already been added to the DragLayer.
2566 // There was a one-off crash where the folder had a parent already.
2567 if (folder.getParent() == null) {
2568 mDragLayer.addView(folder);
2569 mDragController.addDropTarget((DropTarget) folder);
2570 } else {
2571 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2572 folder.getParent() + ").");
2573 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002574 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002575 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002576
2577 // Notify the accessibility manager that this folder "window" has appeared and occluded
2578 // the workspace items
2579 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2580 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002581 }
2582
2583 public void closeFolder() {
2584 Folder folder = mWorkspace.getOpenFolder();
2585 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002586 if (folder.isEditingName()) {
2587 folder.dismissEditingName();
2588 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002589 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002590
2591 // Dismiss the folder cling
2592 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002593 }
2594 }
2595
2596 void closeFolder(Folder folder) {
2597 folder.getInfo().opened = false;
2598
2599 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2600 if (parent != null) {
2601 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2602 shrinkAndFadeInFolderIcon(fi);
2603 }
2604 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002605
2606 // Notify the accessibility manager that this folder "window" has disappeard and no
2607 // longer occludeds the workspace items
2608 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002609 }
2610
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002611 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002612 if (!isDraggingEnabled()) return false;
2613 if (isWorkspaceLocked()) return false;
2614 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002615
Adam Cohen1697b792013-09-17 19:08:21 -07002616 if (v instanceof Workspace) {
2617 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002618 if (mWorkspace.enterOverviewMode()) {
2619 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2620 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2621 return true;
2622 } else {
2623 return false;
2624 }
Adam Cohen1697b792013-09-17 19:08:21 -07002625 }
Adam Cohen1697b792013-09-17 19:08:21 -07002626 }
2627
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002628 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002629 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002630 }
2631
Michael Jurka0280c3b2010-09-17 15:00:07 -07002632 resetAddInfo();
2633 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002635 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002636 return true;
2637 }
2638
Winson Chung3d503fb2011-07-13 17:25:49 -07002639 // The hotseat touch handling does not go through Workspace, and we always allow long press
2640 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002641 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002642 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2643 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002644 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002645 // User long pressed on empty space
2646 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2647 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2648 // Disabling reordering until we sort out some issues.
2649 if (mWorkspace.isInOverviewMode()) {
2650 mWorkspace.startReordering(v);
2651 } else {
2652 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002653 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002654 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002655 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002657 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 }
2659 }
2660 }
2661 return true;
2662 }
2663
Winson Chung3d503fb2011-07-13 17:25:49 -07002664 boolean isHotseatLayout(View layout) {
2665 return mHotseat != null && layout != null &&
2666 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2667 }
2668 Hotseat getHotseat() {
2669 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002670 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002671 View getOverviewPanel() {
2672 return mOverviewPanel;
2673 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002674 SearchDropTargetBar getSearchBar() {
2675 return mSearchDropTargetBar;
2676 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002677
Winson Chung3d503fb2011-07-13 17:25:49 -07002678 /**
2679 * Returns the CellLayout of the specified container at the specified screen.
2680 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002681 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002682 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2683 if (mHotseat != null) {
2684 return mHotseat.getLayout();
2685 } else {
2686 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002687 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002688 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002689 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002690 }
2691 }
2692
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002693 Workspace getWorkspace() {
2694 return mWorkspace;
2695 }
2696
Daniel Sandler843e8602010-06-07 14:59:01 -04002697 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002698 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002699 }
2700
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002701 /**
2702 * Helper method for the cameraZoomIn/cameraZoomOut animations
2703 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002704 * @param scaleFactor The scale factor used for the zoom
2705 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002706 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002707 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002708 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002709 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002710
Craig Mautner360310b2012-10-26 15:13:08 -07002711 private void setWorkspaceBackground(boolean workspace) {
2712 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002713 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002714 }
2715
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002716 void updateWallpaperVisibility(boolean visible) {
2717 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2718 int curflags = getWindow().getAttributes().flags
2719 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2720 if (wpflags != curflags) {
2721 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2722 }
Craig Mautner360310b2012-10-26 15:13:08 -07002723 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002724 }
2725
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002726 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2727 if (v instanceof LauncherTransitionable) {
2728 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2729 }
2730 }
2731
Michael Jurkabed61d22012-02-14 22:51:29 -08002732 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2733 if (v instanceof LauncherTransitionable) {
2734 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2735 }
Winson Chung70442722012-02-10 15:43:22 -08002736
2737 // Update the workspace transition step as well
2738 dispatchOnLauncherTransitionStep(v, 0f);
2739 }
2740
2741 private void dispatchOnLauncherTransitionStep(View v, float t) {
2742 if (v instanceof LauncherTransitionable) {
2743 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2744 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002745 }
2746
2747 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2748 if (v instanceof LauncherTransitionable) {
2749 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2750 }
Winson Chung70442722012-02-10 15:43:22 -08002751
2752 // Update the workspace transition step as well
2753 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002754 }
2755
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002756 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002757 * Things to test when changing the following seven functions.
2758 * - Home from workspace
2759 * - from center screen
2760 * - from other screens
2761 * - Home from all apps
2762 * - from center screen
2763 * - from other screens
2764 * - Back from all apps
2765 * - from center screen
2766 * - from other screens
2767 * - Launch app from workspace and quit
2768 * - with back
2769 * - with home
2770 * - Launch app from all apps and quit
2771 * - with back
2772 * - with home
2773 * - Go to a screen that's not the default, then all
2774 * apps, and launch and app, and go back
2775 * - with back
2776 * -with home
2777 * - On workspace, long press power and go back
2778 * - with back
2779 * - with home
2780 * - On all apps, long press power and go back
2781 * - with back
2782 * - with home
2783 * - On workspace, power off
2784 * - On all apps, power off
2785 * - Launch an app and turn off the screen while in that app
2786 * - Go back with home key
2787 * - Go back with back key TODO: make this not go to workspace
2788 * - From all apps
2789 * - From workspace
2790 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2791 * - From all apps
2792 * - From the center workspace
2793 * - From another workspace
2794 */
2795
2796 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002797 * Zoom the camera out from the workspace to reveal 'toView'.
2798 * Assumes that the view to show is anchored at either the very top or very bottom
2799 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002800 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002801 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002802 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2803 showAppsCustomizeHelper(animated, springLoaded, contentType);
2804 }
2805 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2806 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002807 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002808 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002809 mStateAnimation.cancel();
2810 mStateAnimation = null;
2811 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002812 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002813
Winson Chungf0ea4d32011-06-06 14:27:16 -07002814 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2815 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2816 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002817 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002818 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002819 final int startDelay =
2820 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002821
Michael Jurkab3e22d92011-10-31 15:58:33 -07002822 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002823
Michael Jurkad74c9842011-07-10 12:44:21 -07002824 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002825 Animator workspaceAnim =
2826 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002827 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2828 // Set the content type for the all apps space
2829 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2830 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002831
2832 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002833 toView.setScaleX(scale);
2834 toView.setScaleY(scale);
2835 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2836 scaleAnim.
2837 scaleX(1f).scaleY(1f).
2838 setDuration(duration).
2839 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002840
Winson Chungf0ea4d32011-06-06 14:27:16 -07002841 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002842 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002843 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002844 .ofFloat(toView, "alpha", 0f, 1f)
2845 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002846 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002847 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2848 @Override
2849 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002850 if (animation == null) {
2851 throw new RuntimeException("animation is null");
2852 }
Winson Chung70442722012-02-10 15:43:22 -08002853 float t = (Float) animation.getAnimatedValue();
2854 dispatchOnLauncherTransitionStep(fromView, t);
2855 dispatchOnLauncherTransitionStep(toView, t);
2856 }
2857 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002858
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002859 // toView should appear right at the end of the workspace shrink
2860 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002861 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002862 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002863 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002864
2865 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002866 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002867 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002868 // Prepare the position
2869 toView.setTranslationX(0.0f);
2870 toView.setTranslationY(0.0f);
2871 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002872 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002873 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002874 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002875 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002876 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2877 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002878
Winson Chungc7d2b602012-05-16 17:02:20 -07002879 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002880 if (mSearchDropTargetBar != null) {
2881 mSearchDropTargetBar.hideSearchBar(false);
2882 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002883 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002884 });
2885
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002886 if (workspaceAnim != null) {
2887 mStateAnimation.play(workspaceAnim);
2888 }
Michael Jurka1899a362011-11-03 13:50:45 -07002889
2890 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002891
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002892 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2893 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002894
2895 // If any of the objects being animated haven't been measured/laid out
2896 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002897 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002898 (mWorkspace.getMeasuredWidth() == 0) ||
2899 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002900 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002901 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002902
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002903 final AnimatorSet stateAnimation = mStateAnimation;
2904 final Runnable startAnimRunnable = new Runnable() {
2905 public void run() {
2906 // Check that mStateAnimation hasn't changed while
2907 // we waited for a layout/draw pass
2908 if (mStateAnimation != stateAnimation)
2909 return;
2910 setPivotsForZoom(toView, scale);
2911 dispatchOnLauncherTransitionStart(fromView, animated, false);
2912 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002913 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002914 }
2915 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002916 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002917 final ViewTreeObserver observer = toView.getViewTreeObserver();
2918 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2919 public void onGlobalLayout() {
2920 startAnimRunnable.run();
2921 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2922 }
2923 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002924 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002925 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002926 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002927 } else {
2928 toView.setTranslationX(0.0f);
2929 toView.setTranslationY(0.0f);
2930 toView.setScaleX(1.0f);
2931 toView.setScaleY(1.0f);
2932 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002933 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002934
Daniel Sandlere4f98912013-06-25 15:13:26 -04002935 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002936 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002937 if (mSearchDropTargetBar != null) {
2938 mSearchDropTargetBar.hideSearchBar(false);
2939 }
Michael Jurkaabded662011-03-04 12:06:57 -08002940 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002941 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002942 dispatchOnLauncherTransitionStart(fromView, animated, false);
2943 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002944 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002945 dispatchOnLauncherTransitionStart(toView, animated, false);
2946 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002947 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002948 }
2949
2950 /**
2951 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002952 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002953 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002954 */
Adam Cohened307df2013-10-02 09:37:31 -07002955 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08002956 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002957
Michael Jurkab3e22d92011-10-31 15:58:33 -07002958 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002959 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002960 mStateAnimation.cancel();
2961 mStateAnimation = null;
2962 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002963 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002964
Winson Chungf0ea4d32011-06-06 14:27:16 -07002965 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002966 final int fadeOutDuration =
2967 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002968 final float scaleFactor = (float)
2969 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2970 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002971 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002972 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07002973 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002974 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2975 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002976 toState, animated, stagger, -1);
2977 } else if (toState == Workspace.State.SPRING_LOADED ||
2978 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002979 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002980 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002981 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002982
Michael Jurkab3e22d92011-10-31 15:58:33 -07002983 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07002984 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002985 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002986 final LauncherViewPropertyAnimator scaleAnim =
2987 new LauncherViewPropertyAnimator(fromView);
2988 scaleAnim.
2989 scaleX(scaleFactor).scaleY(scaleFactor).
2990 setDuration(duration).
2991 setInterpolator(new Workspace.ZoomInInterpolator());
2992
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002993 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002994 .ofFloat(fromView, "alpha", 1f, 0f)
2995 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002996 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002997 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2998 @Override
2999 public void onAnimationUpdate(ValueAnimator animation) {
3000 float t = 1f - (Float) animation.getAnimatedValue();
3001 dispatchOnLauncherTransitionStep(fromView, t);
3002 dispatchOnLauncherTransitionStep(toView, t);
3003 }
3004 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003005
Michael Jurka2ecf9952012-06-18 12:52:28 -07003006 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003007
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003008 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3009 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003010 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003011
3012 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003013 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003014 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003015 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003016 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3017 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003018 if (onCompleteRunnable != null) {
3019 onCompleteRunnable.run();
3020 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003021 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003022 }
3023 });
3024
Winson Chungf0ea4d32011-06-06 14:27:16 -07003025 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003026 if (workspaceAnim != null) {
3027 mStateAnimation.play(workspaceAnim);
3028 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003029 dispatchOnLauncherTransitionStart(fromView, animated, true);
3030 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003031 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003032 } else {
3033 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003034 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003035 dispatchOnLauncherTransitionStart(fromView, animated, true);
3036 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003037 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003038 dispatchOnLauncherTransitionStart(toView, animated, true);
3039 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003040 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003041 }
3042
Michael Jurkae326f182011-11-21 14:05:46 -08003043 @Override
3044 public void onTrimMemory(int level) {
3045 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003046 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003047 mAppsCustomizeTabHost.onTrimMemory();
3048 }
3049 }
3050
Adam Cohened307df2013-10-02 09:37:31 -07003051 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003052 showWorkspace(animated, null);
3053 }
3054
Adam Cohened307df2013-10-02 09:37:31 -07003055 protected void showWorkspace() {
3056 showWorkspace(true);
3057 }
3058
Adam Cohened66b2b2012-01-23 17:28:51 -08003059 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003060 if (mWorkspace.isInOverviewMode()) {
3061 mWorkspace.exitOverviewMode(animated);
3062 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003063 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003064 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003065 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003066 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003067
Winson Chungc7d2b602012-05-16 17:02:20 -07003068 // Show the search bar (only animate if we were showing the drop target bar in spring
3069 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003070 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003071 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003072 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003073
Michael Jurkab3e22d92011-10-31 15:58:33 -07003074 // Set focus to the AppsCustomize button
3075 if (mAllAppsButton != null) {
3076 mAllAppsButton.requestFocus();
3077 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003078 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003079
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003080 // Change the state *after* we've called all the transition code
3081 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003082
Winson Chung5fb63472011-02-02 17:03:37 -08003083 // Resume the auto-advance of widgets
3084 mUserPresent = true;
3085 updateRunning();
3086
alanv1d4fde62012-10-17 13:15:47 -07003087 // Send an accessibility event to announce the context change
3088 getWindow().getDecorView()
3089 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003090
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003091 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003092 }
3093
Adam Cohened307df2013-10-02 09:37:31 -07003094 void showOverviewMode(boolean animated) {
3095 mWorkspace.setVisibility(View.VISIBLE);
3096 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3097 mState = State.WORKSPACE;
3098 onWorkspaceShown(animated);
3099 }
3100
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003101 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003102 }
3103
Winson Chung82963d52013-10-09 11:20:57 -07003104 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3105 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003106 if (mState != State.WORKSPACE) return;
3107
Winson Chung82963d52013-10-09 11:20:57 -07003108 if (resetPageToZero) {
3109 mAppsCustomizeTabHost.reset();
3110 }
Winson Chungc58497e2013-09-03 17:48:37 -07003111 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003112 mAppsCustomizeTabHost.requestFocus();
3113
Michael Jurkab3e22d92011-10-31 15:58:33 -07003114 // Change the state *after* we've called all the transition code
3115 mState = State.APPS_CUSTOMIZE;
3116
3117 // Pause the auto-advance of widgets until we are out of AllApps
3118 mUserPresent = false;
3119 updateRunning();
3120 closeFolder();
3121
3122 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003123 getWindow().getDecorView()
3124 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003125 }
3126
Adam Cohen7777d962011-08-18 18:58:38 -07003127 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003128 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003129 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003130 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003131 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003132 }
Adam Cohen7777d962011-08-18 18:58:38 -07003133
Adam Cohenad4e15c2013-10-17 16:21:35 -07003134 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003135 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003136 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3137
Winson Chunge7a03942011-08-05 15:05:12 -07003138 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003139 @Override
3140 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003141 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003142 // Before we show workspace, hide all apps again because
3143 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3144 // clean up our state transition functions
3145 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003146 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003147 } else {
3148 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003149 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003150 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003151 }, delay);
3152
Winson Chung557d6ed2011-07-08 15:34:52 -07003153 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003154
Michael Jurkad3ef3062010-11-23 16:23:58 -08003155 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003156 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003157 final boolean animated = true;
3158 final boolean springLoaded = true;
3159 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003160 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003161 }
3162 // Otherwise, we are not in spring loaded mode, so don't do anything.
3163 }
3164
Michael Jurkab3e22d92011-10-31 15:58:33 -07003165 void lockAllApps() {
3166 // TODO
3167 }
3168
3169 void unlockAllApps() {
3170 // TODO
3171 }
3172
Winson Chungf0ea4d32011-06-06 14:27:16 -07003173 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003174 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003175 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003176 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003177 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003178 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003179 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003180 int duration = 0;
3181 if (mSearchDropTargetBar != null) {
3182 duration = mSearchDropTargetBar.getTransitionInDuration();
3183 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003184 mHotseat.animate().alpha(1f).setDuration(duration);
3185 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003186 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003187 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003188 }
3189 }
3190 }
3191
3192 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003193 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003194 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003195 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003196 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003197 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003198 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003199 int duration = 0;
3200 if (mSearchDropTargetBar != null) {
3201 duration = mSearchDropTargetBar.getTransitionOutDuration();
3202 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003203 mHotseat.animate().alpha(0f).setDuration(duration);
3204 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003205 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003206 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003207 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003208 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003209 }
3210
Patrick Dubroy5f445422011-02-18 14:35:21 -08003211 /**
3212 * Add an item from all apps or customize onto the given workspace screen.
3213 * If layout is null, add to the current screen.
3214 */
3215 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003216 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003217 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003218 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003219 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003220
Winson Chungdff8ebb2011-09-08 17:25:31 -07003221 /** Maps the current orientation to an index for referencing orientation correct global icons */
3222 private int getCurrentOrientationIndexForGlobalIcons() {
3223 // default - 0, landscape - 1
3224 switch (getResources().getConfiguration().orientation) {
3225 case Configuration.ORIENTATION_LANDSCAPE:
3226 return 1;
3227 default:
3228 return 0;
3229 }
3230 }
3231
Michael Jurkaae65ee32012-04-30 11:45:54 -07003232 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003233 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003234 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003235 // Look for the toolbar icon specified in the activity meta-data
3236 Bundle metaData = packageManager.getActivityInfo(
3237 activityName, PackageManager.GET_META_DATA).metaData;
3238 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003239 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003240 if (iconResId != 0) {
3241 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003242 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003243 }
3244 }
3245 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003246 // This can happen if the activity defines an invalid drawable
3247 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3248 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003249 } catch (Resources.NotFoundException nfe) {
3250 // This can happen if the activity defines an invalid drawable
3251 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3252 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003253 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003254 return null;
3255 }
3256
3257 // if successful in getting icon, return it; otherwise, set button to use default drawable
3258 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003259 int buttonId, ComponentName activityName, int fallbackDrawableId,
3260 String toolbarResourceName) {
3261 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003262 Resources r = getResources();
3263 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3264 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003265
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003266 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003267 // If we were unable to find the icon via the meta-data, use a generic one
3268 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003269 toolbarIcon = r.getDrawable(fallbackDrawableId);
3270 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003271 if (button != null) {
3272 button.setCompoundDrawables(toolbarIcon, null, null, null);
3273 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003274 return null;
3275 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003276 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003277 if (button != null) {
3278 button.setCompoundDrawables(toolbarIcon, null, null, null);
3279 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003280 return toolbarIcon.getConstantState();
3281 }
3282 }
3283
3284 // if successful in getting icon, return it; otherwise, set button to use default drawable
3285 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003286 int buttonId, ComponentName activityName, int fallbackDrawableId,
3287 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003288 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003289 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003290
Michael Jurka19e0fc52011-07-22 18:00:21 -07003291 if (button != null) {
3292 // If we were unable to find the icon via the meta-data, use a
3293 // generic one
3294 if (toolbarIcon == null) {
3295 button.setImageResource(fallbackDrawableId);
3296 } else {
3297 button.setImageDrawable(toolbarIcon);
3298 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003299 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003300
3301 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3302
Michael Jurka0423dcf2010-10-05 14:56:18 -07003303 }
3304
Winson Chung1b884092012-06-08 17:13:02 -07003305 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003306 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003307 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003308 }
3309
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003310 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003311 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003312 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003313 }
3314
Winson Chungbb185bd2011-11-21 12:31:42 -08003315 private void invalidatePressedFocusedStates(View container, View button) {
3316 if (container instanceof HolographicLinearLayout) {
3317 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3318 layout.invalidatePressedFocusedStates();
3319 } else if (button instanceof HolographicImageView) {
3320 HolographicImageView view = (HolographicImageView) button;
3321 view.invalidatePressedFocusedStates();
3322 }
3323 }
3324
Cristina Stancu476493b2013-08-07 17:20:14 +01003325 public View getQsbBar() {
3326 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003327 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3328 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003329 }
3330 return mQsbBar;
3331 }
3332
3333 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003334 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003335 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003336 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003337 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003338
Winson Chung1cad91e2011-05-25 17:41:01 -07003339 final SearchManager searchManager =
3340 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3341 ComponentName activityName = searchManager.getGlobalSearchActivity();
3342 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003343 int coi = getCurrentOrientationIndexForGlobalIcons();
3344 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003345 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3346 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3347 if (sGlobalSearchIcon[coi] == null) {
3348 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3349 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3350 TOOLBAR_ICON_METADATA_NAME);
3351 }
3352
Winson Chungc51db6a2011-10-05 11:44:49 -07003353 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3354 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003355 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003356 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003357 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003358 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003359 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3360 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003361 if (searchButton != null) searchButton.setVisibility(View.GONE);
3362 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003363 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003364 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003365 }
3366 }
3367
Cristina Stancu476493b2013-08-07 17:20:14 +01003368 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003369 final View searchButtonContainer = findViewById(R.id.search_button_container);
3370 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003371 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003372 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003373 }
3374
Cristina Stancu476493b2013-08-07 17:20:14 +01003375 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003376 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003377 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003378
Winson Chungc51db6a2011-10-05 11:44:49 -07003379 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003380 final SearchManager searchManager =
3381 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3382 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3383
3384 ComponentName activityName = null;
3385 if (globalSearchActivity != null) {
3386 // Check if the global search activity handles voice search
3387 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3388 intent.setPackage(globalSearchActivity.getPackageName());
3389 activityName = intent.resolveActivity(getPackageManager());
3390 }
3391
3392 if (activityName == null) {
3393 // Fallback: check if an activity other than the global search activity
3394 // resolves this
3395 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3396 activityName = intent.resolveActivity(getPackageManager());
3397 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003398 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003399 int coi = getCurrentOrientationIndexForGlobalIcons();
3400 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003401 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3402 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3403 if (sVoiceSearchIcon[coi] == null) {
3404 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3405 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3406 TOOLBAR_ICON_METADATA_NAME);
3407 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003408 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003409 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003410 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003411 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003412 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003413 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003414 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003415 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003416 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003417 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003418 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003419 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003420
Cristina Stancu476493b2013-08-07 17:20:14 +01003421 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003422 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3423 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003424 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003425 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003426 }
3427
Winson Chung5841efa2013-09-30 18:06:44 -07003428 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003429 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3430 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003431 boolean visible = !forceDisableVoiceButtonProxy &&
3432 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003433 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003434 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003435 }
3436 }
Winson Chung5841efa2013-09-30 18:06:44 -07003437
3438 /**
3439 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3440 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3441 */
3442 public void disableVoiceButtonProxy(boolean disabled) {
3443 updateVoiceButtonProxyVisible(disabled);
3444 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003445 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003446 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003447 */
3448 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003449 if (!DISABLE_MARKET_BUTTON) {
3450 final View marketButton = findViewById(R.id.market_button);
3451 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3452 // Find the app market activity by resolving an intent.
3453 // (If multiple app markets are installed, it will return the ResolverActivity.)
3454 ComponentName activityName = intent.resolveActivity(getPackageManager());
3455 if (activityName != null) {
3456 int coi = getCurrentOrientationIndexForGlobalIcons();
3457 mAppMarketIntent = intent;
3458 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3459 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3460 TOOLBAR_ICON_METADATA_NAME);
3461 marketButton.setVisibility(View.VISIBLE);
3462 } else {
3463 // We should hide and disable the view so that we don't try and restore the visibility
3464 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3465 marketButton.setVisibility(View.GONE);
3466 marketButton.setEnabled(false);
3467 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003468 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003469 }
3470
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003471 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003472 if (!DISABLE_MARKET_BUTTON) {
3473 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3474 Resources r = getResources();
3475 Drawable marketIconDrawable = d.newDrawable(r);
3476 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3477 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3478 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003479
Winson Chungd64a6662013-09-30 11:06:59 -07003480 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3481 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003482 }
3483
Michael Jurkad7c28052012-04-27 15:43:36 -07003484 @Override
3485 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003486 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003487 final List<CharSequence> text = event.getText();
3488 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003489 // Populate event with a fake title based on the current state.
3490 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003491 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003492 } else {
3493 text.add(getString(R.string.all_apps_home_button_label));
3494 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003495 return result;
3496 }
3497
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003498 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003499 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003500 */
3501 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3502 @Override
3503 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003504 closeSystemDialogs();
3505 }
3506 }
3507
3508 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003509 * Receives notifications whenever the appwidgets are reset.
3510 */
3511 private class AppWidgetResetObserver extends ContentObserver {
3512 public AppWidgetResetObserver() {
3513 super(new Handler());
3514 }
3515
3516 @Override
3517 public void onChange(boolean selfChange) {
3518 onAppWidgetReset();
3519 }
3520 }
3521
3522 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003523 * If the activity is currently paused, signal that we need to run the passed Runnable
3524 * in onResume.
3525 *
3526 * This needs to be called from incoming places where resources might have been loaded
3527 * while we are paused. That is becaues the Configuration might be wrong
3528 * when we're not running, and if it comes back to what it was when we
3529 * were paused, we are not restarted.
3530 *
3531 * Implementation of the method from LauncherModel.Callbacks.
3532 *
3533 * @return true if we are currently paused. The caller might be able to
3534 * skip some work in that case since we will come back again.
3535 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003536 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003537 if (mPaused) {
3538 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003539 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003540 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003541 }
3542 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003543 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003544 return true;
3545 } else {
3546 return false;
3547 }
3548 }
3549
Michael Jurkac402cd92013-05-20 15:49:32 +02003550 private boolean waitUntilResume(Runnable run) {
3551 return waitUntilResume(run, false);
3552 }
3553
Michael Jurka1e2f4652013-07-08 18:03:46 -07003554 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003555 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003556 }
3557
Michael Jurka7607c2f2013-04-03 14:33:19 -07003558 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003559 * If the activity is currently paused, signal that we need to re-run the loader
3560 * in onResume.
3561 *
3562 * This needs to be called from incoming places where resources might have been loaded
3563 * while we are paused. That is becaues the Configuration might be wrong
3564 * when we're not running, and if it comes back to what it was when we
3565 * were paused, we are not restarted.
3566 *
3567 * Implementation of the method from LauncherModel.Callbacks.
3568 *
3569 * @return true if we are currently paused. The caller might be able to
3570 * skip some work in that case since we will come back again.
3571 */
3572 public boolean setLoadOnResume() {
3573 if (mPaused) {
3574 Log.i(TAG, "setLoadOnResume");
3575 mOnResumeNeedsLoad = true;
3576 return true;
3577 } else {
3578 return false;
3579 }
3580 }
3581
3582 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003584 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003585 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003586 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003587 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003588 } else {
3589 return SCREEN_COUNT / 2;
3590 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003592
Joe Onorato9c1289c2009-08-17 11:03:03 -04003593 /**
3594 * Refreshes the shortcuts shown on the workspace.
3595 *
3596 * Implementation of the method from LauncherModel.Callbacks.
3597 */
3598 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003599 // If we're starting binding all over again, clear any bind calls we'd postponed in
3600 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3601 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003602 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003603
Winson Chungd64d1762013-08-20 14:37:16 -07003604 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003605 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003606 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003607
Michael Jurka05bf6442011-11-30 20:28:53 -08003608 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003609 if (mHotseat != null) {
3610 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003611 }
3612 }
3613
Adam Cohendcd297f2013-06-18 13:13:40 -07003614 @Override
3615 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003616 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003617
Adam Cohen5084cba2013-09-03 12:01:16 -07003618 // If there are no screens, we need to have an empty screen
3619 if (orderedScreenIds.size() == 0) {
3620 mWorkspace.addExtraEmptyScreen();
3621 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003622
3623 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003624 // setCurrentPage() so ensure that all pages are added before calling this).
3625 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003626 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3627 mWorkspace.createCustomContentPage();
3628 }
Winson Chung64359a52013-07-08 17:17:08 -07003629 }
3630
3631 @Override
3632 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003633 int count = orderedScreenIds.size();
3634 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003635 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003636 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003637 }
3638
Adam Cohen39a06042013-07-19 14:30:12 -07003639 private boolean shouldShowWeightWatcher() {
3640 String spKey = LauncherAppState.getSharedPreferencesKey();
3641 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003642 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003643
3644 return show;
3645 }
3646
3647 private void toggleShowWeightWatcher() {
3648 String spKey = LauncherAppState.getSharedPreferencesKey();
3649 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3650 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3651
3652 show = !show;
3653
3654 SharedPreferences.Editor editor = sp.edit();
3655 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3656 editor.commit();
3657
3658 if (mWeightWatcher != null) {
3659 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3660 }
3661 }
3662
Winson Chungd64d1762013-08-20 14:37:16 -07003663 public void bindAppsAdded(final ArrayList<Long> newScreens,
3664 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003665 final ArrayList<ItemInfo> addAnimated,
3666 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003667 Runnable r = new Runnable() {
3668 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003669 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003670 }
3671 };
3672 if (waitUntilResume(r)) {
3673 return;
3674 }
3675
Winson Chungd64d1762013-08-20 14:37:16 -07003676 // Add the new screens
3677 bindAddScreens(newScreens);
3678
3679 // We add the items without animation on non-visible pages, and with
3680 // animations on the new page (which we will try and snap to).
3681 if (!addNotAnimated.isEmpty()) {
3682 bindItems(addNotAnimated, 0,
3683 addNotAnimated.size(), false);
3684 }
3685 if (!addAnimated.isEmpty()) {
3686 bindItems(addAnimated, 0,
3687 addAnimated.size(), true);
3688 }
Winson Chungc58497e2013-09-03 17:48:37 -07003689
Winson Chung87412982013-10-03 18:34:14 -07003690 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003691 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003692
Winson Chungc58497e2013-09-03 17:48:37 -07003693 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3694 addedApps != null && mAppsCustomizeContent != null) {
3695 mAppsCustomizeContent.addApps(addedApps);
3696 }
Winson Chungd64d1762013-08-20 14:37:16 -07003697 }
3698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003699 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 * Bind the items start-end from the list.
3701 *
3702 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003703 */
Winson Chung64359a52013-07-08 17:17:08 -07003704 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3705 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003706 Runnable r = new Runnable() {
3707 public void run() {
3708 bindItems(shortcuts, start, end, forceAnimateIcons);
3709 }
3710 };
3711 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003712 return;
3713 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003714
Winson Chungf0c6ae02012-03-21 16:10:31 -07003715 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003716 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3717 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003718 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003719 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003720 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003721 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003722 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003723
3724 // Short circuit if we are loading dock items for a configuration which has no dock
3725 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3726 mHotseat == null) {
3727 continue;
3728 }
3729
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 switch (item.itemType) {
3731 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3732 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003733 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003734 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003735
Winson Chung64359a52013-07-08 17:17:08 -07003736 /*
3737 * TODO: FIX collision case
3738 */
Winson Chungce376632013-07-11 16:12:41 -07003739 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3740 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3741 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3742 throw new RuntimeException("OCCUPIED");
3743 }
Winson Chung64359a52013-07-08 17:17:08 -07003744 }
3745
Adam Cohendcd297f2013-06-18 13:13:40 -07003746 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3747 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003748 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003749 // Animate all the applications up now
3750 shortcut.setAlpha(0f);
3751 shortcut.setScaleX(0f);
3752 shortcut.setScaleY(0f);
3753 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003754 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003755 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003756 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003757 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003758 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003759 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003760 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003761 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3762 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003763 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003764 default:
3765 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003767 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003768
Winson Chung997a9232013-07-24 15:33:46 -07003769 if (animateIcons) {
3770 // Animate to the correct page
3771 if (newShortcutsScreenId > -1) {
3772 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003773 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003774 final Runnable startBounceAnimRunnable = new Runnable() {
3775 public void run() {
3776 anim.playTogether(bounceAnims);
3777 anim.start();
3778 }
3779 };
Winson Chung997a9232013-07-24 15:33:46 -07003780 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003781 // We post the animation slightly delayed to prevent slowdowns
3782 // when we are loading right after we return to launcher.
3783 mWorkspace.postDelayed(new Runnable() {
3784 public void run() {
3785 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003786 mWorkspace.postDelayed(startBounceAnimRunnable,
3787 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003788 }
3789 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003790 } else {
3791 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003792 }
3793 }
Winson Chung64359a52013-07-08 17:17:08 -07003794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003796 }
3797
Joe Onorato9c1289c2009-08-17 11:03:03 -04003798 /**
3799 * Implementation of the method from LauncherModel.Callbacks.
3800 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003801 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003802 Runnable r = new Runnable() {
3803 public void run() {
3804 bindFolders(folders);
3805 }
3806 };
3807 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003808 return;
3809 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003810 sFolders.clear();
3811 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003812 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813
3814 /**
3815 * Add the views for a widget to the workspace.
3816 *
3817 * Implementation of the method from LauncherModel.Callbacks.
3818 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003819 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003820 Runnable r = new Runnable() {
3821 public void run() {
3822 bindAppWidget(item);
3823 }
3824 };
3825 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003826 return;
3827 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003828
Daniel Sandler843e8602010-06-07 14:59:01 -04003829 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3830 if (DEBUG_WIDGETS) {
3831 Log.d(TAG, "bindAppWidget: " + item);
3832 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003833 final Workspace workspace = mWorkspace;
3834
3835 final int appWidgetId = item.appWidgetId;
3836 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003837 if (DEBUG_WIDGETS) {
3838 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3839 }
3840
Joe Onorato9c1289c2009-08-17 11:03:03 -04003841 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3842
Joe Onorato9c1289c2009-08-17 11:03:03 -04003843 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003844 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003845
Adam Cohendcd297f2013-06-18 13:13:40 -07003846 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003847 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003848 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3849
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850 workspace.requestLayout();
3851
Daniel Sandler843e8602010-06-07 14:59:01 -04003852 if (DEBUG_WIDGETS) {
3853 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3854 + (SystemClock.uptimeMillis()-start) + "ms");
3855 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003856 }
3857
Adam Cohen1462de32012-07-24 22:34:36 -07003858 public void onPageBoundSynchronously(int page) {
3859 mSynchronouslyBoundPages.add(page);
3860 }
3861
Joe Onorato9c1289c2009-08-17 11:03:03 -04003862 /**
3863 * Callback saying that there aren't any more items to bind.
3864 *
3865 * Implementation of the method from LauncherModel.Callbacks.
3866 */
Adam Cohene25af792013-06-06 23:08:25 -07003867 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003868 Runnable r = new Runnable() {
3869 public void run() {
3870 finishBindingItems(upgradePath);
3871 }
3872 };
3873 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003874 return;
3875 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003876 if (mSavedState != null) {
3877 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003878 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003880 mSavedState = null;
3881 }
3882
Adam Cohen1462de32012-07-24 22:34:36 -07003883 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003885 // If we received the result of any pending adds while the loader was running (e.g. the
3886 // widget configuration forced an orientation change), process them now.
3887 for (int i = 0; i < sPendingAddList.size(); i++) {
3888 completeAdd(sPendingAddList.get(i));
3889 }
3890 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003891
Winson Chungc51db6a2011-10-05 11:44:49 -07003892 // Update the market app icon as necessary (the other icons will be managed in response to
3893 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003894 if (!DISABLE_MARKET_BUTTON) {
3895 updateAppMarketIcon();
3896 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003897
Winson Chungf0c6ae02012-03-21 16:10:31 -07003898 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003899 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003900 mWorkspace.getUniqueComponents(true, null);
3901 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003902 }
Adam Cohen99894d92013-06-14 11:22:59 -07003903
Adam Cohen66a01fd2013-06-11 12:48:00 -07003904 mWorkspace.post(new Runnable() {
3905 @Override
3906 public void run() {
3907 onFinishBindingItems();
3908 }
3909 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003910 }
3911
Winson Chunga0b7e862013-09-05 16:03:15 -07003912 public boolean isAllAppsButtonRank(int rank) {
3913 if (mHotseat != null) {
3914 return mHotseat.isAllAppsButtonRank(rank);
3915 }
3916 return false;
3917 }
3918
Winson Chunga2413752012-04-03 14:22:34 -07003919 private boolean canRunNewAppsAnimation() {
3920 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3921 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3922 }
3923
Winson Chungc9168342013-06-26 14:54:55 -07003924 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3925 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3926 PropertyValuesHolder.ofFloat("alpha", 1f),
3927 PropertyValuesHolder.ofFloat("scaleX", 1f),
3928 PropertyValuesHolder.ofFloat("scaleY", 1f));
3929 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3930 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3931 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3932 return bounceAnim;
3933 }
3934
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003935 @Override
3936 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003937 boolean searchVisible = updateGlobalSearchIcon();
3938 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003939 if (mSearchDropTargetBar != null) {
3940 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3941 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003942 }
3943
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003944 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945 * Add the icons for all apps.
3946 *
3947 * Implementation of the method from LauncherModel.Callbacks.
3948 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003949 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003950 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3951 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3952 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3953 getHotseat().addAllAppsFolder(mIconCache, apps,
3954 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3955 }
3956 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003957 }
Winson Chungc58497e2013-09-03 17:48:37 -07003958 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07003959 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07003960 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07003961 mAppsCustomizeContent.onPackagesUpdated(
3962 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07003963 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 }
3966
3967 /**
3968 * A package was updated.
3969 *
3970 * Implementation of the method from LauncherModel.Callbacks.
3971 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003972 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003973 Runnable r = new Runnable() {
3974 public void run() {
3975 bindAppsUpdated(apps);
3976 }
3977 };
3978 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003979 return;
3980 }
3981
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003982 if (mWorkspace != null) {
3983 mWorkspace.updateShortcuts(apps);
3984 }
Winson Chungc58497e2013-09-03 17:48:37 -07003985
3986 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3987 mAppsCustomizeContent != null) {
3988 mAppsCustomizeContent.updateApps(apps);
3989 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003990 }
3991
3992 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003993 * A package was uninstalled. We take both the super set of packageNames
3994 * in addition to specific applications to remove, the reason being that
3995 * this can be called when a package is updated as well. In that scenario,
3996 * we only remove specific components from the workspace, where as
3997 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 *
3999 * Implementation of the method from LauncherModel.Callbacks.
4000 */
Winson Chung83892cc2013-05-01 16:53:33 -07004001 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004002 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004003 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004004 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004005 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004006 }
Winson Chungd64d1762013-08-20 14:37:16 -07004007 };
4008 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004009 return;
4010 }
4011
Winson Chungdf95eb12013-10-16 14:57:07 -07004012 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004013 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004014 }
4015 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004016 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004017 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004018
Winson Chunga1820962011-10-03 16:31:06 -07004019 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004020 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004021
Winson Chungdf95eb12013-10-16 14:57:07 -07004022 // Update AllApps
Winson Chungc58497e2013-09-03 17:48:37 -07004023 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4024 mAppsCustomizeContent != null) {
4025 mAppsCustomizeContent.removeApps(appInfos);
4026 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004027 }
Joe Onoratobe386092009-11-17 17:32:16 -08004028
4029 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004030 * A number of packages were updated.
4031 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004032 private ArrayList<Object> mWidgetsAndShortcuts;
4033 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004034 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004035 bindPackagesUpdated(mWidgetsAndShortcuts);
4036 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004037 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004038 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004039 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4040 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4041 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004042 return;
4043 }
4044
Winson Chung64359a52013-07-08 17:17:08 -07004045 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004046 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4047 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004048 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004049 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004050 }
4051
Winson Chung400438b2011-01-16 17:53:48 -08004052 private int mapConfigurationOriActivityInfoOri(int configOri) {
4053 final Display d = getWindowManager().getDefaultDisplay();
4054 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4055 switch (d.getRotation()) {
4056 case Surface.ROTATION_0:
4057 case Surface.ROTATION_180:
4058 // We are currently in the same basic orientation as the natural orientation
4059 naturalOri = configOri;
4060 break;
4061 case Surface.ROTATION_90:
4062 case Surface.ROTATION_270:
4063 // We are currently in the other basic orientation to the natural orientation
4064 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4065 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4066 break;
4067 }
4068
4069 int[] oriMap = {
4070 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4071 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4072 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4073 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4074 };
4075 // Since the map starts at portrait, we need to offset if this device's natural orientation
4076 // is landscape.
4077 int indexOffset = 0;
4078 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4079 indexOffset = 1;
4080 }
4081 return oriMap[(d.getRotation() + indexOffset) % 4];
4082 }
Adam Cohen446e9402011-09-15 18:21:21 -07004083
Winson Chung4b919f82012-05-01 10:44:08 -07004084 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004085 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004086 getResources().getBoolean(R.bool.allow_rotation);
4087 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004088 }
Winson Chung4b919f82012-05-01 10:44:08 -07004089 public void lockScreenOrientation() {
4090 if (isRotationEnabled()) {
4091 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4092 .getConfiguration().orientation));
4093 }
4094 }
4095 public void unlockScreenOrientation(boolean immediate) {
4096 if (isRotationEnabled()) {
4097 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004098 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004099 } else {
4100 mHandler.postDelayed(new Runnable() {
4101 public void run() {
4102 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4103 }
4104 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004105 }
Winson Chung4b919f82012-05-01 10:44:08 -07004106 }
Winson Chung400438b2011-01-16 17:53:48 -08004107 }
4108
Winson Chung82f55532011-08-09 14:14:23 -07004109 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004110 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004111 if (DISABLE_CLINGS) {
4112 return false;
4113 }
4114
Winson Chung3a6e7f32013-10-09 15:50:52 -07004115 // For now, limit only to phones
4116 LauncherAppState app = LauncherAppState.getInstance();
4117 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4118 if (grid.isTablet()) {
4119 return false;
4120 }
4121
Brett Chabot2a9e2282011-08-23 15:03:13 -07004122 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004123 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004124
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004125 // Disable clings for accessibility when explore by touch is enabled
4126 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4127 ACCESSIBILITY_SERVICE);
4128 if (a11yManager.isTouchExplorationEnabled()) {
4129 return false;
4130 }
4131
Michael Jurkae233a8b2013-03-19 13:49:20 +01004132 // Restricted secondary users (child mode) will potentially have very few apps
4133 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004134// boolean supportsLimitedUsers =
4135// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4136// Account[] accounts = AccountManager.get(this).getAccounts();
4137// if (supportsLimitedUsers && accounts.length == 0) {
4138// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4139// Bundle restrictions = um.getUserRestrictions();
4140// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4141// return false;
4142// }
4143// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004144 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004145 }
Michael Jurka22143132012-10-04 17:42:38 +02004146
Winson Chungfa545132013-09-26 17:45:32 -07004147 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004148 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004149 Cling cling = (Cling) findViewById(clingId);
4150 View scrim = null;
4151 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004152 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004153 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004154 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004155 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004156 cling.show(animate, SHOW_CLING_DURATION);
4157
4158 if (dimNavBarVisibilty) {
4159 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4160 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004161 }
4162 }
4163 return cling;
4164 }
Michael Jurka22143132012-10-04 17:42:38 +02004165
Winson Chungaf40f202013-09-18 18:26:31 -07004166 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4167 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004168 // To catch cases where siblings of top-level views are made invisible, just check whether
4169 // the cling is directly set to GONE before dismissing it.
4170 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004171 final Runnable cleanUpClingCb = new Runnable() {
4172 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004173 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004174 // We should update the shared preferences on a background thread
4175 new Thread("dismissClingThread") {
4176 public void run() {
4177 SharedPreferences.Editor editor = mSharedPrefs.edit();
4178 editor.putBoolean(flag, true);
4179 editor.commit();
4180 }
4181 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004182 if (postAnimationCb != null) {
4183 postAnimationCb.run();
4184 }
4185 }
4186 };
4187 if (duration <= 0) {
4188 cleanUpClingCb.run();
4189 } else {
4190 cling.hide(duration, cleanUpClingCb);
4191 }
Michael Jurka22143132012-10-04 17:42:38 +02004192 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004193
4194 if (restoreNavBarVisibilty) {
4195 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4196 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4197 }
Winson Chung82f55532011-08-09 14:14:23 -07004198 }
4199 }
Michael Jurka22143132012-10-04 17:42:38 +02004200
Winson Chung9d9d74f2011-09-19 11:49:12 -07004201 private void removeCling(int id) {
4202 final View cling = findViewById(id);
4203 if (cling != null) {
4204 final ViewGroup parent = (ViewGroup) cling.getParent();
4205 parent.post(new Runnable() {
4206 @Override
4207 public void run() {
4208 parent.removeView(cling);
4209 }
4210 });
Michael Jurka22143132012-10-04 17:42:38 +02004211 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004212 }
4213 }
Michael Jurka974c3862012-05-22 22:00:31 -07004214
4215 private boolean skipCustomClingIfNoAccounts() {
4216 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4217 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4218 if (customCling) {
4219 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004220 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004221 Account[] accounts = am.getAccountsByType("com.google");
4222 return accounts.length == 0;
4223 }
4224 return false;
4225 }
4226
Winson Chung82e5c982013-10-09 12:04:50 -07004227 public void updateCustomContentHintVisibility() {
4228 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4229 String ccHintStr = getFirstRunCustomContentHint();
4230
4231 if (mWorkspace.hasCustomContent()) {
4232 // Show the custom content hint if ccHintStr is not empty
4233 if (cling != null) {
4234 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4235 }
4236 } else {
4237 // Hide the custom content hint
4238 if (cling != null) {
4239 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4240 }
4241 }
4242 }
4243
4244 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4245 boolean animate) {
4246 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4247 if (ccHint != null) {
4248 if (visible && !ccHintStr.isEmpty()) {
4249 ccHint.setText(ccHintStr);
4250 ccHint.setVisibility(View.VISIBLE);
4251 if (animate) {
4252 ccHint.setAlpha(0f);
4253 ccHint.animate().alpha(1f)
4254 .setDuration(SHOW_CLING_DURATION)
4255 .start();
4256 } else {
4257 ccHint.setAlpha(1f);
4258 }
4259 } else {
4260 if (animate) {
4261 ccHint.animate().alpha(0f)
4262 .setDuration(SHOW_CLING_DURATION)
4263 .setListener(new AnimatorListenerAdapter() {
4264 @Override
4265 public void onAnimationEnd(Animator animation) {
4266 ccHint.setVisibility(View.GONE);
4267 }
4268 })
4269 .start();
4270 } else {
4271 ccHint.setAlpha(0f);
4272 ccHint.setVisibility(View.GONE);
4273 }
4274 }
4275 }
4276 }
4277
Winson Chungaf40f202013-09-18 18:26:31 -07004278 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004279 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004280 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004281 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004282 // If we're not using the default workspace layout, replace workspace cling
4283 // with a custom workspace cling (usually specified in an overlay)
4284 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004285 if (!DISABLE_CUSTOM_CLINGS) {
4286 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4287 getResources().getBoolean(R.bool.config_useCustomClings)) {
4288 // Use a custom cling
4289 View cling = findViewById(R.id.workspace_cling);
4290 ViewGroup clingParent = (ViewGroup) cling.getParent();
4291 int clingIndex = clingParent.indexOfChild(cling);
4292 clingParent.removeViewAt(clingIndex);
4293 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4294 clingParent.addView(customCling, clingIndex);
4295 customCling.setId(R.id.workspace_cling);
4296 }
4297 }
4298 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4299 if (cling != null) {
4300 String sbHintStr = getFirstRunClingSearchBarHint();
4301 String ccHintStr = getFirstRunCustomContentHint();
4302 if (!sbHintStr.isEmpty()) {
4303 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4304 sbHint.setText(sbHintStr);
4305 sbHint.setVisibility(View.VISIBLE);
4306 }
Winson Chung82e5c982013-10-09 12:04:50 -07004307 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004308 }
Winson Chungfa545132013-09-26 17:45:32 -07004309 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004310 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004311 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004312 }
4313 }
Winson Chungaf40f202013-09-18 18:26:31 -07004314
Winson Chunge6eabff2013-09-24 11:01:23 -07004315 protected String getFirstRunClingSearchBarHint() {
4316 return "";
4317 }
4318 protected String getFirstRunCustomContentHint() {
4319 return "";
4320 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004321 protected int getFirstRunFocusedHotseatAppDrawableId() {
4322 return -1;
4323 }
4324 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4325 return null;
4326 }
4327 protected int getFirstRunFocusedHotseatAppRank() {
4328 return -1;
4329 }
4330 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4331 return "";
4332 }
4333 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4334 return "";
4335 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004336
Winson Chungaf40f202013-09-18 18:26:31 -07004337 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004338 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004339 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004340 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004341 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4342
4343 // Set the focused hotseat app if there is one
4344 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4345 getFirstRunFocusedHotseatAppRank(),
4346 getFirstRunFocusedHotseatAppComponentName(),
4347 getFirstRunFocusedHotseatAppBubbleTitle(),
4348 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004349 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004350 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004351 }
4352 }
4353 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004354 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004355 if (isClingsEnabled() &&
4356 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004357 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4358 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004359 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004360 } else {
4361 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004362 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004363 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004364 }
Michael Jurka104c4562013-07-08 18:03:46 -07004365 protected SharedPreferences getSharedPrefs() {
4366 return mSharedPrefs;
4367 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004368 public boolean isFolderClingVisible() {
4369 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004370 if (cling != null) {
4371 return cling.getVisibility() == View.VISIBLE;
4372 }
4373 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004374 }
Winson Chungaf40f202013-09-18 18:26:31 -07004375 public void dismissFirstRunCling(View v) {
4376 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4377 Runnable cb = new Runnable() {
4378 public void run() {
4379 // Show the workspace cling next
4380 showFirstRunWorkspaceCling();
4381 }
4382 };
4383 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4384 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004385
4386 // Fade out the search bar for the workspace cling coming up
4387 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004388 }
Winson Chung82f55532011-08-09 14:14:23 -07004389 public void dismissWorkspaceCling(View v) {
4390 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004391 Runnable cb = null;
4392 if (v == null) {
4393 cb = new Runnable() {
4394 public void run() {
4395 mWorkspace.enterOverviewMode();
4396 }
4397 };
4398 }
4399 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4400 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004401
4402 // Fade in the search bar
4403 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004404 }
4405 public void dismissFolderCling(View v) {
4406 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004407 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4408 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004409 }
4410
Winson Chung80baf5a2010-08-09 16:03:15 -07004411 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004412 * Prints out out state for debugging.
4413 */
4414 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004415 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004416 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004417 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4418 Log.d(TAG, "mRestoring=" + mRestoring);
4419 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4420 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004421 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004422 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004423
Winson Chung785d2eb2011-04-14 16:08:02 -07004424 if (mAppsCustomizeContent != null) {
4425 mAppsCustomizeContent.dumpState();
4426 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004427 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004428 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004429
4430 @Override
4431 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4432 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004433 synchronized (sDumpLogs) {
4434 writer.println(" ");
4435 writer.println("Debug logs: ");
4436 for (int i = 0; i < sDumpLogs.size(); i++) {
4437 writer.println(" " + sDumpLogs.get(i));
4438 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004439 }
4440 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004441
4442 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004443 if (DEBUG_DUMP_LOG) {
4444 synchronized (sDumpLogs) {
4445 Log.d(TAG, "");
4446 Log.d(TAG, "*********************");
4447 Log.d(TAG, "Launcher debug logs: ");
4448 for (int i = 0; i < sDumpLogs.size(); i++) {
4449 Log.d(TAG, " " + sDumpLogs.get(i));
4450 }
4451 Log.d(TAG, "*********************");
4452 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004453 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004454 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004455 }
4456
4457 public static void addDumpLog(String tag, String log, boolean debugLog) {
4458 if (debugLog) {
4459 Log.d(tag, log);
4460 }
Winson Chungede41292013-09-19 16:27:36 -07004461 if (DEBUG_DUMP_LOG) {
4462 sDateStamp.setTime(System.currentTimeMillis());
4463 synchronized (sDumpLogs) {
4464 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004465 }
Winson Chungede41292013-09-19 16:27:36 -07004466 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004467 }
4468
Winson Chungede41292013-09-19 16:27:36 -07004469 public void dumpLogsToLocalData() {
4470 if (DEBUG_DUMP_LOG) {
4471 new Thread("DumpLogsToLocalData") {
4472 @Override
4473 public void run() {
4474 boolean success = false;
4475 sDateStamp.setTime(sRunStart);
4476 String FILENAME = sDateStamp.getMonth() + "-"
4477 + sDateStamp.getDay() + "_"
4478 + sDateStamp.getHours() + "-"
4479 + sDateStamp.getMinutes() + "_"
4480 + sDateStamp.getSeconds() + ".txt";
4481
4482 FileOutputStream fos = null;
4483 File outFile = null;
4484 try {
4485 outFile = new File(getFilesDir(), FILENAME);
4486 outFile.createNewFile();
4487 fos = new FileOutputStream(outFile);
4488 } catch (Exception e) {
4489 e.printStackTrace();
4490 }
4491 if (fos != null) {
4492 PrintWriter writer = new PrintWriter(fos);
4493
4494 writer.println(" ");
4495 writer.println("Debug logs: ");
4496 synchronized (sDumpLogs) {
4497 for (int i = 0; i < sDumpLogs.size(); i++) {
4498 writer.println(" " + sDumpLogs.get(i));
4499 }
4500 }
4501 writer.close();
4502 }
4503 try {
4504 if (fos != null) {
4505 fos.close();
4506 success = true;
4507 }
4508 } catch (IOException e) {
4509 e.printStackTrace();
4510 }
4511 }
4512 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004513 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004515}
Michael Jurka2763be32011-02-24 11:19:57 -08004516
Michael Jurkaabded662011-03-04 12:06:57 -08004517interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004518 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004519 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004520 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004521 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004522 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004523}