blob: aad9b9f35f4b5b5d793840d10940c792bb157eae [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;
80import android.view.MenuItem;
81import android.view.MotionEvent;
82import android.view.Surface;
83import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070084import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080085import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewGroup;
87import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080088import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070091import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070092import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Advanceable;
96import android.widget.FrameLayout;
97import android.widget.ImageView;
98import android.widget.TextView;
99import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700100
Daniel Sandler325dc232013-06-05 22:57:57 -0400101import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800102
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.DataInputStream;
104import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700105import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.IOException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700111import java.io.InputStream;
112import java.io.OutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700116import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700119import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121/**
122 * Default launcher application.
123 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100124public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700125 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700126 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800127 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700128 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Daniel Sandlerf061f822013-06-27 13:59:36 -0400130 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400131 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700132 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400133 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700134
Winson Chung70d72102011-08-12 11:24:35 -0700135 private static final int MENU_GROUP_WALLPAPER = 1;
136 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
137 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700138 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
139 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
141 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700142 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final int REQUEST_PICK_APPLICATION = 6;
144 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700146 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Michael Jurka8b805b12012-04-18 14:23:14 -0700148 private static final int REQUEST_BIND_APPWIDGET = 11;
149
Mathew Inwood876a8462013-06-14 14:12:41 +0100150 /**
151 * IntentStarter uses request codes starting with this. This must be greater than all activity
152 * request codes used internally.
153 */
154 protected static final int REQUEST_LAST = 100;
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
157
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800158 static final int SCREEN_COUNT = 5;
159 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Romain Guy98d01652009-06-30 16:21:04 -0700161 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800162 // To turn on these properties, type
163 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
164 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
165 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Winson Chung2672ff92012-05-04 16:22:30 -0700167 // The Intent extra that defines whether to ignore the launch animation
168 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400169 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: int
172 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700173 // Type: int
174 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
177 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
179 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700182 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: boolean
184 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
185 // Type: long
186 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700187 // Type: int
188 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
189 // Type: int
190 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
191 // Type: parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100194 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700195 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100196 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700197 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100198 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700199
Adam Cohen39a06042013-07-19 14:30:12 -0700200 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700201 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700202
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700204 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700205 private State mState = State.WORKSPACE;
206 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700209 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
210 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700211 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700212 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800215 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
218 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700219 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800221 private final BroadcastReceiver mCloseSystemDialogsReceiver
222 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800223 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 private LayoutInflater mInflater;
226
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800229 private DragLayer mDragLayer;
230 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700231 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700233 private AppWidgetManager mAppWidgetManager;
234 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Michael Jurkac9d95c52011-08-29 14:03:34 -0700236 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700237 private AppWidgetProviderInfo mPendingAddWidgetInfo;
238
Michael Jurka0280c3b2010-09-17 15:00:07 -0700239 private int[] mTmpAddItemCellCoordinates = new int[2];
240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 private FolderInfo mFolderInfo;
242
Winson Chung3d503fb2011-07-13 17:25:49 -0700243 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700244 private View mOverviewPanel;
245
Michael Jurka838a4ca2011-02-07 13:33:06 -0800246 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247
Winson Chungc51db6a2011-10-05 11:44:49 -0700248 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700249 private AppsCustomizeTabHost mAppsCustomizeTabHost;
250 private AppsCustomizePagedView mAppsCustomizeContent;
251 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100252 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700255 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
256 // scroll issues (because the workspace may not have been measured yet) and extra work.
257 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
258 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259
260 private SpannableStringBuilder mDefaultKeySsb = null;
261
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262 private boolean mWorkspaceLoading = true;
263
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800264 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 private boolean mRestoring;
266 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700267 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
Michael Jurka1e2f4652013-07-08 18:03:46 -0700269 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700270 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
271
Winson Chung4a2afa32012-07-19 14:53:05 -0700272 // Keep track of whether the user has left launcher
273 private static boolean sPausedFromUserAction = false;
274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private Bundle mSavedInstanceState;
276
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800278 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800279 private boolean mUserPresent = true;
280 private boolean mVisible = false;
281 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700282 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700284 private static LocaleConfiguration sLocaleConfiguration = null;
285
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700286 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700287
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700288 private Intent mAppMarketIntent = null;
289
Adam Cohended9f8d2010-11-03 13:25:16 -0700290 // Related to the auto-advancing of widgets
291 private final int ADVANCE_MSG = 1;
292 private final int mAdvanceInterval = 20000;
293 private final int mAdvanceStagger = 250;
294 private long mAutoAdvanceSentTime;
295 private long mAutoAdvanceTimeLeft = -1;
296 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
297 new HashMap<View, AppWidgetProviderInfo>();
298
Winson Chung400438b2011-01-16 17:53:48 -0800299 // Determines how long to wait after a rotation before restoring the screen orientation to
300 // match the sensor state.
301 private final int mRestoreScreenOrientationDelay = 500;
302
Michael Jurka4ef207b2010-11-29 17:05:45 -0800303 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700304 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
305 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
306 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800307
Craig Mautner360310b2012-10-26 15:13:08 -0700308 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700309
Adam Cohen1462de32012-07-24 22:34:36 -0700310 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
311
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700312 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700313 static Date sDateStamp = new Date();
314 static DateFormat sDateFormat =
315 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
316 static long sRunStart = System.currentTimeMillis();
317 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Adam Cohene25af792013-06-06 23:08:25 -0700323 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
324
Winson Chungf0c6ae02012-03-21 16:10:31 -0700325 // Holds the page that we need to animate to, and the icon views that we need to animate up
326 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700327 private ImageView mFolderIconImageView;
328 private Bitmap mFolderIconBitmap;
329 private Canvas mFolderIconCanvas;
330 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700331
Michael Jurkaddd62e92011-02-16 17:49:14 -0800332 private BubbleTextView mWaitingForResume;
333
Michael Jurka22143132012-10-04 17:42:38 +0200334 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
335 = new HideFromAccessibilityHelper();
336
Michael Jurkac1f5d262011-09-30 19:32:27 -0700337 private Runnable mBuildLayersRunnable = new Runnable() {
338 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700339 if (mWorkspace != null) {
340 mWorkspace.buildPageHardwareLayers();
341 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700342 }
343 };
344
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800345 private static ArrayList<PendingAddArguments> sPendingAddList
346 = new ArrayList<PendingAddArguments>();
347
Michael Jurka0a457bf2012-11-19 14:05:05 -0800348 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
349
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800350 private static class PendingAddArguments {
351 int requestCode;
352 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700353 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700354 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355 int cellX;
356 int cellY;
357 }
358
Daniel Sandlerff02d492013-08-05 02:12:05 -0400359 private Stats mStats;
360
Michael Jurka0a457bf2012-11-19 14:05:05 -0800361 private static boolean isPropertyEnabled(String propertyName) {
362 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700363 }
364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 @Override
366 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700367 if (DEBUG_STRICT_MODE) {
368 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
369 .detectDiskReads()
370 .detectDiskWrites()
371 .detectNetwork() // or .detectAll() for all detectable problems
372 .penaltyLog()
373 .build());
374 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
375 .detectLeakedSqlLiteObjects()
376 .detectLeakedClosableObjects()
377 .penaltyLog()
378 .penaltyDeath()
379 .build());
380 }
381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400383
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400384 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400385 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700386
387 // Determine the dynamic grid properties
388 Point smallestSize = new Point();
389 Point largestSize = new Point();
390 Point realSize = new Point();
391 Display display = getWindowManager().getDefaultDisplay();
392 display.getCurrentSizeRange(smallestSize, largestSize);
393 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700394 DisplayMetrics dm = new DisplayMetrics();
395 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700396 // Lazy-initialize the dynamic grid
397 DeviceProfile grid = app.initDynamicGrid(this,
398 Math.min(smallestSize.x, smallestSize.y),
399 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700400 realSize.x, realSize.y,
401 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700402
403 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400404 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700405 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800406 mModel = app.setLauncher(this);
407 mIconCache = app.getIconCache();
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);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700414 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
415 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700416
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700417 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
418 // this also ensures that any synchronous binding below doesn't re-trigger another
419 // LauncherModel load.
420 mPaused = false;
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200423 android.os.Debug.startMethodTracing(
424 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 }
426
427 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 setContentView(R.layout.launcher);
429 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100430 grid.layout(this);
Winson Chung7d7541e2011-09-16 20:14:36 -0700431 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800433 registerContentObservers();
434
Joe Onorato7c312c12009-08-13 21:36:53 -0700435 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 mSavedState = savedInstanceState;
438 restoreState(mSavedState);
439
Winson Chunga12a2502010-12-20 14:41:35 -0800440 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700441 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200442 mAppsCustomizeContent.onPackagesUpdated(
443 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700444 }
Winson Chunga12a2502010-12-20 14:41:35 -0800445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 if (PROFILE_STARTUP) {
447 android.os.Debug.stopMethodTracing();
448 }
449
450 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700451 if (sPausedFromUserAction) {
452 // If the user leaves launcher, then we should just load items asynchronously when
453 // they return.
454 mModel.startLoader(true, -1);
455 } else {
456 // We only load the page synchronously if the user rotates (or triggers a
457 // configuration change) while launcher is in the foreground
458 mModel.startLoader(true, mWorkspace.getCurrentPage());
459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
462 // For handling default keys
463 mDefaultKeySsb = new SpannableStringBuilder();
464 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800465
466 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
467 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800468
Adam Cohenf9426d52012-06-04 17:26:21 -0700469 updateGlobalIcons();
470
471 // On large interfaces, we want the screen to auto-rotate based on the current orientation
472 unlockScreenOrientation(true);
473 }
474
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 protected void onUserLeaveHint() {
476 super.onUserLeaveHint();
477 sPausedFromUserAction = true;
478 }
479
Winson Chung98ca0f72013-07-29 12:58:51 -0700480 /** To be overriden by subclasses to hint to Launcher that we have custom content */
481 protected boolean hasCustomContentToLeft() {
482 return false;
483 }
484
Adam Cohenf9426d52012-06-04 17:26:21 -0700485 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700486 boolean searchVisible = false;
487 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800488 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700489 int coi = getCurrentOrientationIndexForGlobalIcons();
490 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
491 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700492 updateAppMarketIcon();
493 searchVisible = updateGlobalSearchIcon();
494 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700495 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700496 if (sGlobalSearchIcon[coi] != null) {
497 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700498 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700499 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700500 if (sVoiceSearchIcon[coi] != null) {
501 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700502 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700503 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700504 if (sAppMarketIcon[coi] != null) {
505 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800506 }
Winson Chungadf0c182012-08-23 14:59:07 -0700507 if (mSearchDropTargetBar != null) {
508 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
509 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 }
Romain Guycbb89e42009-06-08 15:52:54 -0700511
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700513 if (sLocaleConfiguration == null) {
514 new AsyncTask<Void, Void, LocaleConfiguration>() {
515 @Override
516 protected LocaleConfiguration doInBackground(Void... unused) {
517 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
518 readConfiguration(Launcher.this, localeConfiguration);
519 return localeConfiguration;
520 }
521
522 @Override
523 protected void onPostExecute(LocaleConfiguration result) {
524 sLocaleConfiguration = result;
525 checkForLocaleChange(); // recursive, but now with a locale configuration
526 }
527 }.execute();
528 return;
529 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700530
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 final Configuration configuration = getResources().getConfiguration();
532
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700533 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 final String locale = configuration.locale.toString();
535
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700536 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 final int mcc = configuration.mcc;
538
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700539 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 final int mnc = configuration.mnc;
541
Romain Guy84f296c2009-11-04 15:00:44 -0800542 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
Romain Guy84f296c2009-11-04 15:00:44 -0800544 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700545 sLocaleConfiguration.locale = locale;
546 sLocaleConfiguration.mcc = mcc;
547 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700548
Joe Onorato0589f0f2010-02-08 13:44:00 -0800549 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700550
551 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
552 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700553 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700554 public void run() {
555 writeConfiguration(Launcher.this, localeConfiguration);
556 }
557 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700558 }
559 }
560
561 private static class LocaleConfiguration {
562 public String locale;
563 public int mcc = -1;
564 public int mnc = -1;
565 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700566
Romain Guy98d01652009-06-30 16:21:04 -0700567 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
568 DataInputStream in = null;
569 try {
570 in = new DataInputStream(context.openFileInput(PREFERENCES));
571 configuration.locale = in.readUTF();
572 configuration.mcc = in.readInt();
573 configuration.mnc = in.readInt();
574 } catch (FileNotFoundException e) {
575 // Ignore
576 } catch (IOException e) {
577 // Ignore
578 } finally {
579 if (in != null) {
580 try {
581 in.close();
582 } catch (IOException e) {
583 // Ignore
584 }
585 }
586 }
587 }
588
589 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
590 DataOutputStream out = null;
591 try {
592 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
593 out.writeUTF(configuration.locale);
594 out.writeInt(configuration.mcc);
595 out.writeInt(configuration.mnc);
596 out.flush();
597 } catch (FileNotFoundException e) {
598 // Ignore
599 } catch (IOException e) {
600 //noinspection ResultOfMethodCallIgnored
601 context.getFileStreamPath(PREFERENCES).delete();
602 } finally {
603 if (out != null) {
604 try {
605 out.close();
606 } catch (IOException e) {
607 // Ignore
608 }
609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 }
611 }
612
Bjorn Bringertc459e522013-06-07 19:36:01 +0100613 public LayoutInflater getInflater() {
614 return mInflater;
615 }
616
Adam Cohen716b51e2011-06-30 12:09:54 -0700617 public DragLayer getDragLayer() {
618 return mDragLayer;
619 }
620
Winson Chung36a62fe2012-05-06 18:04:42 -0700621 boolean isDraggingEnabled() {
622 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
623 // that is subsequently removed from the workspace in startBinding().
624 return !mModel.isLoadingWorkspace();
625 }
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 static int getScreen() {
628 synchronized (sLock) {
629 return sScreen;
630 }
631 }
632
633 static void setScreen(int screen) {
634 synchronized (sLock) {
635 sScreen = screen;
636 }
637 }
638
Winson Chung557d6ed2011-07-08 15:34:52 -0700639 /**
640 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
641 * a configuration step, this allows the proper animations to run after other transitions.
642 */
643 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700644 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800645 switch (args.requestCode) {
646 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800649 break;
650 case REQUEST_PICK_SHORTCUT:
651 processShortcut(args.intent);
652 break;
653 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700654 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700655 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700656 result = true;
657 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 case REQUEST_CREATE_APPWIDGET:
659 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700660 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700661 result = true;
662 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 }
Michael Jurka27614d22012-04-02 06:40:22 -0700664 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
665 // if you turned the screen off and then back while in All Apps, Launcher would not
666 // return to the workspace. Clearing mAddInfo.container here fixes this issue
667 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700668 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 }
670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700672 protected void onActivityResult(
673 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700674 // Reset the startActivity waiting flag
675 mWaitingForResult = false;
676
Michael Jurka8b805b12012-04-18 14:23:14 -0700677 if (requestCode == REQUEST_BIND_APPWIDGET) {
678 int appWidgetId = data != null ?
679 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
680 if (resultCode == RESULT_CANCELED) {
681 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
682 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700683 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700684 }
685 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200686 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
687 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
688 mWorkspace.exitOverviewMode(false);
689 }
690 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700691 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200692
Winson Chung557d6ed2011-07-08 15:34:52 -0700693 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800694 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
695 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700696
Adam Cohened66b2b2012-01-23 17:28:51 -0800697 // We have special handling for widgets
698 if (isWidgetDrop) {
699 int appWidgetId = data != null ?
700 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700701 if (appWidgetId < 0) {
702 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
703 "widget configuration activity.");
704 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
705 } else {
706 completeTwoStageWidgetDrop(resultCode, appWidgetId);
707 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800708 return;
709 }
710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // The pattern used here is that a user PICKs a specific application,
712 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
715 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700716 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800717 final PendingAddArguments args = new PendingAddArguments();
718 args.requestCode = requestCode;
719 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700720 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700721 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700722 args.cellX = mPendingAddInfo.cellX;
723 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800724 if (isWorkspaceLocked()) {
725 sPendingAddList.add(args);
726 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700727 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700731 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
733 null);
734 }
735
736 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700738 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 Runnable onCompleteRunnable = null;
740 int animationType = 0;
741
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700742 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 if (resultCode == RESULT_OK) {
744 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
745 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 mPendingAddWidgetInfo);
747 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 onCompleteRunnable = new Runnable() {
749 @Override
750 public void run() {
751 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700752 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800753 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
754 null);
755 }
756 };
757 } else if (resultCode == RESULT_CANCELED) {
758 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
759 onCompleteRunnable = new Runnable() {
760 @Override
761 public void run() {
762 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
763 null);
764 }
765 };
766 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700767 if (mDragLayer.getAnimatedView() != null) {
768 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
769 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
770 animationType, boundWidget, true);
771 } else {
772 // The animated view may be null in the case of a rotation during widget configuration
773 onCompleteRunnable.run();
774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 }
776
777 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700778 protected void onStop() {
779 super.onStop();
780 FirstFrameAnimatorHelper.setIsVisible(false);
781 }
782
783 @Override
784 protected void onStart() {
785 super.onStart();
786 FirstFrameAnimatorHelper.setIsVisible(true);
787 }
788
789 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200791 long startTime = 0;
792 if (DEBUG_RESUME_TIME) {
793 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400794 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200795 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700797
Winson Chung4a2afa32012-07-19 14:53:05 -0700798 // Restore the previous launcher state
799 if (mOnResumeState == State.WORKSPACE) {
800 showWorkspace(false);
801 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chungc58497e2013-09-03 17:48:37 -0700802 showAllApps(false, AppsCustomizePagedView.ContentType.Applications);
Winson Chung4a2afa32012-07-19 14:53:05 -0700803 }
804 mOnResumeState = State.NONE;
805
Craig Mautner360310b2012-10-26 15:13:08 -0700806 // Background was set to gradient in onPause(), restore to black if in all apps.
807 setWorkspaceBackground(mState == State.WORKSPACE);
808
Winson Chungde0fb8f2012-05-08 14:37:08 -0700809 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700810 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700811
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800812 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700813 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700814 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700816 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400817 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700818 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700820 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200821 // We might have postponed some bind calls until onResume (see waitUntilResume) --
822 // execute them here
823 long startTimeCallbacks = 0;
824 if (DEBUG_RESUME_TIME) {
825 startTimeCallbacks = System.currentTimeMillis();
826 }
827
828 if (mAppsCustomizeContent != null) {
829 mAppsCustomizeContent.setBulkBind(true);
830 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700831 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
832 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200833 }
834 if (mAppsCustomizeContent != null) {
835 mAppsCustomizeContent.setBulkBind(false);
836 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700837 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200838 if (DEBUG_RESUME_TIME) {
839 Log.d(TAG, "Time spent processing callbacks in onResume: " +
840 (System.currentTimeMillis() - startTimeCallbacks));
841 }
Michael Jurka447bf842013-05-15 14:52:15 +0200842 }
Michael Jurka54554252013-08-01 12:52:23 +0200843 if (mOnResumeCallbacks.size() > 0) {
844 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
845 mOnResumeCallbacks.get(i).run();
846 }
847 mOnResumeCallbacks.clear();
848 }
Winson Chunge4e50662012-01-23 14:45:13 -0800849
850 // Reset the pressed state of icons that were locked in the press state while activities
851 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800852 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800853 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800854 mWaitingForResume.setStayPressed(false);
855 }
Winson Chunge4e50662012-01-23 14:45:13 -0800856 if (mAppsCustomizeContent != null) {
857 // Resets the previous all apps icon press state
858 mAppsCustomizeContent.resetDrawableState();
859 }
Adam Cohen06dff352012-06-01 17:17:08 -0700860 // It is possible that widgets can receive updates while launcher is not in the foreground.
861 // Consequently, the widgets will be inflated in the orientation of the foreground activity
862 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
863 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700864 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700865
866 // Again, as with the above scenario, it's possible that one or more of the global icons
867 // were updated in the wrong orientation.
868 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200869 if (DEBUG_RESUME_TIME) {
870 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
871 }
Adam Cohen4caf2982013-08-20 18:54:31 -0700872
873 // Write all the logs to disk
874 Launcher.addDumpLog(TAG, "10249126 - onResume() - dumping logs to disk", true);
875 dumpLogsToLocalData(false);
Adam Cohen06dff352012-06-01 17:17:08 -0700876 }
877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700879 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700880 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
881 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
882 // when Launcher resumes and we are still in AllApps.
883 updateWallpaperVisibility(true);
884
Winson Chung997a9232013-07-24 15:33:46 -0700885 // Ensure that items added to Launcher are queued until Launcher returns
886 InstallShortcutReceiver.enableInstallQueue();
887
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700888 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700889 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800890 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700891 mDragController.resetLastGestureUpTime();
Adam Cohen4caf2982013-08-20 18:54:31 -0700892
893 // Write all the logs to disk
894 Launcher.addDumpLog(TAG, "10249126 - onPause() - dumping logs to disk", true);
895 dumpLogsToLocalData(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700896 }
Romain Guycbb89e42009-06-08 15:52:54 -0700897
Adam Cohen66a01fd2013-06-11 12:48:00 -0700898 protected void onFinishBindingItems() {
899 }
900
Adam Cohenbffe7452013-07-22 18:21:45 -0700901 QSBScroller mQsbScroller = new QSBScroller() {
902 int scrollY = 0;
903
904 @Override
905 public void setScrollY(int scroll) {
906 scrollY = scroll;
907
908 if (mWorkspace.isOnOrMovingToCustomContent()) {
909 mSearchDropTargetBar.setTranslationY(- scrollY);
910 }
911 }
912 };
913
914 public void resetQSBScroll() {
915 mSearchDropTargetBar.animate().translationY(0).start();
916 }
917
918 public interface CustomContentCallbacks {
919 // Custom content is completely shown
920 public void onShow();
921
922 // Custom content is completely hidden
923 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700924
925 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
926 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700927 }
928
Adam Cohen30bacb22013-08-29 14:25:25 -0700929 protected void startSettings() {
930 }
931
Adam Cohenbffe7452013-07-22 18:21:45 -0700932 public interface QSBScroller {
933 public void setScrollY(int scrollY);
934 }
935
Adam Cohen66a01fd2013-06-11 12:48:00 -0700936 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700937 public QSBScroller addToCustomContentPage(View customContent) {
938 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700939 }
940
Winson Chung98ca0f72013-07-29 12:58:51 -0700941 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700942 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700943 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700944 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700945 }
946
Adam Cohenedb40762013-07-18 16:45:45 -0700947 // The custom content needs to offset its content to account for the QSB
948 public int getTopOffsetForCustomContent() {
949 return mWorkspace.getPaddingTop();
950 }
951
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700952 @Override
953 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 // Flag the loader to stop early before switching
955 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700956 if (mAppsCustomizeContent != null) {
957 mAppsCustomizeContent.surrender();
958 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800959 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700960 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700961
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800962 // We can't hide the IME if it was forced open. So don't bother
963 /*
964 @Override
965 public void onWindowFocusChanged(boolean hasFocus) {
966 super.onWindowFocusChanged(hasFocus);
967
968 if (hasFocus) {
969 final InputMethodManager inputManager = (InputMethodManager)
970 getSystemService(Context.INPUT_METHOD_SERVICE);
971 WindowManager.LayoutParams lp = getWindow().getAttributes();
972 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
973 android.os.Handler()) {
974 protected void onReceiveResult(int resultCode, Bundle resultData) {
975 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
976 }
977 });
978 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
979 }
980 }
981 */
982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 private boolean acceptFilter() {
984 final InputMethodManager inputManager = (InputMethodManager)
985 getSystemService(Context.INPUT_METHOD_SERVICE);
986 return !inputManager.isFullscreenMode();
987 }
988
989 @Override
990 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700991 final int uniChar = event.getUnicodeChar();
992 final boolean handled = super.onKeyDown(keyCode, event);
993 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
994 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
996 keyCode, event);
997 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700998 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700999 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001000 // showSearchDialog()
1001 // If there are multiple keystrokes before the search dialog takes focus,
1002 // onSearchRequested() will be called for every keystroke,
1003 // but it is idempotent, so it's fine.
1004 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
1006 }
1007
Joe Onorato8a9625e2010-01-28 15:55:35 -08001008 // Eat the long press event so the keyboard doesn't come up.
1009 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1010 return true;
1011 }
1012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 return handled;
1014 }
1015
Karl Rosaen138a0412009-04-23 19:00:21 -07001016 private String getTypedText() {
1017 return mDefaultKeySsb.toString();
1018 }
1019
1020 private void clearTypedText() {
1021 mDefaultKeySsb.clear();
1022 mDefaultKeySsb.clearSpans();
1023 Selection.setSelection(mDefaultKeySsb, 0);
1024 }
1025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001027 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1028 * State
1029 */
1030 private static State intToState(int stateOrdinal) {
1031 State state = State.WORKSPACE;
1032 final State[] stateValues = State.values();
1033 for (int i = 0; i < stateValues.length; i++) {
1034 if (stateValues[i].ordinal() == stateOrdinal) {
1035 state = stateValues[i];
1036 break;
1037 }
1038 }
1039 return state;
1040 }
1041
1042 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 * Restores the previous state, if it exists.
1044 *
1045 * @param savedState The previous state.
1046 */
1047 private void restoreState(Bundle savedState) {
1048 if (savedState == null) {
1049 return;
1050 }
1051
Michael Jurka883f55b2010-10-21 15:47:14 -07001052 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001053 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001054 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001055 }
1056
Winson Chungf0c6ae02012-03-21 16:10:31 -07001057 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001059 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 }
1061
Winson Chung3d503fb2011-07-13 17:25:49 -07001062 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001064
Winson Chung3d503fb2011-07-13 17:25:49 -07001065 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1066 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001067 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001068 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1069 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001070 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1071 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1072 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001073 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 mRestoring = true;
1075 }
1076
1077 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1078 if (renameFolder) {
1079 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001080 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 mRestoring = true;
1082 }
Winson Chunga12a2502010-12-20 14:41:35 -08001083
Winson Chung785d2eb2011-04-14 16:08:02 -07001084 // Restore the AppsCustomize tab
1085 if (mAppsCustomizeTabHost != null) {
1086 String curTab = savedState.getString("apps_customize_currentTab");
1087 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001088 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001089 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001090 mAppsCustomizeContent.loadAssociatedPages(
1091 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001092 }
1093
Winson Chung5afbf7b2011-07-25 11:53:08 -07001094 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1095 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
1098
1099 /**
1100 * Finds all the views we need and configure them properly.
1101 */
1102 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001103 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001104
Craig Mautner360310b2012-10-26 15:13:08 -07001105 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001106 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1107 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001108
John Spurlock77e1f472013-09-11 10:09:51 -04001109 mLauncherView.setSystemUiVisibility(
1110 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001111 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112
Winson Chung4c98d922011-05-31 16:50:48 -07001113 // Setup the drag layer
1114 mDragLayer.setup(this, dragController);
1115
Winson Chung3d503fb2011-07-13 17:25:49 -07001116 // Setup the hotseat
1117 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1118 if (mHotseat != null) {
1119 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001120 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001121 }
1122
Adam Cohenf358a4b2013-07-23 16:47:31 -07001123 mOverviewPanel = findViewById(R.id.overview_panel);
1124 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1125 @Override
1126 public void onClick(View arg0) {
Winson Chungc58497e2013-09-03 17:48:37 -07001127 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001128 }
1129 });
1130 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1131 @Override
1132 public void onClick(View arg0) {
1133 startWallpaper();
1134 }
1135 });
Adam Cohen30bacb22013-08-29 14:25:25 -07001136 findViewById(R.id.settings_button).setOnClickListener(new OnClickListener() {
1137 @Override
1138 public void onClick(View arg0) {
1139 startSettings();
1140 }
1141 });
Adam Cohendbdff6b2013-09-18 19:09:15 -07001142 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001143
Winson Chung4c98d922011-05-31 16:50:48 -07001144 // Setup the workspace
1145 mWorkspace.setHapticFeedbackEnabled(false);
1146 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001147 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001148 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001149
Winson Chungf0ea4d32011-06-06 14:27:16 -07001150 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001151 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001152
Winson Chungf0ea4d32011-06-06 14:27:16 -07001153 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001154 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001155 mAppsCustomizeContent = (AppsCustomizePagedView)
1156 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1157 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001158
Winson Chung3d503fb2011-07-13 17:25:49 -07001159 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001160 dragController.setDragScoller(mWorkspace);
1161 dragController.setScrollView(mDragLayer);
1162 dragController.setMoveTarget(mWorkspace);
1163 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001164 if (mSearchDropTargetBar != null) {
1165 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001166 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001167
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001168 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001169 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001170 mWeightWatcher = new WeightWatcher(this);
1171 mWeightWatcher.setAlpha(0.5f);
1172 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001173 new FrameLayout.LayoutParams(
1174 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001175 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001176 Gravity.BOTTOM)
1177 );
Adam Cohen39a06042013-07-19 14:30:12 -07001178
1179 boolean show = shouldShowWeightWatcher();
1180 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
1183
1184 /**
1185 * Creates a view representing a shortcut.
1186 *
1187 * @param info The data structure describing the shortcut.
1188 *
1189 * @return A View inflated from R.layout.application.
1190 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001191 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001193 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 }
1195
1196 /**
1197 * Creates a view representing a shortcut inflated from the specified resource.
1198 *
1199 * @param layoutResId The id of the XML layout used to create the shortcut.
1200 * @param parent The group the shortcut belongs to.
1201 * @param info The data structure describing the shortcut.
1202 *
1203 * @return A View inflated from layoutResId.
1204 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001205 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001206 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1207 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 return favorite;
1210 }
1211
1212 /**
1213 * Add an application shortcut to the workspace.
1214 *
1215 * @param data The intent describing the application.
1216 * @param cellInfo The position on screen where to create the shortcut.
1217 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001218 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001219 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001220 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221
Adam Cohenfbba09b2011-07-18 21:43:05 -07001222 // First we check if we already know the exact location where we want to add this item.
1223 if (cellX >= 0 && cellY >= 0) {
1224 cellXY[0] = cellX;
1225 cellXY[1] = cellY;
1226 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001227 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001228 return;
1229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001231 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001232
Romain Guy73b979d2009-06-09 12:57:21 -07001233 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001234 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001236 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001237 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001238 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001239 } else {
1240 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 }
1242 }
Romain Guycbb89e42009-06-08 15:52:54 -07001243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 /**
1245 * Add a shortcut to the workspace.
1246 *
1247 * @param data The intent describing the shortcut.
1248 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001250 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001251 int cellY) {
1252 int[] cellXY = mTmpAddItemCellCoordinates;
1253 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001254 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001255
Michael Jurkad3ef3062010-11-23 16:23:58 -08001256 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001257
Adam Cohen558baaf2011-08-15 15:22:57 -07001258 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001259 if (info == null) {
1260 return;
1261 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001262 final View view = createShortcut(info);
1263
Adam Cohenfbba09b2011-07-18 21:43:05 -07001264 // First we check if we already know the exact location where we want to add this item.
1265 if (cellX >= 0 && cellY >= 0) {
1266 cellXY[0] = cellX;
1267 cellXY[1] = cellY;
1268 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001269
1270 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001271 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001272 true, null,null)) {
1273 return;
1274 }
1275 DragObject dragObject = new DragObject();
1276 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001277 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1278 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001279 return;
1280 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001281 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001282 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001283 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001284 foundCellSpan = (result != null);
1285 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001286 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001287 }
1288
1289 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001290 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001291 return;
1292 }
1293
Adam Cohendcd297f2013-06-18 13:13:40 -07001294 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295
1296 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001297 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001298 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
1300 }
1301
Adam Cohen2f093b62012-04-30 18:59:53 -07001302 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1303 int minHeight) {
1304 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001305 // We want to account for the extra amount of padding that we are adding to the widget
1306 // to ensure that it gets the full amount of space that it has requested
1307 int requiredWidth = minWidth + padding.left + padding.right;
1308 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001309 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001310 }
1311
Adam Cohen2f093b62012-04-30 18:59:53 -07001312 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1313 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001314 }
1315
Adam Cohen2f093b62012-04-30 18:59:53 -07001316 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1317 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001318 }
1319
Adam Cohen2f093b62012-04-30 18:59:53 -07001320 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1321 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001322 }
1323
Adam Cohen2f093b62012-04-30 18:59:53 -07001324 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1325 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001326 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001327 }
1328
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001330 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001332 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001333 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001335 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001336 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1337 if (appWidgetInfo == null) {
1338 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1339 }
Romain Guycbb89e42009-06-08 15:52:54 -07001340
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001341 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001342 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001343
Adam Cohen2f093b62012-04-30 18:59:53 -07001344 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1345 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001348 // We have saved the position to which the widget was dragged-- this really only matters
1349 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001350 int[] cellXY = mTmpAddItemCellCoordinates;
1351 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001352 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001353 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1355 cellXY[0] = mPendingAddInfo.cellX;
1356 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001357 spanXY[0] = mPendingAddInfo.spanX;
1358 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001359 foundCellSpan = true;
1360 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001361 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001363 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1364 spanXY[1], cellXY, finalSpan);
1365 spanXY[0] = finalSpan[0];
1366 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001367 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001369 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001370 }
1371
Michael Jurka0280c3b2010-09-17 15:00:07 -07001372 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001373 if (appWidgetId != -1) {
1374 // Deleting an app widget ID is a void call but writes to disk before returning
1375 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001376 new Thread("deleteAppWidgetId") {
1377 public void run() {
1378 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1379 }
1380 }.start();
1381 }
Winson Chung93eef082012-03-23 15:59:27 -07001382 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001383 return;
1384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001386 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001387 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1388 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001389 launcherInfo.spanX = spanXY[0];
1390 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001391 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1392 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001395 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396
1397 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001398 if (hostView == null) {
1399 // Perform actual inflation because we're live
1400 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1401 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1402 } else {
1403 // The AppWidgetHostView has already been inflated and instantiated
1404 launcherInfo.hostView = hostView;
1405 }
Romain Guycbb89e42009-06-08 15:52:54 -07001406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001408 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001409 launcherInfo.notifyWidgetSizeChanged(this);
1410
Adam Cohendcd297f2013-06-18 13:13:40 -07001411 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001412 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001413
1414 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001416 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
Romain Guycbb89e42009-06-08 15:52:54 -07001418
Adam Cohended9f8d2010-11-03 13:25:16 -07001419 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1420 @Override
1421 public void onReceive(Context context, Intent intent) {
1422 final String action = intent.getAction();
1423 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1424 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001425 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001426 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001427
Winson Chungc100e8e2011-08-09 16:02:43 -07001428 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001429 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001430 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1431 mAppsCustomizeTabHost.reset();
Adam Cohen040a5d22013-09-16 17:09:33 -07001432 showWorkspaceAndExitOverviewMode(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001433 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001434 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1435 mUserPresent = true;
1436 updateRunning();
1437 }
1438 }
1439 };
1440
1441 @Override
1442 public void onAttachedToWindow() {
1443 super.onAttachedToWindow();
1444
1445 // Listen for broadcasts related to user-presence
1446 final IntentFilter filter = new IntentFilter();
1447 filter.addAction(Intent.ACTION_SCREEN_OFF);
1448 filter.addAction(Intent.ACTION_USER_PRESENT);
1449 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001450 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001451 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001452 mVisible = true;
1453 }
1454
1455 @Override
1456 public void onDetachedFromWindow() {
1457 super.onDetachedFromWindow();
1458 mVisible = false;
1459
Adam Cohend113e0c2010-11-11 10:48:05 -08001460 if (mAttached) {
1461 unregisterReceiver(mReceiver);
1462 mAttached = false;
1463 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001464 updateRunning();
1465 }
1466
1467 public void onWindowVisibilityChanged(int visibility) {
1468 mVisible = visibility == View.VISIBLE;
1469 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001470 // The following code used to be in onResume, but it turns out onResume is called when
1471 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1472 // is a more appropriate event to handle
1473 if (mVisible) {
1474 mAppsCustomizeTabHost.onWindowVisible();
1475 if (!mWorkspaceLoading) {
1476 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001477 // We want to let Launcher draw itself at least once before we force it to build
1478 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001479 // apps is nice and speedy.
1480 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001481 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001482 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001483 if (mStarted) return;
1484 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001485 // We delay the layer building a bit in order to give
1486 // other message processing a time to run. In particular
1487 // this avoids a delay in hiding the IME if it was
1488 // currently shown, because doing that may involve
1489 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001490 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001491 final ViewTreeObserver.OnDrawListener listener = this;
1492 mWorkspace.post(new Runnable() {
1493 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001494 if (mWorkspace != null &&
1495 mWorkspace.getViewTreeObserver() != null) {
1496 mWorkspace.getViewTreeObserver().
1497 removeOnDrawListener(listener);
1498 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001499 }
1500 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001501 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001502 }
1503 });
1504 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001505 // When Launcher comes back to foreground, a different Activity might be responsible for
1506 // the app market intent, so refresh the icon
1507 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001508 clearTypedText();
1509 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001510 }
1511
1512 private void sendAdvanceMessage(long delay) {
1513 mHandler.removeMessages(ADVANCE_MSG);
1514 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1515 mHandler.sendMessageDelayed(msg, delay);
1516 mAutoAdvanceSentTime = System.currentTimeMillis();
1517 }
1518
1519 private void updateRunning() {
1520 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1521 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1522 mAutoAdvanceRunning = autoAdvanceRunning;
1523 if (autoAdvanceRunning) {
1524 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1525 sendAdvanceMessage(delay);
1526 } else {
1527 if (!mWidgetsToAdvance.isEmpty()) {
1528 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1529 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1530 }
1531 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001532 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001533 }
1534 }
1535 }
1536
1537 private final Handler mHandler = new Handler() {
1538 @Override
1539 public void handleMessage(Message msg) {
1540 if (msg.what == ADVANCE_MSG) {
1541 int i = 0;
1542 for (View key: mWidgetsToAdvance.keySet()) {
1543 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1544 final int delay = mAdvanceStagger * i;
1545 if (v instanceof Advanceable) {
1546 postDelayed(new Runnable() {
1547 public void run() {
1548 ((Advanceable) v).advance();
1549 }
1550 }, delay);
1551 }
1552 i++;
1553 }
1554 sendAdvanceMessage(mAdvanceInterval);
1555 }
1556 }
1557 };
1558
1559 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001560 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001561 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1562 if (v instanceof Advanceable) {
1563 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001564 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001565 updateRunning();
1566 }
1567 }
1568
1569 void removeWidgetToAutoAdvance(View hostView) {
1570 if (mWidgetsToAdvance.containsKey(hostView)) {
1571 mWidgetsToAdvance.remove(hostView);
1572 updateRunning();
1573 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001574 }
1575
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001577 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001578 launcherInfo.hostView = null;
1579 }
1580
Winson Chung93eef082012-03-23 15:59:27 -07001581 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1582 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1583 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001584 }
1585
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001586 public LauncherAppWidgetHost getAppWidgetHost() {
1587 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
Romain Guycbb89e42009-06-08 15:52:54 -07001589
Winson Chunga9abd0e2010-10-27 17:18:37 -07001590 public LauncherModel getModel() {
1591 return mModel;
1592 }
1593
Bjorn Bringertc459e522013-06-07 19:36:01 +01001594 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001595 getWindow().closeAllPanels();
1596
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001597 // Whatever we were doing is hereby canceled.
1598 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001599 }
1600
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 @Override
1602 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001603 long startTime = 0;
1604 if (DEBUG_RESUME_TIME) {
1605 startTime = System.currentTimeMillis();
1606 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 super.onNewIntent(intent);
1608
1609 // Close the menu
1610 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001611 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001612 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001613
Jamie Genniscb222e82012-10-23 15:44:26 -07001614 final boolean alreadyOnHome =
1615 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001616 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001617
Jamie Genniscb222e82012-10-23 15:44:26 -07001618 Runnable processIntent = new Runnable() {
1619 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001620 if (mWorkspace == null) {
1621 // Can be cases where mWorkspace is null, this prevents a NPE
1622 return;
1623 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001624 Folder openFolder = mWorkspace.getOpenFolder();
1625 // In all these cases, only animate if we're already on home
1626 mWorkspace.exitWidgetResizeMode();
1627 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1628 openFolder == null) {
1629 mWorkspace.moveToDefaultScreen(true);
1630 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001631
Jamie Genniscb222e82012-10-23 15:44:26 -07001632 closeFolder();
1633 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001634
Jamie Genniscb222e82012-10-23 15:44:26 -07001635 // If we are already on home, then just animate back to the workspace,
1636 // otherwise, just wait until onResume to set the state back to Workspace
1637 if (alreadyOnHome) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001638 showWorkspaceAndExitOverviewMode(true);
Jamie Genniscb222e82012-10-23 15:44:26 -07001639 } else {
1640 mOnResumeState = State.WORKSPACE;
1641 }
1642
1643 final View v = getWindow().peekDecorView();
1644 if (v != null && v.getWindowToken() != null) {
1645 InputMethodManager imm = (InputMethodManager)getSystemService(
1646 INPUT_METHOD_SERVICE);
1647 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1648 }
1649
1650 // Reset AllApps to its initial state
1651 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1652 mAppsCustomizeTabHost.reset();
1653 }
1654 }
1655 };
1656
1657 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1658 // Delay processing of the intent to allow the status bar animation to finish
1659 // first in order to avoid janky animations.
1660 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001661 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001662 // Process the intent immediately.
1663 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001664 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 }
Michael Jurka447bf842013-05-15 14:52:15 +02001667 if (DEBUG_RESUME_TIME) {
1668 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 }
1671
Adam Cohen040a5d22013-09-16 17:09:33 -07001672 protected void showWorkspaceAndExitOverviewMode(boolean animate) {
1673 showWorkspace(animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001674 if (mWorkspace.isInOverviewMode()) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001675 mWorkspace.exitOverviewMode(animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001676 }
1677 }
Adam Cohen040a5d22013-09-16 17:09:33 -07001678 protected void showWorkspaceAndExitOverviewMode() {
1679 showWorkspaceAndExitOverviewMode(true);
1680 }
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001683 public void onRestoreInstanceState(Bundle state) {
1684 super.onRestoreInstanceState(state);
1685 for (int page: mSynchronouslyBoundPages) {
1686 mWorkspace.restoreInstanceStateForChild(page);
1687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 }
1689
1690 @Override
1691 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001692 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001693 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694
Michael Jurka883f55b2010-10-21 15:47:14 -07001695 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001696 // We close any open folder since it will not be re-opened, and we need to make sure
1697 // this state is reflected.
1698 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699
Adam Cohendcd297f2013-06-18 13:13:40 -07001700 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001701 mWaitingForResult) {
1702 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001703 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001704 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1705 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001706 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1707 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1708 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 }
1710
1711 if (mFolderInfo != null && mWaitingForResult) {
1712 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1713 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1714 }
Michael Jurka946ad472010-07-09 18:05:18 -07001715
Winson Chung785d2eb2011-04-14 16:08:02 -07001716 // Save the current AppsCustomize tab
1717 if (mAppsCustomizeTabHost != null) {
1718 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1719 if (currentTabTag != null) {
1720 outState.putString("apps_customize_currentTab", currentTabTag);
1721 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001722 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1723 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 }
1726
1727 @Override
1728 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001730
Winson Chunge7a03942011-08-05 15:05:12 -07001731 // Remove all pending runnables
1732 mHandler.removeMessages(ADVANCE_MSG);
1733 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001734 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001735
Winson Chungcd2b0142011-06-08 16:02:26 -07001736 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001737 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001738 mModel.stopLoader();
1739 app.setLauncher(null);
1740
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001742 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001744 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001746 mAppWidgetHost = null;
1747
1748 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749
1750 TextKeyListener.getInstance().release();
1751
Winson Chung81b52252012-08-27 15:34:29 -07001752 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1753 // to prevent leaking Launcher activities on orientation change.
1754 if (mModel != null) {
1755 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1756 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001757
1758 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001759 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001760
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001761 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001762 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1763 mWorkspace.removeAllViews();
1764 mWorkspace = null;
1765 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001766
Michael Jurka2ecf9952012-06-18 12:52:28 -07001767 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 }
1769
Adam Cohena9cf38f2011-05-02 15:36:58 -07001770 public DragController getDragController() {
1771 return mDragController;
1772 }
1773
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 @Override
1775 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001776 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 super.startActivityForResult(intent, requestCode);
1778 }
1779
Winson Chung70d72102011-08-12 11:24:35 -07001780 /**
1781 * Indicates that we want global search for this activity by setting the globalSearch
1782 * argument for {@link #startSearch} to true.
1783 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001785 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001787
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001788 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001789
Karl Rosaen138a0412009-04-23 19:00:21 -07001790 if (initialQuery == null) {
1791 // Use any text typed in the launcher as the initial query
1792 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 if (appSearchData == null) {
1795 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001796 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 }
Winson Chungadf0c182012-08-23 14:59:07 -07001798 Rect sourceBounds = new Rect();
1799 if (mSearchDropTargetBar != null) {
1800 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1801 }
Romain Guycbb89e42009-06-08 15:52:54 -07001802
Bjorn Bringertc459e522013-06-07 19:36:01 +01001803 startSearch(initialQuery, selectInitialQuery,
1804 appSearchData, sourceBounds);
1805 }
1806
1807 public void startSearch(String initialQuery,
1808 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001809 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001810 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001811 }
1812
1813 /**
1814 * Starts the global search activity. This code is a copied from SearchManager
1815 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001816 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001817 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001818 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001819 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1820 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1821 if (globalSearchActivity == null) {
1822 Log.w(TAG, "No global search activity found.");
1823 return;
1824 }
1825 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1826 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1827 intent.setComponent(globalSearchActivity);
1828 // Make sure that we have a Bundle to put source in
1829 if (appSearchData == null) {
1830 appSearchData = new Bundle();
1831 } else {
1832 appSearchData = new Bundle(appSearchData);
1833 }
1834 // Set source to package name of app that starts global search, if not set already.
1835 if (!appSearchData.containsKey("source")) {
1836 appSearchData.putString("source", getPackageName());
1837 }
1838 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1839 if (!TextUtils.isEmpty(initialQuery)) {
1840 intent.putExtra(SearchManager.QUERY, initialQuery);
1841 }
1842 if (selectInitialQuery) {
1843 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1844 }
1845 intent.setSourceBounds(sourceBounds);
1846 try {
1847 startActivity(intent);
1848 } catch (ActivityNotFoundException ex) {
1849 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 }
1852
Winson Chung70d72102011-08-12 11:24:35 -07001853 @Override
1854 public boolean onCreateOptionsMenu(Menu menu) {
1855 if (isWorkspaceLocked()) {
1856 return false;
1857 }
1858
1859 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001860
1861 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1862 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1863 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001864 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1865 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1866 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001867 String helpUrl = getString(R.string.help_url);
1868 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001869 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1870 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1871
Winson Chung70d72102011-08-12 11:24:35 -07001872 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1873 .setIcon(android.R.drawable.ic_menu_gallery)
1874 .setAlphabeticShortcut('W');
1875 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1876 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001877 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001878 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001879 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1880 .setIcon(android.R.drawable.ic_menu_preferences)
1881 .setIntent(settings)
1882 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001883 if (!helpUrl.isEmpty()) {
1884 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1885 .setIcon(android.R.drawable.ic_menu_help)
1886 .setIntent(help)
1887 .setAlphabeticShortcut('H');
1888 }
Winson Chung70d72102011-08-12 11:24:35 -07001889 return true;
1890 }
1891
1892 @Override
1893 public boolean onPrepareOptionsMenu(Menu menu) {
1894 super.onPrepareOptionsMenu(menu);
1895
1896 if (mAppsCustomizeTabHost.isTransitioning()) {
1897 return false;
1898 }
1899 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1900 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1901
1902 return true;
1903 }
1904
1905 @Override
1906 public boolean onOptionsItemSelected(MenuItem item) {
1907 switch (item.getItemId()) {
1908 case MENU_WALLPAPER_SETTINGS:
1909 startWallpaper();
1910 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001911 }
1912
1913 return super.onOptionsItemSelected(item);
1914 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001916 @Override
1917 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001918 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001919 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001920 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001921 }
1922
Joe Onorato9c1289c2009-08-17 11:03:03 -04001923 public boolean isWorkspaceLocked() {
1924 return mWorkspaceLoading || mWaitingForResult;
1925 }
1926
Michael Jurka0280c3b2010-09-17 15:00:07 -07001927 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001928 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001929 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001930 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1931 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001932 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001933 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001934 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001935
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001936 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1937 AppWidgetProviderInfo appWidgetInfo) {
1938 if (appWidgetInfo.configure != null) {
1939 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001940
Bjorn Bringert7984c942009-12-09 15:38:25 +00001941 // Launch over to configure widget, if needed
1942 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001943 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001944 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001945 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001947 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001949 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001950 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001951 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 }
1953 }
Romain Guycbb89e42009-06-08 15:52:54 -07001954
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001955 protected void moveToCustomContentScreen(boolean animate) {
1956 mWorkspace.moveToCustomContentScreen(animate);
1957 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001958 /**
1959 * Process a shortcut drop.
1960 *
1961 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001962 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001963 * @param cell The cell it should be added to, optional
1964 * @param position The location on the screen where it was dropped, optional
1965 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001966 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001967 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001968 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001969 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001970 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001971 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001972
1973 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001974 mPendingAddInfo.cellX = cell[0];
1975 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001976 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001977
1978 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1979 createShortcutIntent.setComponent(componentName);
1980 processShortcut(createShortcutIntent);
1981 }
1982
Adam Cohenfbba09b2011-07-18 21:43:05 -07001983 /**
1984 * Process a widget drop.
1985 *
1986 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001988 * @param cell The cell it should be added to, optional
1989 * @param position The location on the screen where it was dropped, optional
1990 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001991 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001992 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001993 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001994 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001995 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001996 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001997 mPendingAddInfo.minSpanX = info.minSpanX;
1998 mPendingAddInfo.minSpanY = info.minSpanY;
1999
Adam Cohenfbba09b2011-07-18 21:43:05 -07002000 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002001 mPendingAddInfo.cellX = cell[0];
2002 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002003 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002004 if (span != null) {
2005 mPendingAddInfo.spanX = span[0];
2006 mPendingAddInfo.spanY = span[1];
2007 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002008
Adam Cohened66b2b2012-01-23 17:28:51 -08002009 AppWidgetHostView hostView = info.boundWidget;
2010 int appWidgetId;
2011 if (hostView != null) {
2012 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002013 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002014 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002015 // In this case, we either need to start an activity to get permission to bind
2016 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002017 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002018 Bundle options = info.bindOptions;
2019
2020 boolean success = false;
2021 if (options != null) {
2022 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2023 info.componentName, options);
2024 } else {
2025 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2026 info.componentName);
2027 }
2028 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002029 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002030 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002031 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002032 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2033 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2034 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002035 // TODO: we need to make sure that this accounts for the options bundle.
2036 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002037 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2038 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002039 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002040 }
2041
Joe Onoratodeb98af2010-02-19 14:59:39 -08002042 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002043 // Handle case where user selected "Applications"
2044 String applicationName = getResources().getString(R.string.group_applications);
2045 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002046
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002047 if (applicationName != null && applicationName.equals(shortcutName)) {
2048 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2049 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002050
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002051 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2052 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002053 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002054 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002055 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002056 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
2059
Winson Chung24ab2f12010-09-16 14:10:47 -07002060 void processWallpaper(Intent intent) {
2061 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2062 }
2063
Adam Cohendcd297f2013-06-18 13:13:40 -07002064 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002065 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002066 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 folderInfo.title = getText(R.string.folder_name);
2068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002070 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002071 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002072 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073
2074 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002075 FolderIcon newFolder =
2076 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002077 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002078 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002079 // Force measure the new folder icon
2080 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2081 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002082 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 }
Romain Guycbb89e42009-06-08 15:52:54 -07002084
Joe Onorato9c1289c2009-08-17 11:03:03 -04002085 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002086 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002087 }
2088
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002089 protected void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002090 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002092 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002093 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 }
2095
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002096 protected ComponentName getWallpaperPickerComponent() {
2097 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2098 }
2099
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002100 /**
2101 * Registers various content observers. The current implementation registers
2102 * only a favorites observer to keep track of the favorites applications.
2103 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002104 private void registerContentObservers() {
2105 ContentResolver resolver = getContentResolver();
2106 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2107 true, mWidgetObserver);
2108 }
2109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 @Override
2111 public boolean dispatchKeyEvent(KeyEvent event) {
2112 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2113 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002115 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002116 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002117 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002118 dumpState();
2119 return true;
2120 }
2121 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002122 }
2123 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2124 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002125 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 return true;
2127 }
2128 }
2129
2130 return super.dispatchKeyEvent(event);
2131 }
2132
Joe Onorato88ec0992009-11-19 13:16:06 -08002133 @Override
2134 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002135 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002136 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002137 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002138 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002139 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002140 Folder openFolder = mWorkspace.getOpenFolder();
2141 if (openFolder.isEditingName()) {
2142 openFolder.dismissEditingName();
2143 } else {
2144 closeFolder();
2145 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002146 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002147 mWorkspace.exitWidgetResizeMode();
2148
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002149 // Back button is a no-op here, but give at least some feedback for the button press
2150 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002151 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002152 }
2153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002155 * Re-listen when widgets are reset.
2156 */
2157 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002158 if (mAppWidgetHost != null) {
2159 mAppWidgetHost.startListening();
2160 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002161 }
2162
2163 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 * Launches the intent referred by the clicked shortcut.
2165 *
2166 * @param v The view representing the clicked shortcut.
2167 */
2168 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002169 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2170 // view has detached (it's possible for this to happen if the view is removed mid touch).
2171 if (v.getWindowToken() == null) {
2172 return;
2173 }
2174
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002175 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002176 return;
2177 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002178
Adam Cohen1697b792013-09-17 19:08:21 -07002179 if (v instanceof Workspace) {
2180 if (mWorkspace.isInOverviewMode()) {
2181 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002182 }
2183 return;
2184 }
2185
2186 if (v instanceof CellLayout) {
2187 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002188 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002189 }
2190 }
2191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002193 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002195 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2196 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002197
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002198 // Check for special shortcuts
2199 if (intent.getComponent() != null) {
2200 final String shortcutClass = intent.getComponent().getClassName();
2201
2202 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chungc58497e2013-09-03 17:48:37 -07002203 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002204 return;
2205 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2206 MemoryDumpActivity.startDump(this);
2207 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002208 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2209 toggleShowWeightWatcher();
2210 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002211 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002212 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002213
2214 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002215 int[] pos = new int[2];
2216 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002217 intent.setSourceBounds(new Rect(pos[0], pos[1],
2218 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002219
2220 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002221
Daniel Sandlerff02d492013-08-05 02:12:05 -04002222 mStats.recordLaunch(intent, shortcut);
2223
Michael Jurkaddd62e92011-02-16 17:49:14 -08002224 if (success && v instanceof BubbleTextView) {
2225 mWaitingForResume = (BubbleTextView) v;
2226 mWaitingForResume.setStayPressed(true);
2227 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002228 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002229 if (v instanceof FolderIcon) {
2230 FolderIcon fi = (FolderIcon) v;
2231 handleFolderClick(fi);
2232 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002233 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002234 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002235 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002236 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002237 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 }
2240 }
2241
Michael Jurka0e260592010-06-30 17:07:39 -07002242 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002243 return false;
2244 }
2245
Michael Jurkaaf442092010-06-10 17:01:57 -07002246 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002247 * Event handler for the search button
2248 *
2249 * @param v The view that was clicked.
2250 */
2251 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002252 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2253
Amith Yamasania135ba82011-08-09 17:42:01 -07002254 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002255 }
2256
2257 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002258 * Event handler for the voice button
2259 *
2260 * @param v The view that was clicked.
2261 */
2262 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002263 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002264
Bjorn Bringertc459e522013-06-07 19:36:01 +01002265 startVoice();
2266 }
2267
2268 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002269 try {
2270 final SearchManager searchManager =
2271 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2272 ComponentName activityName = searchManager.getGlobalSearchActivity();
2273 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002274 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002275 if (activityName != null) {
2276 intent.setPackage(activityName.getPackageName());
2277 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002278 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002279 } catch (ActivityNotFoundException e) {
2280 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002281 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002282 startActivitySafely(null, intent, "onClickVoiceButton");
2283 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002284 }
2285
2286 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002287 * Event handler for the "grid" button that appears on the home screen, which
2288 * enters all apps mode.
2289 *
2290 * @param v The view that was clicked.
2291 */
2292 public void onClickAllAppsButton(View v) {
Winson Chungc58497e2013-09-03 17:48:37 -07002293 showAllApps(true, AppsCustomizePagedView.ContentType.Applications);
Michael Jurka5130e402011-10-13 04:55:35 -07002294 }
2295
2296 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002297 // Provide the same haptic feedback that the system offers for virtual keys.
2298 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002299 }
2300
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002301 public void onClickAppMarketButton(View v) {
2302 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002303 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002304 } else {
2305 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002306 }
2307 }
2308
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002309 /**
2310 * Called when the user stops interacting with the launcher.
2311 * This implies that the user is now on the homescreen and is not doing housekeeping.
2312 */
2313 protected void onInteractionEnd() {}
2314
2315 /**
2316 * Called when the user starts interacting with the launcher.
2317 * The possible interactions are:
2318 * - open all apps
2319 * - reorder an app shortcut, or a widget
2320 * - open the overview mode.
2321 * This is a good time to stop doing things that only make sense
2322 * when the user is on the homescreen and not doing housekeeping.
2323 */
2324 protected void onInteractionBegin() {}
2325
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002326 void startApplicationDetailsActivity(ComponentName componentName) {
2327 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002328 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2329 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002330 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002331 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002332 }
2333
Michael Jurka1e2f4652013-07-08 18:03:46 -07002334 // returns true if the activity was started
2335 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002336 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002337 // System applications cannot be installed. For now, show a toast explaining that.
2338 // We may give them the option of disabling apps this way.
2339 int messageId = R.string.uninstall_system_app_text;
2340 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002341 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002342 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002343 String packageName = componentName.getPackageName();
2344 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002345 Intent intent = new Intent(
2346 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002347 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2348 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002349 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002350 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002351 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002352 }
2353
Michael Jurka86a720e2012-05-09 11:23:23 -07002354 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002356
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002357 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002358 // Only launch using the new animation if the shortcut has not opted out (this is a
2359 // private contract between launcher and may be ignored in the future).
2360 boolean useLaunchAnimation = (v != null) &&
2361 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2362 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002363 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2364 v.getMeasuredWidth(), v.getMeasuredHeight());
2365
2366 startActivity(intent, opts.toBundle());
2367 } else {
2368 startActivity(intent);
2369 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002370 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 } catch (SecurityException e) {
2372 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002373 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002375 "or use the exported attribute for this activity. "
2376 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002378 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002379 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002380
Michael Jurka86a720e2012-05-09 11:23:23 -07002381 boolean startActivitySafely(View v, Intent intent, Object tag) {
2382 boolean success = false;
2383 try {
2384 success = startActivity(v, intent, tag);
2385 } catch (ActivityNotFoundException e) {
2386 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2387 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2388 }
2389 return success;
2390 }
2391
Adam Cohena9cf38f2011-05-02 15:36:58 -07002392 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002393 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002394 Folder openFolder = mWorkspace.getFolderForTag(info);
2395
2396 // If the folder info reports that the associated folder is open, then verify that
2397 // it is actually opened. There have been a few instances where this gets out of sync.
2398 if (info.opened && openFolder == null) {
2399 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002400 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002401 info.opened = false;
2402 }
2403
Adam Cohenfb91f302012-06-11 15:45:18 -07002404 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 // Close any open folder
2406 closeFolder();
2407 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002408 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 } else {
2410 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 int folderScreen;
2412 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002413 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 // .. and close it
2415 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002416 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 // Close any folder open on the current screen
2418 closeFolder();
2419 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002420 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 }
2422 }
2423 }
2424 }
2425
Adam Cohen268c4752012-06-06 17:47:33 -07002426 /**
2427 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2428 * in the DragLayer in the exact absolute location of the original FolderIcon.
2429 */
2430 private void copyFolderIconToImage(FolderIcon fi) {
2431 final int width = fi.getMeasuredWidth();
2432 final int height = fi.getMeasuredHeight();
2433
2434 // Lazy load ImageView, Bitmap and Canvas
2435 if (mFolderIconImageView == null) {
2436 mFolderIconImageView = new ImageView(this);
2437 }
2438 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2439 mFolderIconBitmap.getHeight() != height) {
2440 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2441 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2442 }
2443
2444 DragLayer.LayoutParams lp;
2445 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2446 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2447 } else {
2448 lp = new DragLayer.LayoutParams(width, height);
2449 }
2450
Adam Cohen307fe232012-08-16 17:55:58 -07002451 // The layout from which the folder is being opened may be scaled, adjust the starting
2452 // view size by this scale factor.
2453 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002454 lp.customPosition = true;
2455 lp.x = mRectForFolderAnimation.left;
2456 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002457 lp.width = (int) (scale * width);
2458 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002459
2460 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2461 fi.draw(mFolderIconCanvas);
2462 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002463 if (fi.getFolder() != null) {
2464 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2465 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002466 }
Adam Cohen268c4752012-06-06 17:47:33 -07002467 // Just in case this image view is still in the drag layer from a previous animation,
2468 // we remove it and re-add it.
2469 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2470 mDragLayer.removeView(mFolderIconImageView);
2471 }
2472 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002473 if (fi.getFolder() != null) {
2474 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002475 }
Adam Cohen268c4752012-06-06 17:47:33 -07002476 }
2477
Adam Cohen2801caf2011-05-13 20:57:39 -07002478 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002479 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002480 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2481 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2482 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2483
Adam Cohenc51934b2011-07-26 21:07:43 -07002484 FolderInfo info = (FolderInfo) fi.getTag();
2485 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2486 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002487 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2488 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002489 }
2490
Adam Cohen268c4752012-06-06 17:47:33 -07002491 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2492 copyFolderIconToImage(fi);
2493 fi.setVisibility(View.INVISIBLE);
2494
Michael Jurka2ecf9952012-06-18 12:52:28 -07002495 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002496 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002497 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2498 oa.start();
2499 }
2500
Adam Cohen268c4752012-06-06 17:47:33 -07002501 private void shrinkAndFadeInFolderIcon(final 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", 1.0f);
2504 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2505 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2506
Adam Cohen268c4752012-06-06 17:47:33 -07002507 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002508
Adam Cohen268c4752012-06-06 17:47:33 -07002509 // We remove and re-draw the FolderIcon in-case it has changed
2510 mDragLayer.removeView(mFolderIconImageView);
2511 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002512 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002513 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002514 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002515 oa.addListener(new AnimatorListenerAdapter() {
2516 @Override
2517 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002518 if (cl != null) {
2519 cl.clearFolderLeaveBehind();
2520 // Remove the ImageView copy of the FolderIcon and make the original visible.
2521 mDragLayer.removeView(mFolderIconImageView);
2522 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002523 }
2524 }
2525 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002526 oa.start();
2527 }
2528
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002530 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 * is animated relative to the specified View. If the View is null, no animation
2532 * is played.
2533 *
2534 * @param folderInfo The FolderInfo describing the folder to open.
2535 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002536 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002537 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002538 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539
Adam Cohena9cf38f2011-05-02 15:36:58 -07002540 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541
Adam Cohen4554ee12011-08-03 16:13:21 -07002542 // Just verify that the folder hasn't already been added to the DragLayer.
2543 // There was a one-off crash where the folder had a parent already.
2544 if (folder.getParent() == null) {
2545 mDragLayer.addView(folder);
2546 mDragController.addDropTarget((DropTarget) folder);
2547 } else {
2548 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2549 folder.getParent() + ").");
2550 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002551 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002552 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002553
2554 // Notify the accessibility manager that this folder "window" has appeared and occluded
2555 // the workspace items
2556 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2557 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002558 }
2559
2560 public void closeFolder() {
2561 Folder folder = mWorkspace.getOpenFolder();
2562 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002563 if (folder.isEditingName()) {
2564 folder.dismissEditingName();
2565 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002566 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002567
2568 // Dismiss the folder cling
2569 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002570 }
2571 }
2572
2573 void closeFolder(Folder folder) {
2574 folder.getInfo().opened = false;
2575
2576 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2577 if (parent != null) {
2578 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2579 shrinkAndFadeInFolderIcon(fi);
2580 }
2581 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002582
2583 // Notify the accessibility manager that this folder "window" has disappeard and no
2584 // longer occludeds the workspace items
2585 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586 }
2587
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002589 if (!isDraggingEnabled()) return false;
2590 if (isWorkspaceLocked()) return false;
2591 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592
Adam Cohen1697b792013-09-17 19:08:21 -07002593 if (v instanceof Workspace) {
2594 if (!mWorkspace.isInOverviewMode()) {
2595 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2596 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2597 mWorkspace.enterOverviewMode();
2598 }
2599 return true;
2600 }
2601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002603 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 }
2605
Michael Jurka0280c3b2010-09-17 15:00:07 -07002606 resetAddInfo();
2607 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002608 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002609 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002610 return true;
2611 }
2612
Winson Chung3d503fb2011-07-13 17:25:49 -07002613 // The hotseat touch handling does not go through Workspace, and we always allow long press
2614 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002615 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002616 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2617 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002618 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002619 // User long pressed on empty space
2620 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2621 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2622 // Disabling reordering until we sort out some issues.
2623 if (mWorkspace.isInOverviewMode()) {
2624 mWorkspace.startReordering(v);
2625 } else {
2626 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002628 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002629 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002630 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002631 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002632 }
2633 }
2634 }
2635 return true;
2636 }
2637
Winson Chung3d503fb2011-07-13 17:25:49 -07002638 boolean isHotseatLayout(View layout) {
2639 return mHotseat != null && layout != null &&
2640 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2641 }
2642 Hotseat getHotseat() {
2643 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002644 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002645 View getOverviewPanel() {
2646 return mOverviewPanel;
2647 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002648 SearchDropTargetBar getSearchBar() {
2649 return mSearchDropTargetBar;
2650 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002651
Winson Chung3d503fb2011-07-13 17:25:49 -07002652 /**
2653 * Returns the CellLayout of the specified container at the specified screen.
2654 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002655 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002656 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2657 if (mHotseat != null) {
2658 return mHotseat.getLayout();
2659 } else {
2660 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002661 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002662 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002663 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002664 }
2665 }
2666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002667 Workspace getWorkspace() {
2668 return mWorkspace;
2669 }
2670
Daniel Sandler843e8602010-06-07 14:59:01 -04002671 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002672 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002673 }
2674
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002675 /**
2676 * Helper method for the cameraZoomIn/cameraZoomOut animations
2677 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002678 * @param scaleFactor The scale factor used for the zoom
2679 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002680 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002681 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002682 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002683 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002684
Winson Chung18f41f82012-05-09 13:28:10 -07002685 void disableWallpaperIfInAllApps() {
2686 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002687 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002688 if (mAppsCustomizeTabHost != null &&
2689 !mAppsCustomizeTabHost.isTransitioning()) {
2690 updateWallpaperVisibility(false);
2691 }
2692 }
2693 }
2694
Craig Mautner360310b2012-10-26 15:13:08 -07002695 private void setWorkspaceBackground(boolean workspace) {
2696 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002697 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002698 }
2699
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002700 void updateWallpaperVisibility(boolean visible) {
2701 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2702 int curflags = getWindow().getAttributes().flags
2703 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2704 if (wpflags != curflags) {
2705 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2706 }
Craig Mautner360310b2012-10-26 15:13:08 -07002707 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002708 }
2709
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002710 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2711 if (v instanceof LauncherTransitionable) {
2712 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2713 }
2714 }
2715
Michael Jurkabed61d22012-02-14 22:51:29 -08002716 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2717 if (v instanceof LauncherTransitionable) {
2718 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2719 }
Winson Chung70442722012-02-10 15:43:22 -08002720
2721 // Update the workspace transition step as well
2722 dispatchOnLauncherTransitionStep(v, 0f);
2723 }
2724
2725 private void dispatchOnLauncherTransitionStep(View v, float t) {
2726 if (v instanceof LauncherTransitionable) {
2727 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2728 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002729 }
2730
2731 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2732 if (v instanceof LauncherTransitionable) {
2733 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2734 }
Winson Chung70442722012-02-10 15:43:22 -08002735
2736 // Update the workspace transition step as well
2737 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002738 }
2739
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002740 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002741 * Things to test when changing the following seven functions.
2742 * - Home from workspace
2743 * - from center screen
2744 * - from other screens
2745 * - Home from all apps
2746 * - from center screen
2747 * - from other screens
2748 * - Back from all apps
2749 * - from center screen
2750 * - from other screens
2751 * - Launch app from workspace and quit
2752 * - with back
2753 * - with home
2754 * - Launch app from all apps and quit
2755 * - with back
2756 * - with home
2757 * - Go to a screen that's not the default, then all
2758 * apps, and launch and app, and go back
2759 * - with back
2760 * -with home
2761 * - On workspace, long press power and go back
2762 * - with back
2763 * - with home
2764 * - On all apps, long press power and go back
2765 * - with back
2766 * - with home
2767 * - On workspace, power off
2768 * - On all apps, power off
2769 * - Launch an app and turn off the screen while in that app
2770 * - Go back with home key
2771 * - Go back with back key TODO: make this not go to workspace
2772 * - From all apps
2773 * - From workspace
2774 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2775 * - From all apps
2776 * - From the center workspace
2777 * - From another workspace
2778 */
2779
2780 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002781 * Zoom the camera out from the workspace to reveal 'toView'.
2782 * Assumes that the view to show is anchored at either the very top or very bottom
2783 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002784 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002785 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002786 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2787 showAppsCustomizeHelper(animated, springLoaded, contentType);
2788 }
2789 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2790 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002791 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002792 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002793 mStateAnimation.cancel();
2794 mStateAnimation = null;
2795 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002796 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002797
Winson Chungf0ea4d32011-06-06 14:27:16 -07002798 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2799 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2800 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002801 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002802 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002803 final int startDelay =
2804 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002805
Michael Jurkab3e22d92011-10-31 15:58:33 -07002806 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002807
Michael Jurkad74c9842011-07-10 12:44:21 -07002808 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002809 Animator workspaceAnim =
2810 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002811 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2812 // Set the content type for the all apps space
2813 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2814 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002815
2816 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002817 toView.setScaleX(scale);
2818 toView.setScaleY(scale);
2819 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2820 scaleAnim.
2821 scaleX(1f).scaleY(1f).
2822 setDuration(duration).
2823 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002824
Winson Chungf0ea4d32011-06-06 14:27:16 -07002825 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002826 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002827 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002828 .ofFloat(toView, "alpha", 0f, 1f)
2829 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002830 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002831 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2832 @Override
2833 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002834 if (animation == null) {
2835 throw new RuntimeException("animation is null");
2836 }
Winson Chung70442722012-02-10 15:43:22 -08002837 float t = (Float) animation.getAnimatedValue();
2838 dispatchOnLauncherTransitionStep(fromView, t);
2839 dispatchOnLauncherTransitionStep(toView, t);
2840 }
2841 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002842
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002843 // toView should appear right at the end of the workspace shrink
2844 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002845 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002846 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002847 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002848
2849 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002850 boolean animationCancelled = false;
2851
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002852 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002853 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002854 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002855 // Prepare the position
2856 toView.setTranslationX(0.0f);
2857 toView.setTranslationY(0.0f);
2858 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002859 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002860 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002861 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002862 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002863 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2864 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002865
Adam Cohenf4ddea32011-09-12 13:46:42 -07002866 if (!animationCancelled) {
2867 updateWallpaperVisibility(false);
2868 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002869
2870 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002871 if (mSearchDropTargetBar != null) {
2872 mSearchDropTargetBar.hideSearchBar(false);
2873 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002874 }
2875
Adam Cohencff6af82011-09-13 14:51:53 -07002876 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002877 public void onAnimationCancel(Animator animation) {
2878 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002879 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002880 });
2881
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002882 if (workspaceAnim != null) {
2883 mStateAnimation.play(workspaceAnim);
2884 }
Michael Jurka1899a362011-11-03 13:50:45 -07002885
2886 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002887
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002888 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2889 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002890
2891 // If any of the objects being animated haven't been measured/laid out
2892 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002893 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002894 (mWorkspace.getMeasuredWidth() == 0) ||
2895 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002896 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002897 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002898
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002899 final AnimatorSet stateAnimation = mStateAnimation;
2900 final Runnable startAnimRunnable = new Runnable() {
2901 public void run() {
2902 // Check that mStateAnimation hasn't changed while
2903 // we waited for a layout/draw pass
2904 if (mStateAnimation != stateAnimation)
2905 return;
2906 setPivotsForZoom(toView, scale);
2907 dispatchOnLauncherTransitionStart(fromView, animated, false);
2908 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002909 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002910 }
2911 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002912 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002913 final ViewTreeObserver observer = toView.getViewTreeObserver();
2914 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2915 public void onGlobalLayout() {
2916 startAnimRunnable.run();
2917 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2918 }
2919 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002920 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002921 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002922 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002923 } else {
2924 toView.setTranslationX(0.0f);
2925 toView.setTranslationY(0.0f);
2926 toView.setScaleX(1.0f);
2927 toView.setScaleY(1.0f);
2928 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002929 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002930
Daniel Sandlere4f98912013-06-25 15:13:26 -04002931 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002932 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002933 if (mSearchDropTargetBar != null) {
2934 mSearchDropTargetBar.hideSearchBar(false);
2935 }
Michael Jurkaabded662011-03-04 12:06:57 -08002936 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002937 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002938 dispatchOnLauncherTransitionStart(fromView, animated, false);
2939 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002940 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002941 dispatchOnLauncherTransitionStart(toView, animated, false);
2942 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002943 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002944 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002945 }
2946
2947 /**
2948 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002949 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002950 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002951 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002952 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2953 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002954
Michael Jurkab3e22d92011-10-31 15:58:33 -07002955 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002956 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002957 mStateAnimation.cancel();
2958 mStateAnimation = null;
2959 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002960 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002961
Winson Chungf0ea4d32011-06-06 14:27:16 -07002962 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002963 final int fadeOutDuration =
2964 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002965 final float scaleFactor = (float)
2966 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2967 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002968 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002969 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002970 if (toState == State.WORKSPACE) {
2971 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2972 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002973 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002974 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2975 workspaceAnim = mWorkspace.getChangeStateAnimation(
2976 Workspace.State.SPRING_LOADED, animated);
2977 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002978
Michael Jurkab3e22d92011-10-31 15:58:33 -07002979 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002980 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002981 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002982 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002983 final LauncherViewPropertyAnimator scaleAnim =
2984 new LauncherViewPropertyAnimator(fromView);
2985 scaleAnim.
2986 scaleX(scaleFactor).scaleY(scaleFactor).
2987 setDuration(duration).
2988 setInterpolator(new Workspace.ZoomInInterpolator());
2989
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002990 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002991 .ofFloat(fromView, "alpha", 1f, 0f)
2992 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002993 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002994 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2995 @Override
2996 public void onAnimationUpdate(ValueAnimator animation) {
2997 float t = 1f - (Float) animation.getAnimatedValue();
2998 dispatchOnLauncherTransitionStep(fromView, t);
2999 dispatchOnLauncherTransitionStep(toView, t);
3000 }
3001 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003002
Michael Jurka2ecf9952012-06-18 12:52:28 -07003003 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003004
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003005 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3006 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07003007 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003008
3009 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003010 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003011 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003012 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003013 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003014 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3015 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003016 if (onCompleteRunnable != null) {
3017 onCompleteRunnable.run();
3018 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003019 mAppsCustomizeContent.updateCurrentPageScroll();
3020 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003021 }
3022 });
3023
Winson Chungf0ea4d32011-06-06 14:27:16 -07003024 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003025 if (workspaceAnim != null) {
3026 mStateAnimation.play(workspaceAnim);
3027 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003028 dispatchOnLauncherTransitionStart(fromView, animated, true);
3029 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003030 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003031 } else {
3032 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003033 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003034 dispatchOnLauncherTransitionStart(fromView, animated, true);
3035 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003036 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003037 dispatchOnLauncherTransitionStart(toView, animated, true);
3038 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003039 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003040 }
3041
Michael Jurkae326f182011-11-21 14:05:46 -08003042 @Override
3043 public void onTrimMemory(int level) {
3044 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003045 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003046 mAppsCustomizeTabHost.onTrimMemory();
3047 }
3048 }
3049
Winson Chung18f41f82012-05-09 13:28:10 -07003050 @Override
3051 public void onWindowFocusChanged(boolean hasFocus) {
3052 if (!hasFocus) {
3053 // When another window occludes launcher (like the notification shade, or recents),
3054 // ensure that we enable the wallpaper flag so that transitions are done correctly.
3055 updateWallpaperVisibility(true);
3056 } else {
3057 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07003058 mWorkspace.postDelayed(new Runnable() {
3059 @Override
3060 public void run() {
3061 disableWallpaperIfInAllApps();
3062 }
3063 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07003064 }
3065 }
3066
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003067 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003068 showWorkspace(animated, null);
3069 }
3070
3071 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003072 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003073 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003074 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003075 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003076
Winson Chungc7d2b602012-05-16 17:02:20 -07003077 // Show the search bar (only animate if we were showing the drop target bar in spring
3078 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003079 if (mSearchDropTargetBar != null) {
3080 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3081 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003082
Michael Jurkab3e22d92011-10-31 15:58:33 -07003083 // Set focus to the AppsCustomize button
3084 if (mAllAppsButton != null) {
3085 mAllAppsButton.requestFocus();
3086 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003087 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003088
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003089 // Change the state *after* we've called all the transition code
3090 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003091
Winson Chung5fb63472011-02-02 17:03:37 -08003092 // Resume the auto-advance of widgets
3093 mUserPresent = true;
3094 updateRunning();
3095
alanv1d4fde62012-10-17 13:15:47 -07003096 // Send an accessibility event to announce the context change
3097 getWindow().getDecorView()
3098 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003099
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003100 onWorkspaceShown(animated);
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07003101 onInteractionEnd();
Bjorn Bringertc459e522013-06-07 19:36:01 +01003102 }
3103
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003104 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003105 }
3106
Winson Chungc58497e2013-09-03 17:48:37 -07003107 void showAllApps(boolean animated,
3108 AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003109 if (mState != State.WORKSPACE) return;
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;
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07003116 onInteractionBegin();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003117
3118 // Pause the auto-advance of widgets until we are out of AllApps
3119 mUserPresent = false;
3120 updateRunning();
3121 closeFolder();
3122
3123 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003124 getWindow().getDecorView()
3125 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003126 }
3127
Adam Cohen7777d962011-08-18 18:58:38 -07003128 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003129 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003130 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003131 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003132 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003133 }
Adam Cohen7777d962011-08-18 18:58:38 -07003134
Adam Cohened66b2b2012-01-23 17:28:51 -08003135 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3136 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003137 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3138
Winson Chunge7a03942011-08-05 15:05:12 -07003139 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003140 @Override
3141 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003142 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003143 // Before we show workspace, hide all apps again because
3144 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3145 // clean up our state transition functions
3146 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003147 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003148 } else {
3149 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003150 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003151 }
3152 }, (extendedDelay ?
3153 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3154 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3155 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003156
Michael Jurkad3ef3062010-11-23 16:23:58 -08003157 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003158 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003159 final boolean animated = true;
3160 final boolean springLoaded = true;
3161 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003162 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003163 }
3164 // Otherwise, we are not in spring loaded mode, so don't do anything.
3165 }
3166
Michael Jurkab3e22d92011-10-31 15:58:33 -07003167 void lockAllApps() {
3168 // TODO
3169 }
3170
3171 void unlockAllApps() {
3172 // TODO
3173 }
3174
Winson Chungf0ea4d32011-06-06 14:27:16 -07003175 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003176 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003177 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003178 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003179 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003180 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003181 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003182 int duration = 0;
3183 if (mSearchDropTargetBar != null) {
3184 duration = mSearchDropTargetBar.getTransitionInDuration();
3185 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003186 mHotseat.animate().alpha(1f).setDuration(duration);
3187 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003188 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003189 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003190 }
3191 }
3192 }
3193
3194 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003195 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003196 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003197 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003198 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003199 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003200 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003201 int duration = 0;
3202 if (mSearchDropTargetBar != null) {
3203 duration = mSearchDropTargetBar.getTransitionOutDuration();
3204 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003205 mHotseat.animate().alpha(0f).setDuration(duration);
3206 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003207 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003209 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003210 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003211 }
3212
Patrick Dubroy5f445422011-02-18 14:35:21 -08003213 /**
3214 * Add an item from all apps or customize onto the given workspace screen.
3215 * If layout is null, add to the current screen.
3216 */
3217 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003218 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003219 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003220 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003221 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003222
Winson Chungdff8ebb2011-09-08 17:25:31 -07003223 /** Maps the current orientation to an index for referencing orientation correct global icons */
3224 private int getCurrentOrientationIndexForGlobalIcons() {
3225 // default - 0, landscape - 1
3226 switch (getResources().getConfiguration().orientation) {
3227 case Configuration.ORIENTATION_LANDSCAPE:
3228 return 1;
3229 default:
3230 return 0;
3231 }
3232 }
3233
Michael Jurkaae65ee32012-04-30 11:45:54 -07003234 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003235 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003236 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003237 // Look for the toolbar icon specified in the activity meta-data
3238 Bundle metaData = packageManager.getActivityInfo(
3239 activityName, PackageManager.GET_META_DATA).metaData;
3240 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003241 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003242 if (iconResId != 0) {
3243 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003244 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003245 }
3246 }
3247 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003248 // This can happen if the activity defines an invalid drawable
3249 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3250 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003251 } catch (Resources.NotFoundException nfe) {
3252 // This can happen if the activity defines an invalid drawable
3253 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3254 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003255 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003256 return null;
3257 }
3258
3259 // if successful in getting icon, return it; otherwise, set button to use default drawable
3260 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003261 int buttonId, ComponentName activityName, int fallbackDrawableId,
3262 String toolbarResourceName) {
3263 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003264 Resources r = getResources();
3265 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3266 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003267
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003268 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003269 // If we were unable to find the icon via the meta-data, use a generic one
3270 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003271 toolbarIcon = r.getDrawable(fallbackDrawableId);
3272 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003273 if (button != null) {
3274 button.setCompoundDrawables(toolbarIcon, null, null, null);
3275 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003276 return null;
3277 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003278 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003279 if (button != null) {
3280 button.setCompoundDrawables(toolbarIcon, null, null, null);
3281 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003282 return toolbarIcon.getConstantState();
3283 }
3284 }
3285
3286 // if successful in getting icon, return it; otherwise, set button to use default drawable
3287 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003288 int buttonId, ComponentName activityName, int fallbackDrawableId,
3289 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003290 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003291 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003292
Michael Jurka19e0fc52011-07-22 18:00:21 -07003293 if (button != null) {
3294 // If we were unable to find the icon via the meta-data, use a
3295 // generic one
3296 if (toolbarIcon == null) {
3297 button.setImageResource(fallbackDrawableId);
3298 } else {
3299 button.setImageDrawable(toolbarIcon);
3300 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003301 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003302
3303 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3304
Michael Jurka0423dcf2010-10-05 14:56:18 -07003305 }
3306
Winson Chung1b884092012-06-08 17:13:02 -07003307 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003308 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003309 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003310 }
3311
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003312 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003313 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003314 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003315 }
3316
Winson Chungbb185bd2011-11-21 12:31:42 -08003317 private void invalidatePressedFocusedStates(View container, View button) {
3318 if (container instanceof HolographicLinearLayout) {
3319 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3320 layout.invalidatePressedFocusedStates();
3321 } else if (button instanceof HolographicImageView) {
3322 HolographicImageView view = (HolographicImageView) button;
3323 view.invalidatePressedFocusedStates();
3324 }
3325 }
3326
Cristina Stancu476493b2013-08-07 17:20:14 +01003327 public View getQsbBar() {
3328 if (mQsbBar == null) {
3329 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3330 }
3331 return mQsbBar;
3332 }
3333
3334 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003335 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003336 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003337 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003338 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003339
Winson Chung1cad91e2011-05-25 17:41:01 -07003340 final SearchManager searchManager =
3341 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3342 ComponentName activityName = searchManager.getGlobalSearchActivity();
3343 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003344 int coi = getCurrentOrientationIndexForGlobalIcons();
3345 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003346 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3347 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3348 if (sGlobalSearchIcon[coi] == null) {
3349 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3350 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3351 TOOLBAR_ICON_METADATA_NAME);
3352 }
3353
Winson Chungc51db6a2011-10-05 11:44:49 -07003354 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3355 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003356 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003357 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003358 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003359 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003360 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3361 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003362 if (searchButton != null) searchButton.setVisibility(View.GONE);
3363 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003364 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003365 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003366 }
3367 }
3368
Cristina Stancu476493b2013-08-07 17:20:14 +01003369 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003370 final View searchButtonContainer = findViewById(R.id.search_button_container);
3371 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003372 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003373 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003374 }
3375
Cristina Stancu476493b2013-08-07 17:20:14 +01003376 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003377 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003378 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003379
Winson Chungc51db6a2011-10-05 11:44:49 -07003380 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003381 final SearchManager searchManager =
3382 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3383 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3384
3385 ComponentName activityName = null;
3386 if (globalSearchActivity != null) {
3387 // Check if the global search activity handles voice search
3388 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3389 intent.setPackage(globalSearchActivity.getPackageName());
3390 activityName = intent.resolveActivity(getPackageManager());
3391 }
3392
3393 if (activityName == null) {
3394 // Fallback: check if an activity other than the global search activity
3395 // resolves this
3396 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3397 activityName = intent.resolveActivity(getPackageManager());
3398 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003399 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003400 int coi = getCurrentOrientationIndexForGlobalIcons();
3401 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003402 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3403 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3404 if (sVoiceSearchIcon[coi] == null) {
3405 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3406 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3407 TOOLBAR_ICON_METADATA_NAME);
3408 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003409 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003410 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003411 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003412 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003413 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003414 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003415 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003416 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003417 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003418 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003419 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003420 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003421
Cristina Stancu476493b2013-08-07 17:20:14 +01003422 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003423 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3424 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003425 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003426 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003427 }
3428
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003429 public void setVoiceButtonProxyVisible(boolean visible) {
3430 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3431 if (voiceButtonProxy != null) {
3432 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3433 }
3434 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003435 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003436 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003437 */
3438 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003439 final View marketButton = findViewById(R.id.market_button);
3440 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3441 // Find the app market activity by resolving an intent.
3442 // (If multiple app markets are installed, it will return the ResolverActivity.)
3443 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003444 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003445 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003446 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003447 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003448 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3449 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003450 marketButton.setVisibility(View.VISIBLE);
3451 } else {
3452 // We should hide and disable the view so that we don't try and restore the visibility
3453 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3454 marketButton.setVisibility(View.GONE);
3455 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003456 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003457 }
3458
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003459 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003460 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3461 Resources r = getResources();
3462 Drawable marketIconDrawable = d.newDrawable(r);
3463 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3464 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3465 marketIconDrawable.setBounds(0, 0, w, h);
3466
3467 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003468 }
3469
Michael Jurkad7c28052012-04-27 15:43:36 -07003470 @Override
3471 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003472 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003473 final List<CharSequence> text = event.getText();
3474 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003475 // Populate event with a fake title based on the current state.
3476 if (mState == State.APPS_CUSTOMIZE) {
3477 text.add(getString(R.string.all_apps_button_label));
3478 } else {
3479 text.add(getString(R.string.all_apps_home_button_label));
3480 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003481 return result;
3482 }
3483
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003484 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003485 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003486 */
3487 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3488 @Override
3489 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003490 closeSystemDialogs();
3491 }
3492 }
3493
3494 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003495 * Receives notifications whenever the appwidgets are reset.
3496 */
3497 private class AppWidgetResetObserver extends ContentObserver {
3498 public AppWidgetResetObserver() {
3499 super(new Handler());
3500 }
3501
3502 @Override
3503 public void onChange(boolean selfChange) {
3504 onAppWidgetReset();
3505 }
3506 }
3507
3508 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003509 * If the activity is currently paused, signal that we need to run the passed Runnable
3510 * in onResume.
3511 *
3512 * This needs to be called from incoming places where resources might have been loaded
3513 * while we are paused. That is becaues the Configuration might be wrong
3514 * when we're not running, and if it comes back to what it was when we
3515 * were paused, we are not restarted.
3516 *
3517 * Implementation of the method from LauncherModel.Callbacks.
3518 *
3519 * @return true if we are currently paused. The caller might be able to
3520 * skip some work in that case since we will come back again.
3521 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003522 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003523 if (mPaused) {
3524 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003525 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003526 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003527 }
3528 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003529 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003530 return true;
3531 } else {
3532 return false;
3533 }
3534 }
3535
Michael Jurkac402cd92013-05-20 15:49:32 +02003536 private boolean waitUntilResume(Runnable run) {
3537 return waitUntilResume(run, false);
3538 }
3539
Michael Jurka1e2f4652013-07-08 18:03:46 -07003540 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003541 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003542 }
3543
Michael Jurka7607c2f2013-04-03 14:33:19 -07003544 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003545 * If the activity is currently paused, signal that we need to re-run the loader
3546 * in onResume.
3547 *
3548 * This needs to be called from incoming places where resources might have been loaded
3549 * while we are paused. That is becaues the Configuration might be wrong
3550 * when we're not running, and if it comes back to what it was when we
3551 * were paused, we are not restarted.
3552 *
3553 * Implementation of the method from LauncherModel.Callbacks.
3554 *
3555 * @return true if we are currently paused. The caller might be able to
3556 * skip some work in that case since we will come back again.
3557 */
3558 public boolean setLoadOnResume() {
3559 if (mPaused) {
3560 Log.i(TAG, "setLoadOnResume");
3561 mOnResumeNeedsLoad = true;
3562 return true;
3563 } else {
3564 return false;
3565 }
3566 }
3567
3568 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003570 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003572 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003573 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003574 } else {
3575 return SCREEN_COUNT / 2;
3576 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003578
Joe Onorato9c1289c2009-08-17 11:03:03 -04003579 /**
3580 * Refreshes the shortcuts shown on the workspace.
3581 *
3582 * Implementation of the method from LauncherModel.Callbacks.
3583 */
3584 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003585 // If we're starting binding all over again, clear any bind calls we'd postponed in
3586 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3587 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003588 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003589
Winson Chungd64d1762013-08-20 14:37:16 -07003590 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003591 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003592 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003593
Michael Jurka05bf6442011-11-30 20:28:53 -08003594 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003595 if (mHotseat != null) {
3596 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003597 }
3598 }
3599
Adam Cohendcd297f2013-06-18 13:13:40 -07003600 @Override
3601 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003602 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003603
Adam Cohen5084cba2013-09-03 12:01:16 -07003604 // If there are no screens, we need to have an empty screen
3605 if (orderedScreenIds.size() == 0) {
3606 mWorkspace.addExtraEmptyScreen();
3607 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003608
3609 // Create the custom content page (this call updates mDefaultScreen which calls
3610 // setCurrentPage() so ensure that all pages are added before calling this)
3611 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3612 mWorkspace.createCustomContentPage();
3613 }
Winson Chung64359a52013-07-08 17:17:08 -07003614 }
3615
3616 @Override
3617 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003618 int count = orderedScreenIds.size();
3619 for (int i = 0; i < count; i++) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003620 Launcher.addDumpLog(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")", true);
Adam Cohen89bddfa2013-08-20 11:57:13 -07003621 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003622 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003623 }
3624
Adam Cohen39a06042013-07-19 14:30:12 -07003625 private boolean shouldShowWeightWatcher() {
3626 String spKey = LauncherAppState.getSharedPreferencesKey();
3627 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003628 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003629
3630 return show;
3631 }
3632
Adam Cohen4caf2982013-08-20 18:54:31 -07003633 private boolean emailSent() {
3634 String spKey = LauncherAppState.getSharedPreferencesKey();
3635 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3636 boolean show = sp.getBoolean(CORRUPTION_EMAIL_SENT_KEY, false);
3637 return show;
3638 }
3639
3640 private void setEmailSent(boolean sent) {
3641 String spKey = LauncherAppState.getSharedPreferencesKey();
3642 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3643
3644 SharedPreferences.Editor editor = sp.edit();
3645 editor.putBoolean(CORRUPTION_EMAIL_SENT_KEY, sent);
3646 editor.commit();
3647 }
3648
Adam Cohen39a06042013-07-19 14:30:12 -07003649 private void toggleShowWeightWatcher() {
3650 String spKey = LauncherAppState.getSharedPreferencesKey();
3651 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3652 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3653
3654 show = !show;
3655
3656 SharedPreferences.Editor editor = sp.edit();
3657 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3658 editor.commit();
3659
3660 if (mWeightWatcher != null) {
3661 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3662 }
3663 }
3664
Winson Chungd64d1762013-08-20 14:37:16 -07003665 public void bindAppsAdded(final ArrayList<Long> newScreens,
3666 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003667 final ArrayList<ItemInfo> addAnimated,
3668 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003669 Runnable r = new Runnable() {
3670 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003671 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003672 }
3673 };
3674 if (waitUntilResume(r)) {
3675 return;
3676 }
3677
Adam Cohen4caf2982013-08-20 18:54:31 -07003678 Launcher.addDumpLog(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -07003679
3680 // Add the new screens
3681 bindAddScreens(newScreens);
3682
3683 // We add the items without animation on non-visible pages, and with
3684 // animations on the new page (which we will try and snap to).
3685 if (!addNotAnimated.isEmpty()) {
3686 bindItems(addNotAnimated, 0,
3687 addNotAnimated.size(), false);
3688 }
3689 if (!addAnimated.isEmpty()) {
3690 bindItems(addAnimated, 0,
3691 addAnimated.size(), true);
3692 }
Winson Chungc58497e2013-09-03 17:48:37 -07003693
3694 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3695 addedApps != null && mAppsCustomizeContent != null) {
3696 mAppsCustomizeContent.addApps(addedApps);
3697 }
Winson Chungd64d1762013-08-20 14:37:16 -07003698 }
3699
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003700 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003701 * Bind the items start-end from the list.
3702 *
3703 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003704 */
Winson Chung64359a52013-07-08 17:17:08 -07003705 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3706 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003707 Runnable r = new Runnable() {
3708 public void run() {
3709 bindItems(shortcuts, start, end, forceAnimateIcons);
3710 }
3711 };
3712 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003713 return;
3714 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003715
Winson Chungf0c6ae02012-03-21 16:10:31 -07003716 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003717 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3718 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003719 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003720 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003721 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003722 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003724
3725 // Short circuit if we are loading dock items for a configuration which has no dock
3726 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3727 mHotseat == null) {
3728 continue;
3729 }
3730
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 switch (item.itemType) {
3732 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3733 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003734 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003735 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003736
Winson Chung64359a52013-07-08 17:17:08 -07003737 /*
3738 * TODO: FIX collision case
3739 */
Winson Chungce376632013-07-11 16:12:41 -07003740 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3741 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3742 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3743 throw new RuntimeException("OCCUPIED");
3744 }
Winson Chung64359a52013-07-08 17:17:08 -07003745 }
3746
Adam Cohendcd297f2013-06-18 13:13:40 -07003747 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3748 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003749 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003750 // Animate all the applications up now
3751 shortcut.setAlpha(0f);
3752 shortcut.setScaleX(0f);
3753 shortcut.setScaleY(0f);
3754 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003755 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003756 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003757 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003758 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003759 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003760 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003761 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003762 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3763 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003764 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003765 default:
3766 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003768 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003769
Winson Chung997a9232013-07-24 15:33:46 -07003770 if (animateIcons) {
3771 // Animate to the correct page
3772 if (newShortcutsScreenId > -1) {
3773 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3774 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3775 if (newShortcutsScreenId != currentScreenId) {
3776 mWorkspace.snapToPage(newScreenIndex);
3777 }
3778 }
3779
Winson Chung64359a52013-07-08 17:17:08 -07003780 // We post the animation slightly delayed to prevent slowdowns when we are loading
3781 // right after we return to launcher.
3782 mWorkspace.postDelayed(new Runnable() {
3783 public void run() {
3784 anim.playTogether(bounceAnims);
3785 anim.start();
3786 }
3787 }, NEW_APPS_ANIMATION_DELAY);
3788 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003790 }
3791
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 /**
3793 * Implementation of the method from LauncherModel.Callbacks.
3794 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003795 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003796 Runnable r = new Runnable() {
3797 public void run() {
3798 bindFolders(folders);
3799 }
3800 };
3801 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003802 return;
3803 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003804 sFolders.clear();
3805 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003806 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807
3808 /**
3809 * Add the views for a widget to the workspace.
3810 *
3811 * Implementation of the method from LauncherModel.Callbacks.
3812 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003813 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003814 Runnable r = new Runnable() {
3815 public void run() {
3816 bindAppWidget(item);
3817 }
3818 };
3819 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003820 return;
3821 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003822
Daniel Sandler843e8602010-06-07 14:59:01 -04003823 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3824 if (DEBUG_WIDGETS) {
3825 Log.d(TAG, "bindAppWidget: " + item);
3826 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003827 final Workspace workspace = mWorkspace;
3828
3829 final int appWidgetId = item.appWidgetId;
3830 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003831 if (DEBUG_WIDGETS) {
3832 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3833 }
3834
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3836
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003838 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003839
Adam Cohendcd297f2013-06-18 13:13:40 -07003840 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003841 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003842 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3843
Joe Onorato9c1289c2009-08-17 11:03:03 -04003844 workspace.requestLayout();
3845
Daniel Sandler843e8602010-06-07 14:59:01 -04003846 if (DEBUG_WIDGETS) {
3847 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3848 + (SystemClock.uptimeMillis()-start) + "ms");
3849 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850 }
3851
Adam Cohen1462de32012-07-24 22:34:36 -07003852 public void onPageBoundSynchronously(int page) {
3853 mSynchronouslyBoundPages.add(page);
3854 }
3855
Joe Onorato9c1289c2009-08-17 11:03:03 -04003856 /**
3857 * Callback saying that there aren't any more items to bind.
3858 *
3859 * Implementation of the method from LauncherModel.Callbacks.
3860 */
Adam Cohene25af792013-06-06 23:08:25 -07003861 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003862 Runnable r = new Runnable() {
3863 public void run() {
3864 finishBindingItems(upgradePath);
3865 }
3866 };
3867 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003868 return;
3869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870 if (mSavedState != null) {
3871 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003872 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003874 mSavedState = null;
3875 }
3876
Adam Cohen1462de32012-07-24 22:34:36 -07003877 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003878
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003879 // If we received the result of any pending adds while the loader was running (e.g. the
3880 // widget configuration forced an orientation change), process them now.
3881 for (int i = 0; i < sPendingAddList.size(); i++) {
3882 completeAdd(sPendingAddList.get(i));
3883 }
3884 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003885
Winson Chungc51db6a2011-10-05 11:44:49 -07003886 // Update the market app icon as necessary (the other icons will be managed in response to
3887 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003888 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003889
Winson Chungf0c6ae02012-03-21 16:10:31 -07003890 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003891 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003892 mWorkspace.getUniqueComponents(true, null);
3893 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003894 }
Adam Cohen99894d92013-06-14 11:22:59 -07003895
Adam Cohen66a01fd2013-06-11 12:48:00 -07003896 mWorkspace.post(new Runnable() {
3897 @Override
3898 public void run() {
3899 onFinishBindingItems();
3900 }
3901 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003902
3903 // Write all the logs to disk
3904 Launcher.addDumpLog(TAG, "10249126 - finishBindingItems() - dumping logs to disk", true);
3905 dumpLogsToLocalData(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003906 }
3907
Winson Chunga2413752012-04-03 14:22:34 -07003908 private boolean canRunNewAppsAnimation() {
3909 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3910 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3911 }
3912
Winson Chungc9168342013-06-26 14:54:55 -07003913 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3914 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3915 PropertyValuesHolder.ofFloat("alpha", 1f),
3916 PropertyValuesHolder.ofFloat("scaleX", 1f),
3917 PropertyValuesHolder.ofFloat("scaleY", 1f));
3918 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3919 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3920 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3921 return bounceAnim;
3922 }
3923
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003924 @Override
3925 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003926 boolean searchVisible = updateGlobalSearchIcon();
3927 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003928 if (mSearchDropTargetBar != null) {
3929 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3930 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003931 }
3932
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003933 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003934 * Add the icons for all apps.
3935 *
3936 * Implementation of the method from LauncherModel.Callbacks.
3937 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003938 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003939 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3940 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3941 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3942 getHotseat().addAllAppsFolder(mIconCache, apps,
3943 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3944 }
3945 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003946 }
Winson Chungc58497e2013-09-03 17:48:37 -07003947 } else {
3948 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3949 mAppsCustomizeContent != null) {
3950 mAppsCustomizeContent.setApps(apps);
3951 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003953 }
3954
Bjorn Bringert85f418d2013-09-06 12:50:05 +01003955 @Override
3956 public boolean shouldShowApp(ResolveInfo app) {
3957 return true;
3958 }
3959
Joe Onorato9c1289c2009-08-17 11:03:03 -04003960 /**
3961 * A package was updated.
3962 *
3963 * Implementation of the method from LauncherModel.Callbacks.
3964 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003965 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003966 Runnable r = new Runnable() {
3967 public void run() {
3968 bindAppsUpdated(apps);
3969 }
3970 };
3971 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003972 return;
3973 }
3974
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003975 if (mWorkspace != null) {
3976 mWorkspace.updateShortcuts(apps);
3977 }
Winson Chungc58497e2013-09-03 17:48:37 -07003978
3979 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3980 mAppsCustomizeContent != null) {
3981 mAppsCustomizeContent.updateApps(apps);
3982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003983 }
3984
3985 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003986 * A package was uninstalled. We take both the super set of packageNames
3987 * in addition to specific applications to remove, the reason being that
3988 * this can be called when a package is updated as well. In that scenario,
3989 * we only remove specific components from the workspace, where as
3990 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003991 *
3992 * Implementation of the method from LauncherModel.Callbacks.
3993 */
Winson Chung83892cc2013-05-01 16:53:33 -07003994 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003995 final ArrayList<AppInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003996 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003997 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003998 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003999 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07004000 }
Winson Chungd64d1762013-08-20 14:37:16 -07004001 };
4002 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004003 return;
4004 }
4005
Winson Chung64359a52013-07-08 17:17:08 -07004006 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07004007 mWorkspace.removeItemsByPackageName(packageNames);
4008 } else {
4009 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004010 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004011
Winson Chunga1820962011-10-03 16:31:06 -07004012 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07004013 mDragController.onAppsRemoved(appInfos, this);
Winson Chungc58497e2013-09-03 17:48:37 -07004014
4015 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4016 mAppsCustomizeContent != null) {
4017 mAppsCustomizeContent.removeApps(appInfos);
4018 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004019 }
Joe Onoratobe386092009-11-17 17:32:16 -08004020
4021 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004022 * A number of packages were updated.
4023 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004024 private ArrayList<Object> mWidgetsAndShortcuts;
4025 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004026 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004027 bindPackagesUpdated(mWidgetsAndShortcuts);
4028 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004029 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004030 };
4031
4032 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4033 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4034 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004035 return;
4036 }
4037
Winson Chung64359a52013-07-08 17:17:08 -07004038 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004039 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4040 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004041 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004042 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004043 }
4044
Winson Chung400438b2011-01-16 17:53:48 -08004045 private int mapConfigurationOriActivityInfoOri(int configOri) {
4046 final Display d = getWindowManager().getDefaultDisplay();
4047 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4048 switch (d.getRotation()) {
4049 case Surface.ROTATION_0:
4050 case Surface.ROTATION_180:
4051 // We are currently in the same basic orientation as the natural orientation
4052 naturalOri = configOri;
4053 break;
4054 case Surface.ROTATION_90:
4055 case Surface.ROTATION_270:
4056 // We are currently in the other basic orientation to the natural orientation
4057 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4058 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4059 break;
4060 }
4061
4062 int[] oriMap = {
4063 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4064 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4065 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4066 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4067 };
4068 // Since the map starts at portrait, we need to offset if this device's natural orientation
4069 // is landscape.
4070 int indexOffset = 0;
4071 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4072 indexOffset = 1;
4073 }
4074 return oriMap[(d.getRotation() + indexOffset) % 4];
4075 }
Adam Cohen446e9402011-09-15 18:21:21 -07004076
Winson Chung4b919f82012-05-01 10:44:08 -07004077 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004078 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004079 getResources().getBoolean(R.bool.allow_rotation);
4080 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004081 }
Winson Chung4b919f82012-05-01 10:44:08 -07004082 public void lockScreenOrientation() {
4083 if (isRotationEnabled()) {
4084 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4085 .getConfiguration().orientation));
4086 }
4087 }
4088 public void unlockScreenOrientation(boolean immediate) {
4089 if (isRotationEnabled()) {
4090 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004091 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004092 } else {
4093 mHandler.postDelayed(new Runnable() {
4094 public void run() {
4095 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4096 }
4097 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004098 }
Winson Chung4b919f82012-05-01 10:44:08 -07004099 }
Winson Chung400438b2011-01-16 17:53:48 -08004100 }
4101
Winson Chung82f55532011-08-09 14:14:23 -07004102 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004103 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004104 if (DISABLE_CLINGS) {
4105 return false;
4106 }
4107
Brett Chabot2a9e2282011-08-23 15:03:13 -07004108 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004109 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004110
Michael Jurkae233a8b2013-03-19 13:49:20 +01004111 // Restricted secondary users (child mode) will potentially have very few apps
4112 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004113// boolean supportsLimitedUsers =
4114// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4115// Account[] accounts = AccountManager.get(this).getAccounts();
4116// if (supportsLimitedUsers && accounts.length == 0) {
4117// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4118// Bundle restrictions = um.getUserRestrictions();
4119// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4120// return false;
4121// }
4122// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004123 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004124 }
Michael Jurka22143132012-10-04 17:42:38 +02004125
Winson Chung7d7541e2011-09-16 20:14:36 -07004126 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004127 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004128 if (cling != null) {
4129 cling.init(this, positionData);
4130 cling.setVisibility(View.VISIBLE);
4131 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4132 if (animate) {
4133 cling.buildLayer();
4134 cling.setAlpha(0f);
4135 cling.animate()
4136 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004137 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004138 .setDuration(SHOW_CLING_DURATION)
4139 .setStartDelay(delay)
4140 .start();
4141 } else {
4142 cling.setAlpha(1f);
4143 }
Michael Jurka22143132012-10-04 17:42:38 +02004144 cling.setFocusableInTouchMode(true);
4145 cling.post(new Runnable() {
4146 public void run() {
4147 cling.setFocusable(true);
4148 cling.requestFocus();
4149 }
4150 });
4151 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4152 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004153 }
4154 return cling;
4155 }
Michael Jurka22143132012-10-04 17:42:38 +02004156
Winson Chung7d7541e2011-09-16 20:14:36 -07004157 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004158 // To catch cases where siblings of top-level views are made invisible, just check whether
4159 // the cling is directly set to GONE before dismissing it.
4160 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004161 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004162 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004163 anim.addListener(new AnimatorListenerAdapter() {
4164 public void onAnimationEnd(Animator animation) {
4165 cling.setVisibility(View.GONE);
4166 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004167 // We should update the shared preferences on a background thread
4168 new Thread("dismissClingThread") {
4169 public void run() {
4170 SharedPreferences.Editor editor = mSharedPrefs.edit();
4171 editor.putBoolean(flag, true);
4172 editor.commit();
4173 }
4174 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004175 };
4176 });
4177 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004178 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004179 }
4180 }
Michael Jurka22143132012-10-04 17:42:38 +02004181
Winson Chung9d9d74f2011-09-19 11:49:12 -07004182 private void removeCling(int id) {
4183 final View cling = findViewById(id);
4184 if (cling != null) {
4185 final ViewGroup parent = (ViewGroup) cling.getParent();
4186 parent.post(new Runnable() {
4187 @Override
4188 public void run() {
4189 parent.removeView(cling);
4190 }
4191 });
Michael Jurka22143132012-10-04 17:42:38 +02004192 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004193 }
4194 }
Michael Jurka974c3862012-05-22 22:00:31 -07004195
4196 private boolean skipCustomClingIfNoAccounts() {
4197 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4198 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4199 if (customCling) {
4200 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004201 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004202 Account[] accounts = am.getAccountsByType("com.google");
4203 return accounts.length == 0;
4204 }
4205 return false;
4206 }
4207
Winson Chung7d7541e2011-09-16 20:14:36 -07004208 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004209 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004210 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004211 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4212 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004213 // If we're not using the default workspace layout, replace workspace cling
4214 // with a custom workspace cling (usually specified in an overlay)
4215 // For now, only do this on tablets
4216 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004217 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004218 // Use a custom cling
4219 View cling = findViewById(R.id.workspace_cling);
4220 ViewGroup clingParent = (ViewGroup) cling.getParent();
4221 int clingIndex = clingParent.indexOfChild(cling);
4222 clingParent.removeViewAt(clingIndex);
4223 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4224 clingParent.addView(customCling, clingIndex);
4225 customCling.setId(R.id.workspace_cling);
4226 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004227 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004228 } else {
4229 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004230 }
4231 }
4232 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004233 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004234 if (isClingsEnabled() &&
4235 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004236 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004237 } else {
4238 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004239 }
4240 }
4241 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004242 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004243 if (isClingsEnabled() &&
4244 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4245 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004246 } else {
4247 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004248 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004249 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004250 }
Michael Jurka104c4562013-07-08 18:03:46 -07004251 protected SharedPreferences getSharedPrefs() {
4252 return mSharedPrefs;
4253 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004254 public boolean isFolderClingVisible() {
4255 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004256 if (cling != null) {
4257 return cling.getVisibility() == View.VISIBLE;
4258 }
4259 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004260 }
Winson Chung82f55532011-08-09 14:14:23 -07004261 public void dismissWorkspaceCling(View v) {
4262 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004263 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004264 }
4265 public void dismissAllAppsCling(View v) {
4266 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004267 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4268 }
4269 public void dismissFolderCling(View v) {
4270 Cling cling = (Cling) findViewById(R.id.folder_cling);
4271 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004272 }
4273
Winson Chung80baf5a2010-08-09 16:03:15 -07004274 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004275 * Prints out out state for debugging.
4276 */
4277 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004278 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004279 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004280 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4281 Log.d(TAG, "mRestoring=" + mRestoring);
4282 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4283 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004284 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004285 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004286
Winson Chung785d2eb2011-04-14 16:08:02 -07004287 if (mAppsCustomizeContent != null) {
4288 mAppsCustomizeContent.dumpState();
4289 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004290 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004291 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004292
4293 @Override
4294 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4295 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004296 synchronized (sDumpLogs) {
4297 writer.println(" ");
4298 writer.println("Debug logs: ");
4299 for (int i = 0; i < sDumpLogs.size(); i++) {
4300 writer.println(" " + sDumpLogs.get(i));
4301 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004302 }
4303 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004304
4305 public static void dumpDebugLogsToConsole() {
Adam Cohen4caf2982013-08-20 18:54:31 -07004306 synchronized (sDumpLogs) {
4307 Log.d(TAG, "");
4308 Log.d(TAG, "*********************");
4309 Log.d(TAG, "Launcher debug logs: ");
4310 for (int i = 0; i < sDumpLogs.size(); i++) {
4311 Log.d(TAG, " " + sDumpLogs.get(i));
4312 }
4313 Log.d(TAG, "*********************");
4314 Log.d(TAG, "");
Adam Cohen487f7dd2012-06-28 18:12:10 -07004315 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004316 }
4317
4318 public static void addDumpLog(String tag, String log, boolean debugLog) {
4319 if (debugLog) {
4320 Log.d(tag, log);
4321 }
4322 sDateStamp.setTime(System.currentTimeMillis());
4323 synchronized (sDumpLogs) {
4324 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
4325 }
4326 }
4327
4328 public void dumpLogsToLocalData(final boolean email) {
4329 new Thread("DumpLogsToLocalData") {
4330 @Override
4331 public void run() {
4332 boolean success = false;
4333 sDateStamp.setTime(sRunStart);
4334 String FILENAME = sDateStamp.getMonth() + "-"
4335 + sDateStamp.getDay() + "_"
4336 + sDateStamp.getHours() + "-"
4337 + sDateStamp.getMinutes() + "_"
4338 + sDateStamp.getSeconds() + ".txt";
4339
4340 FileOutputStream fos = null;
4341 File outFile = null;
4342 try {
4343 outFile = new File(getFilesDir(), FILENAME);
4344 outFile.createNewFile();
4345 fos = new FileOutputStream(outFile);
4346 } catch (Exception e) {
4347 e.printStackTrace();
4348 }
4349 if (fos != null) {
4350 PrintWriter writer = new PrintWriter(fos);
4351
4352 writer.println(" ");
4353 writer.println("Debug logs: ");
4354 synchronized (sDumpLogs) {
4355 for (int i = 0; i < sDumpLogs.size(); i++) {
4356 writer.println(" " + sDumpLogs.get(i));
4357 }
4358 }
4359 writer.close();
4360 }
4361 try {
4362 if (fos != null) {
4363 fos.close();
4364 success = true;
4365 }
4366 } catch (IOException e) {
4367 e.printStackTrace();
4368 }
4369
4370 if (success && email) {
4371 if (!emailSent()) {
4372 emailFile(outFile);
4373 }
4374 }
4375 }
4376 }.start();
4377 }
4378
4379 private void emailFile(File file) {
4380 File publicCopy = new File(Environment.getExternalStorageDirectory(), file.getName());
4381 try {
4382 copyFile(file, publicCopy);
4383 } catch (IOException e) {
4384 e.printStackTrace();
4385 return;
4386 }
4387
4388 Intent intent = new Intent(Intent.ACTION_SEND);
4389 intent.setType("text/plain");
4390 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"adamcohen@google.com, winsonc@google.com," +
4391 "mikejurka@google"});
4392 intent.putExtra(Intent.EXTRA_SUBJECT, "Data corruption " + file.getName());
4393 intent.putExtra(Intent.EXTRA_TEXT, "Data corruption has occurred, logs attached");
4394
4395 if (!file.exists() || !file.canRead()) {
4396 Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
4397 finish();
4398 return;
4399 }
4400
4401 Toast.makeText(this, "Data corruption has occurred, please send e-mail", Toast.LENGTH_LONG);
4402 Uri uri = Uri.fromFile(publicCopy);
4403 intent.putExtra(Intent.EXTRA_STREAM, uri);
4404 startActivity(Intent.createChooser(intent, "Please send logs, consider clearing data"));
4405
4406 setEmailSent(true);
4407 }
4408
4409 public void copyFile(File src, File dst) throws IOException {
4410 InputStream in = new FileInputStream(src);
4411 OutputStream out = new FileOutputStream(dst);
4412
4413 // Transfer bytes from in to out
4414 byte[] buf = new byte[1024];
4415 int len;
4416 while ((len = in.read(buf)) > 0) {
4417 out.write(buf, 0, len);
4418 }
4419 in.close();
4420 out.close();
Adam Cohen487f7dd2012-06-28 18:12:10 -07004421 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004422}
Michael Jurka2763be32011-02-24 11:19:57 -08004423
Michael Jurkaabded662011-03-04 12:06:57 -08004424interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004425 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004426 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004427 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004428 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004429 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004430}