blob: 5aa200a23ca1ebcd94ee7b3734bb9a10b0d7ea99 [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 Sandler6053b8002013-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
Dave Hawkeya8881582013-09-17 15:55:33 -0600485 /**
486 * To be overridden by subclasses to create the custom content and call
487 * {@link #addToCustomContentPage}. This will only be invoked if
488 * {@link #hasCustomContentToLeft()} is {@code true}.
489 */
490 protected void addCustomContentToLeft() {
491 }
492
493 /**
494 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
495 * ensure the custom content page is added or removed if necessary.
496 */
497 protected void invalidateHasCustomContentToLeft() {
498 if (mWorkspace.getScreenOrder().isEmpty()) {
499 // Not bound yet, wait for bindScreens to be called.
500 return;
501 }
502
503 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
504 // Create the custom content page and call the subclass to populate it.
505 mWorkspace.createCustomContentPage();
506 addCustomContentToLeft();
507 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
508 mWorkspace.removeCustomContentPage();
509 }
510 }
511
Adam Cohenf9426d52012-06-04 17:26:21 -0700512 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700513 boolean searchVisible = false;
514 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800515 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700516 int coi = getCurrentOrientationIndexForGlobalIcons();
517 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
518 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700519 updateAppMarketIcon();
520 searchVisible = updateGlobalSearchIcon();
521 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700522 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700523 if (sGlobalSearchIcon[coi] != null) {
524 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700525 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700526 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700527 if (sVoiceSearchIcon[coi] != null) {
528 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700529 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700530 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700531 if (sAppMarketIcon[coi] != null) {
532 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800533 }
Winson Chungadf0c182012-08-23 14:59:07 -0700534 if (mSearchDropTargetBar != null) {
535 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 }
Romain Guycbb89e42009-06-08 15:52:54 -0700538
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700540 if (sLocaleConfiguration == null) {
541 new AsyncTask<Void, Void, LocaleConfiguration>() {
542 @Override
543 protected LocaleConfiguration doInBackground(Void... unused) {
544 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
545 readConfiguration(Launcher.this, localeConfiguration);
546 return localeConfiguration;
547 }
548
549 @Override
550 protected void onPostExecute(LocaleConfiguration result) {
551 sLocaleConfiguration = result;
552 checkForLocaleChange(); // recursive, but now with a locale configuration
553 }
554 }.execute();
555 return;
556 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 final Configuration configuration = getResources().getConfiguration();
559
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700560 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 final String locale = configuration.locale.toString();
562
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700563 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 final int mcc = configuration.mcc;
565
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700566 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 final int mnc = configuration.mnc;
568
Romain Guy84f296c2009-11-04 15:00:44 -0800569 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570
Romain Guy84f296c2009-11-04 15:00:44 -0800571 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700572 sLocaleConfiguration.locale = locale;
573 sLocaleConfiguration.mcc = mcc;
574 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700575
Joe Onorato0589f0f2010-02-08 13:44:00 -0800576 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700577
578 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
579 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700580 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581 public void run() {
582 writeConfiguration(Launcher.this, localeConfiguration);
583 }
584 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700585 }
586 }
587
588 private static class LocaleConfiguration {
589 public String locale;
590 public int mcc = -1;
591 public int mnc = -1;
592 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700593
Romain Guy98d01652009-06-30 16:21:04 -0700594 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
595 DataInputStream in = null;
596 try {
597 in = new DataInputStream(context.openFileInput(PREFERENCES));
598 configuration.locale = in.readUTF();
599 configuration.mcc = in.readInt();
600 configuration.mnc = in.readInt();
601 } catch (FileNotFoundException e) {
602 // Ignore
603 } catch (IOException e) {
604 // Ignore
605 } finally {
606 if (in != null) {
607 try {
608 in.close();
609 } catch (IOException e) {
610 // Ignore
611 }
612 }
613 }
614 }
615
616 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
617 DataOutputStream out = null;
618 try {
619 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
620 out.writeUTF(configuration.locale);
621 out.writeInt(configuration.mcc);
622 out.writeInt(configuration.mnc);
623 out.flush();
624 } catch (FileNotFoundException e) {
625 // Ignore
626 } catch (IOException e) {
627 //noinspection ResultOfMethodCallIgnored
628 context.getFileStreamPath(PREFERENCES).delete();
629 } finally {
630 if (out != null) {
631 try {
632 out.close();
633 } catch (IOException e) {
634 // Ignore
635 }
636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
638 }
639
Bjorn Bringertc459e522013-06-07 19:36:01 +0100640 public LayoutInflater getInflater() {
641 return mInflater;
642 }
643
Adam Cohen716b51e2011-06-30 12:09:54 -0700644 public DragLayer getDragLayer() {
645 return mDragLayer;
646 }
647
Winson Chung36a62fe2012-05-06 18:04:42 -0700648 boolean isDraggingEnabled() {
649 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
650 // that is subsequently removed from the workspace in startBinding().
651 return !mModel.isLoadingWorkspace();
652 }
653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 static int getScreen() {
655 synchronized (sLock) {
656 return sScreen;
657 }
658 }
659
660 static void setScreen(int screen) {
661 synchronized (sLock) {
662 sScreen = screen;
663 }
664 }
665
Winson Chung557d6ed2011-07-08 15:34:52 -0700666 /**
667 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
668 * a configuration step, this allows the proper animations to run after other transitions.
669 */
670 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700671 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800672 switch (args.requestCode) {
673 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700674 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700675 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800676 break;
677 case REQUEST_PICK_SHORTCUT:
678 processShortcut(args.intent);
679 break;
680 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700682 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700683 result = true;
684 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 case REQUEST_CREATE_APPWIDGET:
686 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700687 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700688 result = true;
689 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800690 }
Michael Jurka27614d22012-04-02 06:40:22 -0700691 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
692 // if you turned the screen off and then back while in All Apps, Launcher would not
693 // return to the workspace. Clearing mAddInfo.container here fixes this issue
694 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700695 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800696 }
697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700699 protected void onActivityResult(
700 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700701 // Reset the startActivity waiting flag
702 mWaitingForResult = false;
703
Michael Jurka8b805b12012-04-18 14:23:14 -0700704 if (requestCode == REQUEST_BIND_APPWIDGET) {
705 int appWidgetId = data != null ?
706 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
707 if (resultCode == RESULT_CANCELED) {
708 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
709 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700710 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700711 }
712 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200713 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
714 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
715 mWorkspace.exitOverviewMode(false);
716 }
717 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200719
Winson Chung557d6ed2011-07-08 15:34:52 -0700720 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800721 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
722 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700723
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 // We have special handling for widgets
725 if (isWidgetDrop) {
726 int appWidgetId = data != null ?
727 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700728 if (appWidgetId < 0) {
729 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
730 "widget configuration activity.");
731 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
732 } else {
733 completeTwoStageWidgetDrop(resultCode, appWidgetId);
734 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 return;
736 }
737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 // The pattern used here is that a user PICKs a specific application,
739 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700740
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
742 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700743 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 final PendingAddArguments args = new PendingAddArguments();
745 args.requestCode = requestCode;
746 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700747 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700748 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700749 args.cellX = mPendingAddInfo.cellX;
750 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800751 if (isWorkspaceLocked()) {
752 sPendingAddList.add(args);
753 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700754 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800757 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700758 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
760 null);
761 }
762
763 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700764 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700765 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800766 Runnable onCompleteRunnable = null;
767 int animationType = 0;
768
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700769 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800770 if (resultCode == RESULT_OK) {
771 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
772 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700773 mPendingAddWidgetInfo);
774 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 onCompleteRunnable = new Runnable() {
776 @Override
777 public void run() {
778 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700779 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800780 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
781 null);
782 }
783 };
784 } else if (resultCode == RESULT_CANCELED) {
785 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
786 onCompleteRunnable = new Runnable() {
787 @Override
788 public void run() {
789 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
790 null);
791 }
792 };
793 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700794 if (mDragLayer.getAnimatedView() != null) {
795 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
796 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
797 animationType, boundWidget, true);
798 } else {
799 // The animated view may be null in the case of a rotation during widget configuration
800 onCompleteRunnable.run();
801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 }
803
804 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700805 protected void onStop() {
806 super.onStop();
807 FirstFrameAnimatorHelper.setIsVisible(false);
808 }
809
810 @Override
811 protected void onStart() {
812 super.onStart();
813 FirstFrameAnimatorHelper.setIsVisible(true);
814 }
815
816 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200818 long startTime = 0;
819 if (DEBUG_RESUME_TIME) {
820 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400821 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700824
Winson Chung4a2afa32012-07-19 14:53:05 -0700825 // Restore the previous launcher state
826 if (mOnResumeState == State.WORKSPACE) {
827 showWorkspace(false);
828 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chungc58497e2013-09-03 17:48:37 -0700829 showAllApps(false, AppsCustomizePagedView.ContentType.Applications);
Winson Chung4a2afa32012-07-19 14:53:05 -0700830 }
831 mOnResumeState = State.NONE;
832
Craig Mautner360310b2012-10-26 15:13:08 -0700833 // Background was set to gradient in onPause(), restore to black if in all apps.
834 setWorkspaceBackground(mState == State.WORKSPACE);
835
Winson Chungde0fb8f2012-05-08 14:37:08 -0700836 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700837 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700838
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800839 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700840 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700841 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700843 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700845 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700847 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200848 // We might have postponed some bind calls until onResume (see waitUntilResume) --
849 // execute them here
850 long startTimeCallbacks = 0;
851 if (DEBUG_RESUME_TIME) {
852 startTimeCallbacks = System.currentTimeMillis();
853 }
854
855 if (mAppsCustomizeContent != null) {
856 mAppsCustomizeContent.setBulkBind(true);
857 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700858 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
859 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200860 }
861 if (mAppsCustomizeContent != null) {
862 mAppsCustomizeContent.setBulkBind(false);
863 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700864 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200865 if (DEBUG_RESUME_TIME) {
866 Log.d(TAG, "Time spent processing callbacks in onResume: " +
867 (System.currentTimeMillis() - startTimeCallbacks));
868 }
Michael Jurka447bf842013-05-15 14:52:15 +0200869 }
Michael Jurka54554252013-08-01 12:52:23 +0200870 if (mOnResumeCallbacks.size() > 0) {
871 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
872 mOnResumeCallbacks.get(i).run();
873 }
874 mOnResumeCallbacks.clear();
875 }
Winson Chunge4e50662012-01-23 14:45:13 -0800876
877 // Reset the pressed state of icons that were locked in the press state while activities
878 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800879 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800880 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800881 mWaitingForResume.setStayPressed(false);
882 }
Winson Chunge4e50662012-01-23 14:45:13 -0800883 if (mAppsCustomizeContent != null) {
884 // Resets the previous all apps icon press state
885 mAppsCustomizeContent.resetDrawableState();
886 }
Adam Cohen06dff352012-06-01 17:17:08 -0700887 // It is possible that widgets can receive updates while launcher is not in the foreground.
888 // Consequently, the widgets will be inflated in the orientation of the foreground activity
889 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
890 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700891 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700892
893 // Again, as with the above scenario, it's possible that one or more of the global icons
894 // were updated in the wrong orientation.
895 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200896 if (DEBUG_RESUME_TIME) {
897 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
898 }
Adam Cohen4caf2982013-08-20 18:54:31 -0700899
900 // Write all the logs to disk
901 Launcher.addDumpLog(TAG, "10249126 - onResume() - dumping logs to disk", true);
902 dumpLogsToLocalData(false);
Adam Cohen06dff352012-06-01 17:17:08 -0700903 }
904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700906 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700907 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
908 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
909 // when Launcher resumes and we are still in AllApps.
910 updateWallpaperVisibility(true);
911
Winson Chung997a9232013-07-24 15:33:46 -0700912 // Ensure that items added to Launcher are queued until Launcher returns
913 InstallShortcutReceiver.enableInstallQueue();
914
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700915 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700916 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800917 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700918 mDragController.resetLastGestureUpTime();
Adam Cohen4caf2982013-08-20 18:54:31 -0700919
920 // Write all the logs to disk
921 Launcher.addDumpLog(TAG, "10249126 - onPause() - dumping logs to disk", true);
922 dumpLogsToLocalData(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700923 }
Romain Guycbb89e42009-06-08 15:52:54 -0700924
Adam Cohen66a01fd2013-06-11 12:48:00 -0700925 protected void onFinishBindingItems() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600926 if (hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
927 addCustomContentToLeft();
928 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700929 }
930
Adam Cohenbffe7452013-07-22 18:21:45 -0700931 QSBScroller mQsbScroller = new QSBScroller() {
932 int scrollY = 0;
933
934 @Override
935 public void setScrollY(int scroll) {
936 scrollY = scroll;
937
938 if (mWorkspace.isOnOrMovingToCustomContent()) {
939 mSearchDropTargetBar.setTranslationY(- scrollY);
940 }
941 }
942 };
943
944 public void resetQSBScroll() {
945 mSearchDropTargetBar.animate().translationY(0).start();
946 }
947
948 public interface CustomContentCallbacks {
949 // Custom content is completely shown
950 public void onShow();
951
952 // Custom content is completely hidden
953 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700954
955 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
956 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700957 }
958
Adam Cohen30bacb22013-08-29 14:25:25 -0700959 protected void startSettings() {
960 }
961
Adam Cohenbffe7452013-07-22 18:21:45 -0700962 public interface QSBScroller {
963 public void setScrollY(int scrollY);
964 }
965
Adam Cohen66a01fd2013-06-11 12:48:00 -0700966 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700967 public QSBScroller addToCustomContentPage(View customContent) {
968 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700969 }
970
Winson Chung98ca0f72013-07-29 12:58:51 -0700971 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700972 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700973 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700974 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700975 }
976
Adam Cohenedb40762013-07-18 16:45:45 -0700977 // The custom content needs to offset its content to account for the QSB
978 public int getTopOffsetForCustomContent() {
979 return mWorkspace.getPaddingTop();
980 }
981
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700982 @Override
983 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984 // Flag the loader to stop early before switching
985 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700986 if (mAppsCustomizeContent != null) {
987 mAppsCustomizeContent.surrender();
988 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800989 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700990 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700991
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800992 // We can't hide the IME if it was forced open. So don't bother
993 /*
994 @Override
995 public void onWindowFocusChanged(boolean hasFocus) {
996 super.onWindowFocusChanged(hasFocus);
997
998 if (hasFocus) {
999 final InputMethodManager inputManager = (InputMethodManager)
1000 getSystemService(Context.INPUT_METHOD_SERVICE);
1001 WindowManager.LayoutParams lp = getWindow().getAttributes();
1002 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
1003 android.os.Handler()) {
1004 protected void onReceiveResult(int resultCode, Bundle resultData) {
1005 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
1006 }
1007 });
1008 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
1009 }
1010 }
1011 */
1012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 private boolean acceptFilter() {
1014 final InputMethodManager inputManager = (InputMethodManager)
1015 getSystemService(Context.INPUT_METHOD_SERVICE);
1016 return !inputManager.isFullscreenMode();
1017 }
1018
1019 @Override
1020 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001021 final int uniChar = event.getUnicodeChar();
1022 final boolean handled = super.onKeyDown(keyCode, event);
1023 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1024 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1026 keyCode, event);
1027 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001028 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001029 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001030 // showSearchDialog()
1031 // If there are multiple keystrokes before the search dialog takes focus,
1032 // onSearchRequested() will be called for every keystroke,
1033 // but it is idempotent, so it's fine.
1034 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
1036 }
1037
Joe Onorato8a9625e2010-01-28 15:55:35 -08001038 // Eat the long press event so the keyboard doesn't come up.
1039 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1040 return true;
1041 }
1042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 return handled;
1044 }
1045
Karl Rosaen138a0412009-04-23 19:00:21 -07001046 private String getTypedText() {
1047 return mDefaultKeySsb.toString();
1048 }
1049
1050 private void clearTypedText() {
1051 mDefaultKeySsb.clear();
1052 mDefaultKeySsb.clearSpans();
1053 Selection.setSelection(mDefaultKeySsb, 0);
1054 }
1055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001057 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1058 * State
1059 */
1060 private static State intToState(int stateOrdinal) {
1061 State state = State.WORKSPACE;
1062 final State[] stateValues = State.values();
1063 for (int i = 0; i < stateValues.length; i++) {
1064 if (stateValues[i].ordinal() == stateOrdinal) {
1065 state = stateValues[i];
1066 break;
1067 }
1068 }
1069 return state;
1070 }
1071
1072 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 * Restores the previous state, if it exists.
1074 *
1075 * @param savedState The previous state.
1076 */
1077 private void restoreState(Bundle savedState) {
1078 if (savedState == null) {
1079 return;
1080 }
1081
Michael Jurka883f55b2010-10-21 15:47:14 -07001082 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001083 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001084 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001085 }
1086
Winson Chungf0c6ae02012-03-21 16:10:31 -07001087 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001089 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
1091
Winson Chung3d503fb2011-07-13 17:25:49 -07001092 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001093 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001094
Winson Chung3d503fb2011-07-13 17:25:49 -07001095 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1096 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001097 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001098 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1099 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001100 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1101 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1102 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001103 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 mRestoring = true;
1105 }
1106
1107 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1108 if (renameFolder) {
1109 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001110 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 mRestoring = true;
1112 }
Winson Chunga12a2502010-12-20 14:41:35 -08001113
Winson Chung785d2eb2011-04-14 16:08:02 -07001114 // Restore the AppsCustomize tab
1115 if (mAppsCustomizeTabHost != null) {
1116 String curTab = savedState.getString("apps_customize_currentTab");
1117 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001118 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001119 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001120 mAppsCustomizeContent.loadAssociatedPages(
1121 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001122 }
1123
Winson Chung5afbf7b2011-07-25 11:53:08 -07001124 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1125 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
1128
1129 /**
1130 * Finds all the views we need and configure them properly.
1131 */
1132 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001133 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001134
Craig Mautner360310b2012-10-26 15:13:08 -07001135 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001136 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1137 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001138
John Spurlock77e1f472013-09-11 10:09:51 -04001139 mLauncherView.setSystemUiVisibility(
1140 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001141 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142
Winson Chung4c98d922011-05-31 16:50:48 -07001143 // Setup the drag layer
1144 mDragLayer.setup(this, dragController);
1145
Winson Chung3d503fb2011-07-13 17:25:49 -07001146 // Setup the hotseat
1147 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1148 if (mHotseat != null) {
1149 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001150 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001151 }
1152
Adam Cohenf358a4b2013-07-23 16:47:31 -07001153 mOverviewPanel = findViewById(R.id.overview_panel);
1154 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1155 @Override
1156 public void onClick(View arg0) {
Winson Chungc58497e2013-09-03 17:48:37 -07001157 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001158 }
1159 });
1160 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1161 @Override
1162 public void onClick(View arg0) {
1163 startWallpaper();
1164 }
1165 });
Adam Cohen30bacb22013-08-29 14:25:25 -07001166 findViewById(R.id.settings_button).setOnClickListener(new OnClickListener() {
1167 @Override
1168 public void onClick(View arg0) {
1169 startSettings();
1170 }
1171 });
Adam Cohenf358a4b2013-07-23 16:47:31 -07001172
Winson Chung4c98d922011-05-31 16:50:48 -07001173 // Setup the workspace
1174 mWorkspace.setHapticFeedbackEnabled(false);
1175 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001176 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001177 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001178
Winson Chungf0ea4d32011-06-06 14:27:16 -07001179 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001180 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001181
Winson Chungf0ea4d32011-06-06 14:27:16 -07001182 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001183 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001184 mAppsCustomizeContent = (AppsCustomizePagedView)
1185 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1186 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001187
Winson Chung3d503fb2011-07-13 17:25:49 -07001188 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001189 dragController.setDragScoller(mWorkspace);
1190 dragController.setScrollView(mDragLayer);
1191 dragController.setMoveTarget(mWorkspace);
1192 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001193 if (mSearchDropTargetBar != null) {
1194 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001195 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001196
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001197 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001198 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001199 mWeightWatcher = new WeightWatcher(this);
1200 mWeightWatcher.setAlpha(0.5f);
1201 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001202 new FrameLayout.LayoutParams(
1203 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001204 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001205 Gravity.BOTTOM)
1206 );
Adam Cohen39a06042013-07-19 14:30:12 -07001207
1208 boolean show = shouldShowWeightWatcher();
1209 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
1212
1213 /**
1214 * Creates a view representing a shortcut.
1215 *
1216 * @param info The data structure describing the shortcut.
1217 *
1218 * @return A View inflated from R.layout.application.
1219 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001220 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001222 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
1224
1225 /**
1226 * Creates a view representing a shortcut inflated from the specified resource.
1227 *
1228 * @param layoutResId The id of the XML layout used to create the shortcut.
1229 * @param parent The group the shortcut belongs to.
1230 * @param info The data structure describing the shortcut.
1231 *
1232 * @return A View inflated from layoutResId.
1233 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001234 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001235 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1236 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 return favorite;
1239 }
1240
1241 /**
1242 * Add an application shortcut to the workspace.
1243 *
1244 * @param data The intent describing the application.
1245 * @param cellInfo The position on screen where to create the shortcut.
1246 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001247 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001248 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001249 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001250
Adam Cohenfbba09b2011-07-18 21:43:05 -07001251 // First we check if we already know the exact location where we want to add this item.
1252 if (cellX >= 0 && cellY >= 0) {
1253 cellXY[0] = cellX;
1254 cellXY[1] = cellY;
1255 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001256 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001257 return;
1258 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001260 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001261
Romain Guy73b979d2009-06-09 12:57:21 -07001262 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001263 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001265 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001266 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001267 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001268 } else {
1269 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
1271 }
Romain Guycbb89e42009-06-08 15:52:54 -07001272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 /**
1274 * Add a shortcut to the workspace.
1275 *
1276 * @param data The intent describing the shortcut.
1277 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001279 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001280 int cellY) {
1281 int[] cellXY = mTmpAddItemCellCoordinates;
1282 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001283 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001284
Michael Jurkad3ef3062010-11-23 16:23:58 -08001285 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001286
Adam Cohen558baaf2011-08-15 15:22:57 -07001287 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001288 if (info == null) {
1289 return;
1290 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001291 final View view = createShortcut(info);
1292
Adam Cohenfbba09b2011-07-18 21:43:05 -07001293 // First we check if we already know the exact location where we want to add this item.
1294 if (cellX >= 0 && cellY >= 0) {
1295 cellXY[0] = cellX;
1296 cellXY[1] = cellY;
1297 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001298
1299 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001300 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001301 true, null,null)) {
1302 return;
1303 }
1304 DragObject dragObject = new DragObject();
1305 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001306 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1307 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001308 return;
1309 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001310 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001311 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001313 foundCellSpan = (result != null);
1314 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001315 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001316 }
1317
1318 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001319 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320 return;
1321 }
1322
Adam Cohendcd297f2013-06-18 13:13:40 -07001323 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324
1325 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001326 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001327 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
1329 }
1330
Adam Cohen2f093b62012-04-30 18:59:53 -07001331 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1332 int minHeight) {
1333 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001334 // We want to account for the extra amount of padding that we are adding to the widget
1335 // to ensure that it gets the full amount of space that it has requested
1336 int requiredWidth = minWidth + padding.left + padding.right;
1337 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001338 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001339 }
1340
Adam Cohen2f093b62012-04-30 18:59:53 -07001341 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1342 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001343 }
1344
Adam Cohen2f093b62012-04-30 18:59:53 -07001345 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1346 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001347 }
1348
Adam Cohen2f093b62012-04-30 18:59:53 -07001349 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1350 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001351 }
1352
Adam Cohen2f093b62012-04-30 18:59:53 -07001353 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1354 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001355 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001356 }
1357
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001359 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001361 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001362 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001364 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001365 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1366 if (appWidgetInfo == null) {
1367 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1368 }
Romain Guycbb89e42009-06-08 15:52:54 -07001369
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001370 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001371 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001372
Adam Cohen2f093b62012-04-30 18:59:53 -07001373 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1374 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001375
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001377 // We have saved the position to which the widget was dragged-- this really only matters
1378 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001379 int[] cellXY = mTmpAddItemCellCoordinates;
1380 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001381 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001382 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001383 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1384 cellXY[0] = mPendingAddInfo.cellX;
1385 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001386 spanXY[0] = mPendingAddInfo.spanX;
1387 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001388 foundCellSpan = true;
1389 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001390 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001391 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001392 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1393 spanXY[1], cellXY, finalSpan);
1394 spanXY[0] = finalSpan[0];
1395 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001396 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001397 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001398 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001399 }
1400
Michael Jurka0280c3b2010-09-17 15:00:07 -07001401 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001402 if (appWidgetId != -1) {
1403 // Deleting an app widget ID is a void call but writes to disk before returning
1404 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001405 new Thread("deleteAppWidgetId") {
1406 public void run() {
1407 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1408 }
1409 }.start();
1410 }
Winson Chung93eef082012-03-23 15:59:27 -07001411 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001412 return;
1413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001415 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001416 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1417 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001418 launcherInfo.spanX = spanXY[0];
1419 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001420 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1421 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001422
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001424 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425
1426 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001427 if (hostView == null) {
1428 // Perform actual inflation because we're live
1429 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1430 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1431 } else {
1432 // The AppWidgetHostView has already been inflated and instantiated
1433 launcherInfo.hostView = hostView;
1434 }
Romain Guycbb89e42009-06-08 15:52:54 -07001435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001437 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001438 launcherInfo.notifyWidgetSizeChanged(this);
1439
Adam Cohendcd297f2013-06-18 13:13:40 -07001440 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001442
1443 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001445 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
Romain Guycbb89e42009-06-08 15:52:54 -07001447
Adam Cohended9f8d2010-11-03 13:25:16 -07001448 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1449 @Override
1450 public void onReceive(Context context, Intent intent) {
1451 final String action = intent.getAction();
1452 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1453 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001454 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001455 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001456
Winson Chungc100e8e2011-08-09 16:02:43 -07001457 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001458 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001459 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1460 mAppsCustomizeTabHost.reset();
Adam Cohen040a5d22013-09-16 17:09:33 -07001461 showWorkspaceAndExitOverviewMode(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001462 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001463 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1464 mUserPresent = true;
1465 updateRunning();
1466 }
1467 }
1468 };
1469
1470 @Override
1471 public void onAttachedToWindow() {
1472 super.onAttachedToWindow();
1473
1474 // Listen for broadcasts related to user-presence
1475 final IntentFilter filter = new IntentFilter();
1476 filter.addAction(Intent.ACTION_SCREEN_OFF);
1477 filter.addAction(Intent.ACTION_USER_PRESENT);
1478 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001479 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001480 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001481 mVisible = true;
1482 }
1483
1484 @Override
1485 public void onDetachedFromWindow() {
1486 super.onDetachedFromWindow();
1487 mVisible = false;
1488
Adam Cohend113e0c2010-11-11 10:48:05 -08001489 if (mAttached) {
1490 unregisterReceiver(mReceiver);
1491 mAttached = false;
1492 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001493 updateRunning();
1494 }
1495
1496 public void onWindowVisibilityChanged(int visibility) {
1497 mVisible = visibility == View.VISIBLE;
1498 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001499 // The following code used to be in onResume, but it turns out onResume is called when
1500 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1501 // is a more appropriate event to handle
1502 if (mVisible) {
1503 mAppsCustomizeTabHost.onWindowVisible();
1504 if (!mWorkspaceLoading) {
1505 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001506 // We want to let Launcher draw itself at least once before we force it to build
1507 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001508 // apps is nice and speedy.
1509 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001510 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001511 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001512 if (mStarted) return;
1513 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001514 // We delay the layer building a bit in order to give
1515 // other message processing a time to run. In particular
1516 // this avoids a delay in hiding the IME if it was
1517 // currently shown, because doing that may involve
1518 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001519 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001520 final ViewTreeObserver.OnDrawListener listener = this;
1521 mWorkspace.post(new Runnable() {
1522 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001523 if (mWorkspace != null &&
1524 mWorkspace.getViewTreeObserver() != null) {
1525 mWorkspace.getViewTreeObserver().
1526 removeOnDrawListener(listener);
1527 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001528 }
1529 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001530 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001531 }
1532 });
1533 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001534 // When Launcher comes back to foreground, a different Activity might be responsible for
1535 // the app market intent, so refresh the icon
1536 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001537 clearTypedText();
1538 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001539 }
1540
1541 private void sendAdvanceMessage(long delay) {
1542 mHandler.removeMessages(ADVANCE_MSG);
1543 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1544 mHandler.sendMessageDelayed(msg, delay);
1545 mAutoAdvanceSentTime = System.currentTimeMillis();
1546 }
1547
1548 private void updateRunning() {
1549 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1550 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1551 mAutoAdvanceRunning = autoAdvanceRunning;
1552 if (autoAdvanceRunning) {
1553 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1554 sendAdvanceMessage(delay);
1555 } else {
1556 if (!mWidgetsToAdvance.isEmpty()) {
1557 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1558 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1559 }
1560 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001561 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001562 }
1563 }
1564 }
1565
1566 private final Handler mHandler = new Handler() {
1567 @Override
1568 public void handleMessage(Message msg) {
1569 if (msg.what == ADVANCE_MSG) {
1570 int i = 0;
1571 for (View key: mWidgetsToAdvance.keySet()) {
1572 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1573 final int delay = mAdvanceStagger * i;
1574 if (v instanceof Advanceable) {
1575 postDelayed(new Runnable() {
1576 public void run() {
1577 ((Advanceable) v).advance();
1578 }
1579 }, delay);
1580 }
1581 i++;
1582 }
1583 sendAdvanceMessage(mAdvanceInterval);
1584 }
1585 }
1586 };
1587
1588 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001589 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1591 if (v instanceof Advanceable) {
1592 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001593 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001594 updateRunning();
1595 }
1596 }
1597
1598 void removeWidgetToAutoAdvance(View hostView) {
1599 if (mWidgetsToAdvance.containsKey(hostView)) {
1600 mWidgetsToAdvance.remove(hostView);
1601 updateRunning();
1602 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001603 }
1604
Joe Onorato9c1289c2009-08-17 11:03:03 -04001605 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001606 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607 launcherInfo.hostView = null;
1608 }
1609
Winson Chung93eef082012-03-23 15:59:27 -07001610 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1611 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1612 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001613 }
1614
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001615 public LauncherAppWidgetHost getAppWidgetHost() {
1616 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 }
Romain Guycbb89e42009-06-08 15:52:54 -07001618
Winson Chunga9abd0e2010-10-27 17:18:37 -07001619 public LauncherModel getModel() {
1620 return mModel;
1621 }
1622
Bjorn Bringertc459e522013-06-07 19:36:01 +01001623 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001624 getWindow().closeAllPanels();
1625
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001626 // Whatever we were doing is hereby canceled.
1627 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001628 }
1629
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 @Override
1631 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001632 long startTime = 0;
1633 if (DEBUG_RESUME_TIME) {
1634 startTime = System.currentTimeMillis();
1635 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 super.onNewIntent(intent);
1637
1638 // Close the menu
1639 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001640 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001641 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001642
Jamie Genniscb222e82012-10-23 15:44:26 -07001643 final boolean alreadyOnHome =
1644 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001645 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001646
Jamie Genniscb222e82012-10-23 15:44:26 -07001647 Runnable processIntent = new Runnable() {
1648 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001649 if (mWorkspace == null) {
1650 // Can be cases where mWorkspace is null, this prevents a NPE
1651 return;
1652 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001653 Folder openFolder = mWorkspace.getOpenFolder();
1654 // In all these cases, only animate if we're already on home
1655 mWorkspace.exitWidgetResizeMode();
1656 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1657 openFolder == null) {
1658 mWorkspace.moveToDefaultScreen(true);
1659 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001660
Jamie Genniscb222e82012-10-23 15:44:26 -07001661 closeFolder();
1662 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001663
Jamie Genniscb222e82012-10-23 15:44:26 -07001664 // If we are already on home, then just animate back to the workspace,
1665 // otherwise, just wait until onResume to set the state back to Workspace
1666 if (alreadyOnHome) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001667 showWorkspaceAndExitOverviewMode(true);
Jamie Genniscb222e82012-10-23 15:44:26 -07001668 } else {
1669 mOnResumeState = State.WORKSPACE;
1670 }
1671
1672 final View v = getWindow().peekDecorView();
1673 if (v != null && v.getWindowToken() != null) {
1674 InputMethodManager imm = (InputMethodManager)getSystemService(
1675 INPUT_METHOD_SERVICE);
1676 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1677 }
1678
1679 // Reset AllApps to its initial state
1680 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1681 mAppsCustomizeTabHost.reset();
1682 }
1683 }
1684 };
1685
1686 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1687 // Delay processing of the intent to allow the status bar animation to finish
1688 // first in order to avoid janky animations.
1689 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001690 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001691 // Process the intent immediately.
1692 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001693 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 }
Michael Jurka447bf842013-05-15 14:52:15 +02001696 if (DEBUG_RESUME_TIME) {
1697 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 }
1700
Adam Cohen040a5d22013-09-16 17:09:33 -07001701 protected void showWorkspaceAndExitOverviewMode(boolean animate) {
1702 showWorkspace(animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001703 if (mWorkspace.isInOverviewMode()) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001704 mWorkspace.exitOverviewMode(animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001705 }
1706 }
Adam Cohen040a5d22013-09-16 17:09:33 -07001707 protected void showWorkspaceAndExitOverviewMode() {
1708 showWorkspaceAndExitOverviewMode(true);
1709 }
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001710
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001712 public void onRestoreInstanceState(Bundle state) {
1713 super.onRestoreInstanceState(state);
1714 for (int page: mSynchronouslyBoundPages) {
1715 mWorkspace.restoreInstanceStateForChild(page);
1716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 }
1718
1719 @Override
1720 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001721 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001722 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723
Michael Jurka883f55b2010-10-21 15:47:14 -07001724 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001725 // We close any open folder since it will not be re-opened, and we need to make sure
1726 // this state is reflected.
1727 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728
Adam Cohendcd297f2013-06-18 13:13:40 -07001729 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001730 mWaitingForResult) {
1731 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001732 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001733 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1734 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001735 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1736 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1737 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 }
1739
1740 if (mFolderInfo != null && mWaitingForResult) {
1741 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1742 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1743 }
Michael Jurka946ad472010-07-09 18:05:18 -07001744
Winson Chung785d2eb2011-04-14 16:08:02 -07001745 // Save the current AppsCustomize tab
1746 if (mAppsCustomizeTabHost != null) {
1747 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1748 if (currentTabTag != null) {
1749 outState.putString("apps_customize_currentTab", currentTabTag);
1750 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001751 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1752 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
1755
1756 @Override
1757 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001759
Winson Chunge7a03942011-08-05 15:05:12 -07001760 // Remove all pending runnables
1761 mHandler.removeMessages(ADVANCE_MSG);
1762 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001763 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001764
Winson Chungcd2b0142011-06-08 16:02:26 -07001765 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001766 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001767 mModel.stopLoader();
1768 app.setLauncher(null);
1769
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001771 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001773 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001775 mAppWidgetHost = null;
1776
1777 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778
1779 TextKeyListener.getInstance().release();
1780
Winson Chung81b52252012-08-27 15:34:29 -07001781 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1782 // to prevent leaking Launcher activities on orientation change.
1783 if (mModel != null) {
1784 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1785 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001786
1787 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001788 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001789
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001790 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001791 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1792 mWorkspace.removeAllViews();
1793 mWorkspace = null;
1794 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001795
Michael Jurka2ecf9952012-06-18 12:52:28 -07001796 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 }
1798
Adam Cohena9cf38f2011-05-02 15:36:58 -07001799 public DragController getDragController() {
1800 return mDragController;
1801 }
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 @Override
1804 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001805 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 super.startActivityForResult(intent, requestCode);
1807 }
1808
Winson Chung70d72102011-08-12 11:24:35 -07001809 /**
1810 * Indicates that we want global search for this activity by setting the globalSearch
1811 * argument for {@link #startSearch} to true.
1812 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001814 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001816
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001817 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001818
Karl Rosaen138a0412009-04-23 19:00:21 -07001819 if (initialQuery == null) {
1820 // Use any text typed in the launcher as the initial query
1821 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 if (appSearchData == null) {
1824 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001825 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 }
Winson Chungadf0c182012-08-23 14:59:07 -07001827 Rect sourceBounds = new Rect();
1828 if (mSearchDropTargetBar != null) {
1829 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1830 }
Romain Guycbb89e42009-06-08 15:52:54 -07001831
Bjorn Bringertc459e522013-06-07 19:36:01 +01001832 startSearch(initialQuery, selectInitialQuery,
1833 appSearchData, sourceBounds);
1834 }
1835
1836 public void startSearch(String initialQuery,
1837 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001838 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001839 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001840 }
1841
1842 /**
1843 * Starts the global search activity. This code is a copied from SearchManager
1844 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001845 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001846 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001847 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001848 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1849 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1850 if (globalSearchActivity == null) {
1851 Log.w(TAG, "No global search activity found.");
1852 return;
1853 }
1854 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1855 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1856 intent.setComponent(globalSearchActivity);
1857 // Make sure that we have a Bundle to put source in
1858 if (appSearchData == null) {
1859 appSearchData = new Bundle();
1860 } else {
1861 appSearchData = new Bundle(appSearchData);
1862 }
1863 // Set source to package name of app that starts global search, if not set already.
1864 if (!appSearchData.containsKey("source")) {
1865 appSearchData.putString("source", getPackageName());
1866 }
1867 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1868 if (!TextUtils.isEmpty(initialQuery)) {
1869 intent.putExtra(SearchManager.QUERY, initialQuery);
1870 }
1871 if (selectInitialQuery) {
1872 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1873 }
1874 intent.setSourceBounds(sourceBounds);
1875 try {
1876 startActivity(intent);
1877 } catch (ActivityNotFoundException ex) {
1878 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 }
1881
Winson Chung70d72102011-08-12 11:24:35 -07001882 @Override
1883 public boolean onCreateOptionsMenu(Menu menu) {
1884 if (isWorkspaceLocked()) {
1885 return false;
1886 }
1887
1888 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001889
1890 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1891 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1892 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001893 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1894 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1895 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001896 String helpUrl = getString(R.string.help_url);
1897 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001898 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1899 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1900
Winson Chung70d72102011-08-12 11:24:35 -07001901 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1902 .setIcon(android.R.drawable.ic_menu_gallery)
1903 .setAlphabeticShortcut('W');
1904 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1905 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001906 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001907 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001908 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1909 .setIcon(android.R.drawable.ic_menu_preferences)
1910 .setIntent(settings)
1911 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001912 if (!helpUrl.isEmpty()) {
1913 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1914 .setIcon(android.R.drawable.ic_menu_help)
1915 .setIntent(help)
1916 .setAlphabeticShortcut('H');
1917 }
Winson Chung70d72102011-08-12 11:24:35 -07001918 return true;
1919 }
1920
1921 @Override
1922 public boolean onPrepareOptionsMenu(Menu menu) {
1923 super.onPrepareOptionsMenu(menu);
1924
1925 if (mAppsCustomizeTabHost.isTransitioning()) {
1926 return false;
1927 }
1928 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1929 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1930
1931 return true;
1932 }
1933
1934 @Override
1935 public boolean onOptionsItemSelected(MenuItem item) {
1936 switch (item.getItemId()) {
1937 case MENU_WALLPAPER_SETTINGS:
1938 startWallpaper();
1939 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001940 }
1941
1942 return super.onOptionsItemSelected(item);
1943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001945 @Override
1946 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001947 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001948 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001949 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001950 }
1951
Joe Onorato9c1289c2009-08-17 11:03:03 -04001952 public boolean isWorkspaceLocked() {
1953 return mWorkspaceLoading || mWaitingForResult;
1954 }
1955
Michael Jurka0280c3b2010-09-17 15:00:07 -07001956 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001957 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001958 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001959 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1960 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001961 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001962 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001963 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001964
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001965 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1966 AppWidgetProviderInfo appWidgetInfo) {
1967 if (appWidgetInfo.configure != null) {
1968 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001969
Bjorn Bringert7984c942009-12-09 15:38:25 +00001970 // Launch over to configure widget, if needed
1971 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001972 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001973 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001974 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001976 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001977 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001978 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001979 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001980 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
1982 }
Romain Guycbb89e42009-06-08 15:52:54 -07001983
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001984 protected void moveToCustomContentScreen(boolean animate) {
1985 mWorkspace.moveToCustomContentScreen(animate);
1986 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001987 /**
1988 * Process a shortcut drop.
1989 *
1990 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001991 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001992 * @param cell The cell it should be added to, optional
1993 * @param position The location on the screen where it was dropped, optional
1994 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001995 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001996 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001997 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001998 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001999 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002000 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002001
2002 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002003 mPendingAddInfo.cellX = cell[0];
2004 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002005 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002006
2007 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2008 createShortcutIntent.setComponent(componentName);
2009 processShortcut(createShortcutIntent);
2010 }
2011
Adam Cohenfbba09b2011-07-18 21:43:05 -07002012 /**
2013 * Process a widget drop.
2014 *
2015 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002016 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002017 * @param cell The cell it should be added to, optional
2018 * @param position The location on the screen where it was dropped, optional
2019 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002020 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002021 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002022 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002023 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002024 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002025 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002026 mPendingAddInfo.minSpanX = info.minSpanX;
2027 mPendingAddInfo.minSpanY = info.minSpanY;
2028
Adam Cohenfbba09b2011-07-18 21:43:05 -07002029 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002030 mPendingAddInfo.cellX = cell[0];
2031 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002032 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002033 if (span != null) {
2034 mPendingAddInfo.spanX = span[0];
2035 mPendingAddInfo.spanY = span[1];
2036 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002037
Adam Cohened66b2b2012-01-23 17:28:51 -08002038 AppWidgetHostView hostView = info.boundWidget;
2039 int appWidgetId;
2040 if (hostView != null) {
2041 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002042 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002043 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002044 // In this case, we either need to start an activity to get permission to bind
2045 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002046 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002047 Bundle options = info.bindOptions;
2048
2049 boolean success = false;
2050 if (options != null) {
2051 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2052 info.componentName, options);
2053 } else {
2054 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2055 info.componentName);
2056 }
2057 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002058 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002059 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002060 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002061 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2062 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2063 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002064 // TODO: we need to make sure that this accounts for the options bundle.
2065 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002066 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2067 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002068 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002069 }
2070
Joe Onoratodeb98af2010-02-19 14:59:39 -08002071 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002072 // Handle case where user selected "Applications"
2073 String applicationName = getResources().getString(R.string.group_applications);
2074 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002075
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002076 if (applicationName != null && applicationName.equals(shortcutName)) {
2077 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2078 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002079
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002080 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2081 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002082 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002083 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002084 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002085 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088
Winson Chung24ab2f12010-09-16 14:10:47 -07002089 void processWallpaper(Intent intent) {
2090 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2091 }
2092
Adam Cohendcd297f2013-06-18 13:13:40 -07002093 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002094 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002095 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 folderInfo.title = getText(R.string.folder_name);
2097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002099 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002100 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002101 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102
2103 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002104 FolderIcon newFolder =
2105 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002106 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002107 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002108 // Force measure the new folder icon
2109 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2110 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002111 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 }
Romain Guycbb89e42009-06-08 15:52:54 -07002113
Joe Onorato9c1289c2009-08-17 11:03:03 -04002114 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002115 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002116 }
2117
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002118 protected void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002119 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002121 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002122 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 }
2124
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002125 protected ComponentName getWallpaperPickerComponent() {
2126 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2127 }
2128
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002129 /**
2130 * Registers various content observers. The current implementation registers
2131 * only a favorites observer to keep track of the favorites applications.
2132 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002133 private void registerContentObservers() {
2134 ContentResolver resolver = getContentResolver();
2135 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2136 true, mWidgetObserver);
2137 }
2138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 @Override
2140 public boolean dispatchKeyEvent(KeyEvent event) {
2141 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2142 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002144 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002145 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002146 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002147 dumpState();
2148 return true;
2149 }
2150 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002151 }
2152 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2153 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002154 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 return true;
2156 }
2157 }
2158
2159 return super.dispatchKeyEvent(event);
2160 }
2161
Joe Onorato88ec0992009-11-19 13:16:06 -08002162 @Override
2163 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002164 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002165 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002166 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002167 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002168 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002169 Folder openFolder = mWorkspace.getOpenFolder();
2170 if (openFolder.isEditingName()) {
2171 openFolder.dismissEditingName();
2172 } else {
2173 closeFolder();
2174 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002175 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002176 mWorkspace.exitWidgetResizeMode();
2177
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002178 // Back button is a no-op here, but give at least some feedback for the button press
2179 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002180 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002181 }
2182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002184 * Re-listen when widgets are reset.
2185 */
2186 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002187 if (mAppWidgetHost != null) {
2188 mAppWidgetHost.startListening();
2189 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002190 }
2191
2192 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 * Launches the intent referred by the clicked shortcut.
2194 *
2195 * @param v The view representing the clicked shortcut.
2196 */
2197 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002198 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2199 // view has detached (it's possible for this to happen if the view is removed mid touch).
2200 if (v.getWindowToken() == null) {
2201 return;
2202 }
2203
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002204 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002205 return;
2206 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002207
Adam Cohen1697b792013-09-17 19:08:21 -07002208 if (v instanceof Workspace) {
2209 if (mWorkspace.isInOverviewMode()) {
2210 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002211 }
2212 return;
2213 }
2214
2215 if (v instanceof CellLayout) {
2216 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002217 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002218 }
2219 }
2220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002222 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002224 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2225 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002226
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002227 // Check for special shortcuts
2228 if (intent.getComponent() != null) {
2229 final String shortcutClass = intent.getComponent().getClassName();
2230
2231 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chungc58497e2013-09-03 17:48:37 -07002232 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002233 return;
2234 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2235 MemoryDumpActivity.startDump(this);
2236 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002237 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2238 toggleShowWeightWatcher();
2239 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002240 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002241 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002242
2243 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002244 int[] pos = new int[2];
2245 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002246 intent.setSourceBounds(new Rect(pos[0], pos[1],
2247 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002248
2249 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002250
Daniel Sandlerff02d492013-08-05 02:12:05 -04002251 mStats.recordLaunch(intent, shortcut);
2252
Michael Jurkaddd62e92011-02-16 17:49:14 -08002253 if (success && v instanceof BubbleTextView) {
2254 mWaitingForResume = (BubbleTextView) v;
2255 mWaitingForResume.setStayPressed(true);
2256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002258 if (v instanceof FolderIcon) {
2259 FolderIcon fi = (FolderIcon) v;
2260 handleFolderClick(fi);
2261 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002262 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002263 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002264 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002265 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002266 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268 }
2269 }
2270
Michael Jurka0e260592010-06-30 17:07:39 -07002271 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002272 return false;
2273 }
2274
Michael Jurkaaf442092010-06-10 17:01:57 -07002275 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002276 * Event handler for the search button
2277 *
2278 * @param v The view that was clicked.
2279 */
2280 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002281 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2282
Amith Yamasania135ba82011-08-09 17:42:01 -07002283 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002284 }
2285
2286 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002287 * Event handler for the voice button
2288 *
2289 * @param v The view that was clicked.
2290 */
2291 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002292 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002293
Bjorn Bringertc459e522013-06-07 19:36:01 +01002294 startVoice();
2295 }
2296
2297 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002298 try {
2299 final SearchManager searchManager =
2300 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2301 ComponentName activityName = searchManager.getGlobalSearchActivity();
2302 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002303 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002304 if (activityName != null) {
2305 intent.setPackage(activityName.getPackageName());
2306 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002307 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002308 } catch (ActivityNotFoundException e) {
2309 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002310 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002311 startActivitySafely(null, intent, "onClickVoiceButton");
2312 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002313 }
2314
2315 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002316 * Event handler for the "grid" button that appears on the home screen, which
2317 * enters all apps mode.
2318 *
2319 * @param v The view that was clicked.
2320 */
2321 public void onClickAllAppsButton(View v) {
Winson Chungc58497e2013-09-03 17:48:37 -07002322 showAllApps(true, AppsCustomizePagedView.ContentType.Applications);
Michael Jurka5130e402011-10-13 04:55:35 -07002323 }
2324
2325 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002326 // Provide the same haptic feedback that the system offers for virtual keys.
2327 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002328 }
2329
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002330 public void onClickAppMarketButton(View v) {
2331 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002332 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002333 } else {
2334 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002335 }
2336 }
2337
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002338 /**
2339 * Called when the user stops interacting with the launcher.
2340 * This implies that the user is now on the homescreen and is not doing housekeeping.
2341 */
2342 protected void onInteractionEnd() {}
2343
2344 /**
2345 * Called when the user starts interacting with the launcher.
2346 * The possible interactions are:
2347 * - open all apps
2348 * - reorder an app shortcut, or a widget
2349 * - open the overview mode.
2350 * This is a good time to stop doing things that only make sense
2351 * when the user is on the homescreen and not doing housekeeping.
2352 */
2353 protected void onInteractionBegin() {}
2354
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002355 void startApplicationDetailsActivity(ComponentName componentName) {
2356 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002357 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2358 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002359 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002360 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002361 }
2362
Michael Jurka1e2f4652013-07-08 18:03:46 -07002363 // returns true if the activity was started
2364 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002365 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002366 // System applications cannot be installed. For now, show a toast explaining that.
2367 // We may give them the option of disabling apps this way.
2368 int messageId = R.string.uninstall_system_app_text;
2369 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002370 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002371 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002372 String packageName = componentName.getPackageName();
2373 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002374 Intent intent = new Intent(
2375 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002376 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2377 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002378 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002379 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002380 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002381 }
2382
Michael Jurka86a720e2012-05-09 11:23:23 -07002383 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002387 // Only launch using the new animation if the shortcut has not opted out (this is a
2388 // private contract between launcher and may be ignored in the future).
2389 boolean useLaunchAnimation = (v != null) &&
2390 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2391 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002392 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2393 v.getMeasuredWidth(), v.getMeasuredHeight());
2394
2395 startActivity(intent, opts.toBundle());
2396 } else {
2397 startActivity(intent);
2398 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002399 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 } catch (SecurityException e) {
2401 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002402 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002404 "or use the exported attribute for this activity. "
2405 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002407 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002409
Michael Jurka86a720e2012-05-09 11:23:23 -07002410 boolean startActivitySafely(View v, Intent intent, Object tag) {
2411 boolean success = false;
2412 try {
2413 success = startActivity(v, intent, tag);
2414 } catch (ActivityNotFoundException e) {
2415 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2416 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2417 }
2418 return success;
2419 }
2420
Adam Cohena9cf38f2011-05-02 15:36:58 -07002421 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002422 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002423 Folder openFolder = mWorkspace.getFolderForTag(info);
2424
2425 // If the folder info reports that the associated folder is open, then verify that
2426 // it is actually opened. There have been a few instances where this gets out of sync.
2427 if (info.opened && openFolder == null) {
2428 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002429 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002430 info.opened = false;
2431 }
2432
Adam Cohenfb91f302012-06-11 15:45:18 -07002433 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 // Close any open folder
2435 closeFolder();
2436 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002437 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 } else {
2439 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 int folderScreen;
2441 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002442 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 // .. and close it
2444 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002445 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446 // Close any folder open on the current screen
2447 closeFolder();
2448 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002449 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 }
2451 }
2452 }
2453 }
2454
Adam Cohen268c4752012-06-06 17:47:33 -07002455 /**
2456 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2457 * in the DragLayer in the exact absolute location of the original FolderIcon.
2458 */
2459 private void copyFolderIconToImage(FolderIcon fi) {
2460 final int width = fi.getMeasuredWidth();
2461 final int height = fi.getMeasuredHeight();
2462
2463 // Lazy load ImageView, Bitmap and Canvas
2464 if (mFolderIconImageView == null) {
2465 mFolderIconImageView = new ImageView(this);
2466 }
2467 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2468 mFolderIconBitmap.getHeight() != height) {
2469 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2470 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2471 }
2472
2473 DragLayer.LayoutParams lp;
2474 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2475 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2476 } else {
2477 lp = new DragLayer.LayoutParams(width, height);
2478 }
2479
Adam Cohen307fe232012-08-16 17:55:58 -07002480 // The layout from which the folder is being opened may be scaled, adjust the starting
2481 // view size by this scale factor.
2482 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002483 lp.customPosition = true;
2484 lp.x = mRectForFolderAnimation.left;
2485 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002486 lp.width = (int) (scale * width);
2487 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002488
2489 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2490 fi.draw(mFolderIconCanvas);
2491 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002492 if (fi.getFolder() != null) {
2493 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2494 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002495 }
Adam Cohen268c4752012-06-06 17:47:33 -07002496 // Just in case this image view is still in the drag layer from a previous animation,
2497 // we remove it and re-add it.
2498 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2499 mDragLayer.removeView(mFolderIconImageView);
2500 }
2501 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002502 if (fi.getFolder() != null) {
2503 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002504 }
Adam Cohen268c4752012-06-06 17:47:33 -07002505 }
2506
Adam Cohen2801caf2011-05-13 20:57:39 -07002507 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002508 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002509 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2510 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2511 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2512
Adam Cohenc51934b2011-07-26 21:07:43 -07002513 FolderInfo info = (FolderInfo) fi.getTag();
2514 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2515 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002516 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2517 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002518 }
2519
Adam Cohen268c4752012-06-06 17:47:33 -07002520 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2521 copyFolderIconToImage(fi);
2522 fi.setVisibility(View.INVISIBLE);
2523
Michael Jurka2ecf9952012-06-18 12:52:28 -07002524 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002525 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002526 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2527 oa.start();
2528 }
2529
Adam Cohen268c4752012-06-06 17:47:33 -07002530 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002531 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002532 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2533 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2534 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2535
Adam Cohen268c4752012-06-06 17:47:33 -07002536 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002537
Adam Cohen268c4752012-06-06 17:47:33 -07002538 // We remove and re-draw the FolderIcon in-case it has changed
2539 mDragLayer.removeView(mFolderIconImageView);
2540 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002541 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002542 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002543 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002544 oa.addListener(new AnimatorListenerAdapter() {
2545 @Override
2546 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002547 if (cl != null) {
2548 cl.clearFolderLeaveBehind();
2549 // Remove the ImageView copy of the FolderIcon and make the original visible.
2550 mDragLayer.removeView(mFolderIconImageView);
2551 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002552 }
2553 }
2554 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002555 oa.start();
2556 }
2557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002559 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 * is animated relative to the specified View. If the View is null, no animation
2561 * is played.
2562 *
2563 * @param folderInfo The FolderInfo describing the folder to open.
2564 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002565 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002566 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002567 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568
Adam Cohena9cf38f2011-05-02 15:36:58 -07002569 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570
Adam Cohen4554ee12011-08-03 16:13:21 -07002571 // Just verify that the folder hasn't already been added to the DragLayer.
2572 // There was a one-off crash where the folder had a parent already.
2573 if (folder.getParent() == null) {
2574 mDragLayer.addView(folder);
2575 mDragController.addDropTarget((DropTarget) folder);
2576 } else {
2577 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2578 folder.getParent() + ").");
2579 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002580 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002581 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002582
2583 // Notify the accessibility manager that this folder "window" has appeared and occluded
2584 // the workspace items
2585 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2586 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002587 }
2588
2589 public void closeFolder() {
2590 Folder folder = mWorkspace.getOpenFolder();
2591 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002592 if (folder.isEditingName()) {
2593 folder.dismissEditingName();
2594 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002595 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002596
2597 // Dismiss the folder cling
2598 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002599 }
2600 }
2601
2602 void closeFolder(Folder folder) {
2603 folder.getInfo().opened = false;
2604
2605 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2606 if (parent != null) {
2607 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2608 shrinkAndFadeInFolderIcon(fi);
2609 }
2610 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002611
2612 // Notify the accessibility manager that this folder "window" has disappeard and no
2613 // longer occludeds the workspace items
2614 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002615 }
2616
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002617 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002618 if (!isDraggingEnabled()) return false;
2619 if (isWorkspaceLocked()) return false;
2620 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002621
Adam Cohen1697b792013-09-17 19:08:21 -07002622 if (v instanceof Workspace) {
2623 if (!mWorkspace.isInOverviewMode()) {
2624 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2625 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2626 mWorkspace.enterOverviewMode();
2627 }
2628 return true;
2629 }
2630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002632 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002633 }
2634
Michael Jurka0280c3b2010-09-17 15:00:07 -07002635 resetAddInfo();
2636 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002638 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002639 return true;
2640 }
2641
Winson Chung3d503fb2011-07-13 17:25:49 -07002642 // The hotseat touch handling does not go through Workspace, and we always allow long press
2643 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002644 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002645 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2646 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002647 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002648 // User long pressed on empty space
2649 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2650 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2651 // Disabling reordering until we sort out some issues.
2652 if (mWorkspace.isInOverviewMode()) {
2653 mWorkspace.startReordering(v);
2654 } else {
2655 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002658 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002659 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002660 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 }
2662 }
2663 }
2664 return true;
2665 }
2666
Winson Chung3d503fb2011-07-13 17:25:49 -07002667 boolean isHotseatLayout(View layout) {
2668 return mHotseat != null && layout != null &&
2669 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2670 }
2671 Hotseat getHotseat() {
2672 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002673 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002674 View getOverviewPanel() {
2675 return mOverviewPanel;
2676 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002677 SearchDropTargetBar getSearchBar() {
2678 return mSearchDropTargetBar;
2679 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002680
Winson Chung3d503fb2011-07-13 17:25:49 -07002681 /**
2682 * Returns the CellLayout of the specified container at the specified screen.
2683 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002684 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002685 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2686 if (mHotseat != null) {
2687 return mHotseat.getLayout();
2688 } else {
2689 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002690 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002691 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002692 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002693 }
2694 }
2695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002696 Workspace getWorkspace() {
2697 return mWorkspace;
2698 }
2699
Daniel Sandler843e8602010-06-07 14:59:01 -04002700 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002701 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002702 }
2703
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002704 /**
2705 * Helper method for the cameraZoomIn/cameraZoomOut animations
2706 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002707 * @param scaleFactor The scale factor used for the zoom
2708 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002709 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002710 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002711 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002712 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002713
Winson Chung18f41f82012-05-09 13:28:10 -07002714 void disableWallpaperIfInAllApps() {
2715 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002716 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002717 if (mAppsCustomizeTabHost != null &&
2718 !mAppsCustomizeTabHost.isTransitioning()) {
2719 updateWallpaperVisibility(false);
2720 }
2721 }
2722 }
2723
Craig Mautner360310b2012-10-26 15:13:08 -07002724 private void setWorkspaceBackground(boolean workspace) {
2725 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002726 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002727 }
2728
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002729 void updateWallpaperVisibility(boolean visible) {
2730 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2731 int curflags = getWindow().getAttributes().flags
2732 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2733 if (wpflags != curflags) {
2734 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2735 }
Craig Mautner360310b2012-10-26 15:13:08 -07002736 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002737 }
2738
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002739 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2740 if (v instanceof LauncherTransitionable) {
2741 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2742 }
2743 }
2744
Michael Jurkabed61d22012-02-14 22:51:29 -08002745 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2746 if (v instanceof LauncherTransitionable) {
2747 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2748 }
Winson Chung70442722012-02-10 15:43:22 -08002749
2750 // Update the workspace transition step as well
2751 dispatchOnLauncherTransitionStep(v, 0f);
2752 }
2753
2754 private void dispatchOnLauncherTransitionStep(View v, float t) {
2755 if (v instanceof LauncherTransitionable) {
2756 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2757 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002758 }
2759
2760 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2761 if (v instanceof LauncherTransitionable) {
2762 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2763 }
Winson Chung70442722012-02-10 15:43:22 -08002764
2765 // Update the workspace transition step as well
2766 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002767 }
2768
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002769 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002770 * Things to test when changing the following seven functions.
2771 * - Home from workspace
2772 * - from center screen
2773 * - from other screens
2774 * - Home from all apps
2775 * - from center screen
2776 * - from other screens
2777 * - Back from all apps
2778 * - from center screen
2779 * - from other screens
2780 * - Launch app from workspace and quit
2781 * - with back
2782 * - with home
2783 * - Launch app from all apps and quit
2784 * - with back
2785 * - with home
2786 * - Go to a screen that's not the default, then all
2787 * apps, and launch and app, and go back
2788 * - with back
2789 * -with home
2790 * - On workspace, long press power and go back
2791 * - with back
2792 * - with home
2793 * - On all apps, long press power and go back
2794 * - with back
2795 * - with home
2796 * - On workspace, power off
2797 * - On all apps, power off
2798 * - Launch an app and turn off the screen while in that app
2799 * - Go back with home key
2800 * - Go back with back key TODO: make this not go to workspace
2801 * - From all apps
2802 * - From workspace
2803 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2804 * - From all apps
2805 * - From the center workspace
2806 * - From another workspace
2807 */
2808
2809 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002810 * Zoom the camera out from the workspace to reveal 'toView'.
2811 * Assumes that the view to show is anchored at either the very top or very bottom
2812 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002813 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002814 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002815 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2816 showAppsCustomizeHelper(animated, springLoaded, contentType);
2817 }
2818 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2819 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002820 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002821 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002822 mStateAnimation.cancel();
2823 mStateAnimation = null;
2824 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002825 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002826
Winson Chungf0ea4d32011-06-06 14:27:16 -07002827 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2828 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2829 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002830 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002831 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002832 final int startDelay =
2833 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002834
Michael Jurkab3e22d92011-10-31 15:58:33 -07002835 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002836
Michael Jurkad74c9842011-07-10 12:44:21 -07002837 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002838 Animator workspaceAnim =
2839 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002840 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2841 // Set the content type for the all apps space
2842 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2843 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002844
2845 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002846 toView.setScaleX(scale);
2847 toView.setScaleY(scale);
2848 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2849 scaleAnim.
2850 scaleX(1f).scaleY(1f).
2851 setDuration(duration).
2852 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002853
Winson Chungf0ea4d32011-06-06 14:27:16 -07002854 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002855 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002856 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002857 .ofFloat(toView, "alpha", 0f, 1f)
2858 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002859 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002860 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2861 @Override
2862 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002863 if (animation == null) {
2864 throw new RuntimeException("animation is null");
2865 }
Winson Chung70442722012-02-10 15:43:22 -08002866 float t = (Float) animation.getAnimatedValue();
2867 dispatchOnLauncherTransitionStep(fromView, t);
2868 dispatchOnLauncherTransitionStep(toView, t);
2869 }
2870 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002871
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002872 // toView should appear right at the end of the workspace shrink
2873 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002874 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002875 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002876 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002877
2878 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002879 boolean animationCancelled = false;
2880
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002881 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002882 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002883 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002884 // Prepare the position
2885 toView.setTranslationX(0.0f);
2886 toView.setTranslationY(0.0f);
2887 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002888 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002889 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002890 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002891 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002892 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2893 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002894
Adam Cohenf4ddea32011-09-12 13:46:42 -07002895 if (!animationCancelled) {
2896 updateWallpaperVisibility(false);
2897 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002898
2899 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002900 if (mSearchDropTargetBar != null) {
2901 mSearchDropTargetBar.hideSearchBar(false);
2902 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002903 }
2904
Adam Cohencff6af82011-09-13 14:51:53 -07002905 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002906 public void onAnimationCancel(Animator animation) {
2907 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002908 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002909 });
2910
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002911 if (workspaceAnim != null) {
2912 mStateAnimation.play(workspaceAnim);
2913 }
Michael Jurka1899a362011-11-03 13:50:45 -07002914
2915 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002916
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002917 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2918 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002919
2920 // If any of the objects being animated haven't been measured/laid out
2921 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002922 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002923 (mWorkspace.getMeasuredWidth() == 0) ||
2924 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002925 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002926 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002927
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002928 final AnimatorSet stateAnimation = mStateAnimation;
2929 final Runnable startAnimRunnable = new Runnable() {
2930 public void run() {
2931 // Check that mStateAnimation hasn't changed while
2932 // we waited for a layout/draw pass
2933 if (mStateAnimation != stateAnimation)
2934 return;
2935 setPivotsForZoom(toView, scale);
2936 dispatchOnLauncherTransitionStart(fromView, animated, false);
2937 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002938 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002939 }
2940 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002941 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002942 final ViewTreeObserver observer = toView.getViewTreeObserver();
2943 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2944 public void onGlobalLayout() {
2945 startAnimRunnable.run();
2946 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2947 }
2948 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002949 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002950 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002951 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002952 } else {
2953 toView.setTranslationX(0.0f);
2954 toView.setTranslationY(0.0f);
2955 toView.setScaleX(1.0f);
2956 toView.setScaleY(1.0f);
2957 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002958 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002959
Daniel Sandlere4f98912013-06-25 15:13:26 -04002960 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002961 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002962 if (mSearchDropTargetBar != null) {
2963 mSearchDropTargetBar.hideSearchBar(false);
2964 }
Michael Jurkaabded662011-03-04 12:06:57 -08002965 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002966 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002967 dispatchOnLauncherTransitionStart(fromView, animated, false);
2968 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002969 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002970 dispatchOnLauncherTransitionStart(toView, animated, false);
2971 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002972 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002973 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002974 }
2975
2976 /**
2977 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002978 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002979 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002980 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002981 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2982 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002983
Michael Jurkab3e22d92011-10-31 15:58:33 -07002984 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002985 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002986 mStateAnimation.cancel();
2987 mStateAnimation = null;
2988 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002989 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002990
Winson Chungf0ea4d32011-06-06 14:27:16 -07002991 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002992 final int fadeOutDuration =
2993 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002994 final float scaleFactor = (float)
2995 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2996 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002997 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002998 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002999 if (toState == State.WORKSPACE) {
3000 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3001 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07003002 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003003 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
3004 workspaceAnim = mWorkspace.getChangeStateAnimation(
3005 Workspace.State.SPRING_LOADED, animated);
3006 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003007
Michael Jurkab3e22d92011-10-31 15:58:33 -07003008 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003009 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07003010 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003011 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003012 final LauncherViewPropertyAnimator scaleAnim =
3013 new LauncherViewPropertyAnimator(fromView);
3014 scaleAnim.
3015 scaleX(scaleFactor).scaleY(scaleFactor).
3016 setDuration(duration).
3017 setInterpolator(new Workspace.ZoomInInterpolator());
3018
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003019 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003020 .ofFloat(fromView, "alpha", 1f, 0f)
3021 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003022 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003023 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3024 @Override
3025 public void onAnimationUpdate(ValueAnimator animation) {
3026 float t = 1f - (Float) animation.getAnimatedValue();
3027 dispatchOnLauncherTransitionStep(fromView, t);
3028 dispatchOnLauncherTransitionStep(toView, t);
3029 }
3030 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003031
Michael Jurka2ecf9952012-06-18 12:52:28 -07003032 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003033
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003034 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3035 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07003036 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003037
3038 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003039 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003040 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003041 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003042 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003043 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3044 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003045 if (onCompleteRunnable != null) {
3046 onCompleteRunnable.run();
3047 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003048 mAppsCustomizeContent.updateCurrentPageScroll();
3049 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003050 }
3051 });
3052
Winson Chungf0ea4d32011-06-06 14:27:16 -07003053 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003054 if (workspaceAnim != null) {
3055 mStateAnimation.play(workspaceAnim);
3056 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003057 dispatchOnLauncherTransitionStart(fromView, animated, true);
3058 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003059 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003060 } else {
3061 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003062 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003063 dispatchOnLauncherTransitionStart(fromView, animated, true);
3064 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003065 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003066 dispatchOnLauncherTransitionStart(toView, animated, true);
3067 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003068 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003069 }
3070
Michael Jurkae326f182011-11-21 14:05:46 -08003071 @Override
3072 public void onTrimMemory(int level) {
3073 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003074 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003075 mAppsCustomizeTabHost.onTrimMemory();
3076 }
3077 }
3078
Winson Chung18f41f82012-05-09 13:28:10 -07003079 @Override
3080 public void onWindowFocusChanged(boolean hasFocus) {
3081 if (!hasFocus) {
3082 // When another window occludes launcher (like the notification shade, or recents),
3083 // ensure that we enable the wallpaper flag so that transitions are done correctly.
3084 updateWallpaperVisibility(true);
3085 } else {
3086 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07003087 mWorkspace.postDelayed(new Runnable() {
3088 @Override
3089 public void run() {
3090 disableWallpaperIfInAllApps();
3091 }
3092 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07003093 }
3094 }
3095
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003096 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003097 showWorkspace(animated, null);
3098 }
3099
3100 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003101 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003102 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003103 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003104 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003105
Winson Chungc7d2b602012-05-16 17:02:20 -07003106 // Show the search bar (only animate if we were showing the drop target bar in spring
3107 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003108 if (mSearchDropTargetBar != null) {
3109 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3110 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003111
Michael Jurkab3e22d92011-10-31 15:58:33 -07003112 // Set focus to the AppsCustomize button
3113 if (mAllAppsButton != null) {
3114 mAllAppsButton.requestFocus();
3115 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003116 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003117
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003118 // Change the state *after* we've called all the transition code
3119 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003120
Winson Chung5fb63472011-02-02 17:03:37 -08003121 // Resume the auto-advance of widgets
3122 mUserPresent = true;
3123 updateRunning();
3124
alanv1d4fde62012-10-17 13:15:47 -07003125 // Send an accessibility event to announce the context change
3126 getWindow().getDecorView()
3127 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003128
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003129 onWorkspaceShown(animated);
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07003130 onInteractionEnd();
Bjorn Bringertc459e522013-06-07 19:36:01 +01003131 }
3132
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003133 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003134 }
3135
Winson Chungc58497e2013-09-03 17:48:37 -07003136 void showAllApps(boolean animated,
3137 AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003138 if (mState != State.WORKSPACE) return;
3139
Winson Chungc58497e2013-09-03 17:48:37 -07003140 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003141 mAppsCustomizeTabHost.requestFocus();
3142
Michael Jurkab3e22d92011-10-31 15:58:33 -07003143 // Change the state *after* we've called all the transition code
3144 mState = State.APPS_CUSTOMIZE;
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07003145 onInteractionBegin();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003146
3147 // Pause the auto-advance of widgets until we are out of AllApps
3148 mUserPresent = false;
3149 updateRunning();
3150 closeFolder();
3151
3152 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003153 getWindow().getDecorView()
3154 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003155 }
3156
Adam Cohen7777d962011-08-18 18:58:38 -07003157 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003158 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003159 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003160 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003161 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003162 }
Adam Cohen7777d962011-08-18 18:58:38 -07003163
Adam Cohened66b2b2012-01-23 17:28:51 -08003164 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3165 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003166 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3167
Winson Chunge7a03942011-08-05 15:05:12 -07003168 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003169 @Override
3170 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003171 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003172 // Before we show workspace, hide all apps again because
3173 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3174 // clean up our state transition functions
3175 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003176 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003177 } else {
3178 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003179 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003180 }
3181 }, (extendedDelay ?
3182 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3183 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3184 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003185
Michael Jurkad3ef3062010-11-23 16:23:58 -08003186 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003187 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003188 final boolean animated = true;
3189 final boolean springLoaded = true;
3190 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003191 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003192 }
3193 // Otherwise, we are not in spring loaded mode, so don't do anything.
3194 }
3195
Michael Jurkab3e22d92011-10-31 15:58:33 -07003196 void lockAllApps() {
3197 // TODO
3198 }
3199
3200 void unlockAllApps() {
3201 // TODO
3202 }
3203
Winson Chungf0ea4d32011-06-06 14:27:16 -07003204 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003205 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003206 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003207 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003208 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003209 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003210 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003211 int duration = 0;
3212 if (mSearchDropTargetBar != null) {
3213 duration = mSearchDropTargetBar.getTransitionInDuration();
3214 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003215 mHotseat.animate().alpha(1f).setDuration(duration);
3216 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003217 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003218 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003219 }
3220 }
3221 }
3222
3223 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003224 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003225 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003226 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003227 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003228 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003229 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003230 int duration = 0;
3231 if (mSearchDropTargetBar != null) {
3232 duration = mSearchDropTargetBar.getTransitionOutDuration();
3233 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003234 mHotseat.animate().alpha(0f).setDuration(duration);
3235 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003236 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003237 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003238 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003239 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003240 }
3241
Patrick Dubroy5f445422011-02-18 14:35:21 -08003242 /**
3243 * Add an item from all apps or customize onto the given workspace screen.
3244 * If layout is null, add to the current screen.
3245 */
3246 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003247 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003248 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003249 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003250 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003251
Winson Chungdff8ebb2011-09-08 17:25:31 -07003252 /** Maps the current orientation to an index for referencing orientation correct global icons */
3253 private int getCurrentOrientationIndexForGlobalIcons() {
3254 // default - 0, landscape - 1
3255 switch (getResources().getConfiguration().orientation) {
3256 case Configuration.ORIENTATION_LANDSCAPE:
3257 return 1;
3258 default:
3259 return 0;
3260 }
3261 }
3262
Michael Jurkaae65ee32012-04-30 11:45:54 -07003263 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003264 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003265 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003266 // Look for the toolbar icon specified in the activity meta-data
3267 Bundle metaData = packageManager.getActivityInfo(
3268 activityName, PackageManager.GET_META_DATA).metaData;
3269 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003270 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003271 if (iconResId != 0) {
3272 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003273 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003274 }
3275 }
3276 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003277 // This can happen if the activity defines an invalid drawable
3278 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3279 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003280 } catch (Resources.NotFoundException nfe) {
3281 // This can happen if the activity defines an invalid drawable
3282 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3283 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003284 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003285 return null;
3286 }
3287
3288 // if successful in getting icon, return it; otherwise, set button to use default drawable
3289 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003290 int buttonId, ComponentName activityName, int fallbackDrawableId,
3291 String toolbarResourceName) {
3292 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003293 Resources r = getResources();
3294 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3295 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003296
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003297 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003298 // If we were unable to find the icon via the meta-data, use a generic one
3299 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003300 toolbarIcon = r.getDrawable(fallbackDrawableId);
3301 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003302 if (button != null) {
3303 button.setCompoundDrawables(toolbarIcon, null, null, null);
3304 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003305 return null;
3306 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003307 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003308 if (button != null) {
3309 button.setCompoundDrawables(toolbarIcon, null, null, null);
3310 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003311 return toolbarIcon.getConstantState();
3312 }
3313 }
3314
3315 // if successful in getting icon, return it; otherwise, set button to use default drawable
3316 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003317 int buttonId, ComponentName activityName, int fallbackDrawableId,
3318 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003319 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003320 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003321
Michael Jurka19e0fc52011-07-22 18:00:21 -07003322 if (button != null) {
3323 // If we were unable to find the icon via the meta-data, use a
3324 // generic one
3325 if (toolbarIcon == null) {
3326 button.setImageResource(fallbackDrawableId);
3327 } else {
3328 button.setImageDrawable(toolbarIcon);
3329 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003330 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003331
3332 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3333
Michael Jurka0423dcf2010-10-05 14:56:18 -07003334 }
3335
Winson Chung1b884092012-06-08 17:13:02 -07003336 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003337 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003338 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003339 }
3340
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003341 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003342 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003343 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003344 }
3345
Winson Chungbb185bd2011-11-21 12:31:42 -08003346 private void invalidatePressedFocusedStates(View container, View button) {
3347 if (container instanceof HolographicLinearLayout) {
3348 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3349 layout.invalidatePressedFocusedStates();
3350 } else if (button instanceof HolographicImageView) {
3351 HolographicImageView view = (HolographicImageView) button;
3352 view.invalidatePressedFocusedStates();
3353 }
3354 }
3355
Cristina Stancu476493b2013-08-07 17:20:14 +01003356 public View getQsbBar() {
3357 if (mQsbBar == null) {
3358 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3359 }
3360 return mQsbBar;
3361 }
3362
3363 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003364 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003365 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003366 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003367 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003368
Winson Chung1cad91e2011-05-25 17:41:01 -07003369 final SearchManager searchManager =
3370 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3371 ComponentName activityName = searchManager.getGlobalSearchActivity();
3372 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003373 int coi = getCurrentOrientationIndexForGlobalIcons();
3374 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003375 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3376 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3377 if (sGlobalSearchIcon[coi] == null) {
3378 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3379 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3380 TOOLBAR_ICON_METADATA_NAME);
3381 }
3382
Winson Chungc51db6a2011-10-05 11:44:49 -07003383 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3384 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003385 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003386 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003387 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003388 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003389 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3390 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003391 if (searchButton != null) searchButton.setVisibility(View.GONE);
3392 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003393 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003394 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003395 }
3396 }
3397
Cristina Stancu476493b2013-08-07 17:20:14 +01003398 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003399 final View searchButtonContainer = findViewById(R.id.search_button_container);
3400 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003401 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003402 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003403 }
3404
Cristina Stancu476493b2013-08-07 17:20:14 +01003405 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003406 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003407 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003408
Winson Chungc51db6a2011-10-05 11:44:49 -07003409 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003410 final SearchManager searchManager =
3411 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3412 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3413
3414 ComponentName activityName = null;
3415 if (globalSearchActivity != null) {
3416 // Check if the global search activity handles voice search
3417 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3418 intent.setPackage(globalSearchActivity.getPackageName());
3419 activityName = intent.resolveActivity(getPackageManager());
3420 }
3421
3422 if (activityName == null) {
3423 // Fallback: check if an activity other than the global search activity
3424 // resolves this
3425 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3426 activityName = intent.resolveActivity(getPackageManager());
3427 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003428 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003429 int coi = getCurrentOrientationIndexForGlobalIcons();
3430 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003431 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3432 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3433 if (sVoiceSearchIcon[coi] == null) {
3434 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3435 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3436 TOOLBAR_ICON_METADATA_NAME);
3437 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003438 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003439 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003440 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003441 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003442 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003443 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003444 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003445 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003446 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003447 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003448 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003449 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003450
Cristina Stancu476493b2013-08-07 17:20:14 +01003451 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003452 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3453 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003454 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003455 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003456 }
3457
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003458 public void setVoiceButtonProxyVisible(boolean visible) {
3459 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3460 if (voiceButtonProxy != null) {
3461 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3462 }
3463 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003464 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003465 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003466 */
3467 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003468 final View marketButton = findViewById(R.id.market_button);
3469 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3470 // Find the app market activity by resolving an intent.
3471 // (If multiple app markets are installed, it will return the ResolverActivity.)
3472 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003473 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003474 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003475 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003476 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003477 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3478 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003479 marketButton.setVisibility(View.VISIBLE);
3480 } else {
3481 // We should hide and disable the view so that we don't try and restore the visibility
3482 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3483 marketButton.setVisibility(View.GONE);
3484 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003485 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003486 }
3487
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003488 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003489 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3490 Resources r = getResources();
3491 Drawable marketIconDrawable = d.newDrawable(r);
3492 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3493 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3494 marketIconDrawable.setBounds(0, 0, w, h);
3495
3496 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003497 }
3498
Michael Jurkad7c28052012-04-27 15:43:36 -07003499 @Override
3500 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003501 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003502 final List<CharSequence> text = event.getText();
3503 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003504 // Populate event with a fake title based on the current state.
3505 if (mState == State.APPS_CUSTOMIZE) {
3506 text.add(getString(R.string.all_apps_button_label));
3507 } else {
3508 text.add(getString(R.string.all_apps_home_button_label));
3509 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003510 return result;
3511 }
3512
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003513 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003514 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003515 */
3516 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3517 @Override
3518 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003519 closeSystemDialogs();
3520 }
3521 }
3522
3523 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003524 * Receives notifications whenever the appwidgets are reset.
3525 */
3526 private class AppWidgetResetObserver extends ContentObserver {
3527 public AppWidgetResetObserver() {
3528 super(new Handler());
3529 }
3530
3531 @Override
3532 public void onChange(boolean selfChange) {
3533 onAppWidgetReset();
3534 }
3535 }
3536
3537 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003538 * If the activity is currently paused, signal that we need to run the passed Runnable
3539 * in onResume.
3540 *
3541 * This needs to be called from incoming places where resources might have been loaded
3542 * while we are paused. That is becaues the Configuration might be wrong
3543 * when we're not running, and if it comes back to what it was when we
3544 * were paused, we are not restarted.
3545 *
3546 * Implementation of the method from LauncherModel.Callbacks.
3547 *
3548 * @return true if we are currently paused. The caller might be able to
3549 * skip some work in that case since we will come back again.
3550 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003551 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003552 if (mPaused) {
3553 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003554 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003555 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003556 }
3557 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003558 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003559 return true;
3560 } else {
3561 return false;
3562 }
3563 }
3564
Michael Jurkac402cd92013-05-20 15:49:32 +02003565 private boolean waitUntilResume(Runnable run) {
3566 return waitUntilResume(run, false);
3567 }
3568
Michael Jurka1e2f4652013-07-08 18:03:46 -07003569 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003570 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003571 }
3572
Michael Jurka7607c2f2013-04-03 14:33:19 -07003573 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003574 * If the activity is currently paused, signal that we need to re-run the loader
3575 * in onResume.
3576 *
3577 * This needs to be called from incoming places where resources might have been loaded
3578 * while we are paused. That is becaues the Configuration might be wrong
3579 * when we're not running, and if it comes back to what it was when we
3580 * were paused, we are not restarted.
3581 *
3582 * Implementation of the method from LauncherModel.Callbacks.
3583 *
3584 * @return true if we are currently paused. The caller might be able to
3585 * skip some work in that case since we will come back again.
3586 */
3587 public boolean setLoadOnResume() {
3588 if (mPaused) {
3589 Log.i(TAG, "setLoadOnResume");
3590 mOnResumeNeedsLoad = true;
3591 return true;
3592 } else {
3593 return false;
3594 }
3595 }
3596
3597 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003599 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003601 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003602 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003603 } else {
3604 return SCREEN_COUNT / 2;
3605 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003607
Joe Onorato9c1289c2009-08-17 11:03:03 -04003608 /**
3609 * Refreshes the shortcuts shown on the workspace.
3610 *
3611 * Implementation of the method from LauncherModel.Callbacks.
3612 */
3613 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003614 // If we're starting binding all over again, clear any bind calls we'd postponed in
3615 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3616 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003617 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003618
Winson Chungd64d1762013-08-20 14:37:16 -07003619 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003620 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003621 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003622
Michael Jurka05bf644e2011-11-30 20:28:53 -08003623 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003624 if (mHotseat != null) {
3625 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003626 }
3627 }
3628
Adam Cohendcd297f2013-06-18 13:13:40 -07003629 @Override
3630 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003631 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003632
Adam Cohen5084cba2013-09-03 12:01:16 -07003633 // If there are no screens, we need to have an empty screen
3634 if (orderedScreenIds.size() == 0) {
3635 mWorkspace.addExtraEmptyScreen();
3636 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003637
3638 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003639 // setCurrentPage() so ensure that all pages are added before calling this).
3640 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003641 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3642 mWorkspace.createCustomContentPage();
3643 }
Winson Chung64359a52013-07-08 17:17:08 -07003644 }
3645
3646 @Override
3647 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003648 int count = orderedScreenIds.size();
3649 for (int i = 0; i < count; i++) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003650 Launcher.addDumpLog(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")", true);
Adam Cohen89bddfa2013-08-20 11:57:13 -07003651 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003652 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003653 }
3654
Adam Cohen39a06042013-07-19 14:30:12 -07003655 private boolean shouldShowWeightWatcher() {
3656 String spKey = LauncherAppState.getSharedPreferencesKey();
3657 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003658 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003659
3660 return show;
3661 }
3662
Adam Cohen4caf2982013-08-20 18:54:31 -07003663 private boolean emailSent() {
3664 String spKey = LauncherAppState.getSharedPreferencesKey();
3665 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3666 boolean show = sp.getBoolean(CORRUPTION_EMAIL_SENT_KEY, false);
3667 return show;
3668 }
3669
3670 private void setEmailSent(boolean sent) {
3671 String spKey = LauncherAppState.getSharedPreferencesKey();
3672 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3673
3674 SharedPreferences.Editor editor = sp.edit();
3675 editor.putBoolean(CORRUPTION_EMAIL_SENT_KEY, sent);
3676 editor.commit();
3677 }
3678
Adam Cohen39a06042013-07-19 14:30:12 -07003679 private void toggleShowWeightWatcher() {
3680 String spKey = LauncherAppState.getSharedPreferencesKey();
3681 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3682 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3683
3684 show = !show;
3685
3686 SharedPreferences.Editor editor = sp.edit();
3687 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3688 editor.commit();
3689
3690 if (mWeightWatcher != null) {
3691 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3692 }
3693 }
3694
Winson Chungd64d1762013-08-20 14:37:16 -07003695 public void bindAppsAdded(final ArrayList<Long> newScreens,
3696 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003697 final ArrayList<ItemInfo> addAnimated,
3698 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003699 Runnable r = new Runnable() {
3700 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003701 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003702 }
3703 };
3704 if (waitUntilResume(r)) {
3705 return;
3706 }
3707
Adam Cohen4caf2982013-08-20 18:54:31 -07003708 Launcher.addDumpLog(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -07003709
3710 // Add the new screens
3711 bindAddScreens(newScreens);
3712
3713 // We add the items without animation on non-visible pages, and with
3714 // animations on the new page (which we will try and snap to).
3715 if (!addNotAnimated.isEmpty()) {
3716 bindItems(addNotAnimated, 0,
3717 addNotAnimated.size(), false);
3718 }
3719 if (!addAnimated.isEmpty()) {
3720 bindItems(addAnimated, 0,
3721 addAnimated.size(), true);
3722 }
Winson Chungc58497e2013-09-03 17:48:37 -07003723
3724 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3725 addedApps != null && mAppsCustomizeContent != null) {
3726 mAppsCustomizeContent.addApps(addedApps);
3727 }
Winson Chungd64d1762013-08-20 14:37:16 -07003728 }
3729
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003730 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 * Bind the items start-end from the list.
3732 *
3733 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003734 */
Winson Chung64359a52013-07-08 17:17:08 -07003735 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3736 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003737 Runnable r = new Runnable() {
3738 public void run() {
3739 bindItems(shortcuts, start, end, forceAnimateIcons);
3740 }
3741 };
3742 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003743 return;
3744 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003745
Winson Chungf0c6ae02012-03-21 16:10:31 -07003746 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003747 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3748 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003749 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003750 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003751 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003752 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003754
3755 // Short circuit if we are loading dock items for a configuration which has no dock
3756 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3757 mHotseat == null) {
3758 continue;
3759 }
3760
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761 switch (item.itemType) {
3762 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3763 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003764 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003765 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003766
Winson Chung64359a52013-07-08 17:17:08 -07003767 /*
3768 * TODO: FIX collision case
3769 */
Winson Chungce376632013-07-11 16:12:41 -07003770 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3771 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3772 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3773 throw new RuntimeException("OCCUPIED");
3774 }
Winson Chung64359a52013-07-08 17:17:08 -07003775 }
3776
Adam Cohendcd297f2013-06-18 13:13:40 -07003777 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3778 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003779 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003780 // Animate all the applications up now
3781 shortcut.setAlpha(0f);
3782 shortcut.setScaleX(0f);
3783 shortcut.setScaleY(0f);
3784 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003785 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003786 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003788 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003789 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003790 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003791 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003792 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3793 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003794 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003795 default:
3796 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003798 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003799
Winson Chung997a9232013-07-24 15:33:46 -07003800 if (animateIcons) {
3801 // Animate to the correct page
3802 if (newShortcutsScreenId > -1) {
3803 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3804 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3805 if (newShortcutsScreenId != currentScreenId) {
3806 mWorkspace.snapToPage(newScreenIndex);
3807 }
3808 }
3809
Winson Chung64359a52013-07-08 17:17:08 -07003810 // We post the animation slightly delayed to prevent slowdowns when we are loading
3811 // right after we return to launcher.
3812 mWorkspace.postDelayed(new Runnable() {
3813 public void run() {
3814 anim.playTogether(bounceAnims);
3815 anim.start();
3816 }
3817 }, NEW_APPS_ANIMATION_DELAY);
3818 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003819 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003820 }
3821
Joe Onorato9c1289c2009-08-17 11:03:03 -04003822 /**
3823 * Implementation of the method from LauncherModel.Callbacks.
3824 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003825 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003826 Runnable r = new Runnable() {
3827 public void run() {
3828 bindFolders(folders);
3829 }
3830 };
3831 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003832 return;
3833 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003834 sFolders.clear();
3835 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837
3838 /**
3839 * Add the views for a widget to the workspace.
3840 *
3841 * Implementation of the method from LauncherModel.Callbacks.
3842 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003843 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003844 Runnable r = new Runnable() {
3845 public void run() {
3846 bindAppWidget(item);
3847 }
3848 };
3849 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003850 return;
3851 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003852
Daniel Sandler843e8602010-06-07 14:59:01 -04003853 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3854 if (DEBUG_WIDGETS) {
3855 Log.d(TAG, "bindAppWidget: " + item);
3856 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003857 final Workspace workspace = mWorkspace;
3858
3859 final int appWidgetId = item.appWidgetId;
3860 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003861 if (DEBUG_WIDGETS) {
3862 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3863 }
3864
Joe Onorato9c1289c2009-08-17 11:03:03 -04003865 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3866
Joe Onorato9c1289c2009-08-17 11:03:03 -04003867 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003868 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869
Adam Cohendcd297f2013-06-18 13:13:40 -07003870 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003871 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003872 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3873
Joe Onorato9c1289c2009-08-17 11:03:03 -04003874 workspace.requestLayout();
3875
Daniel Sandler843e8602010-06-07 14:59:01 -04003876 if (DEBUG_WIDGETS) {
3877 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3878 + (SystemClock.uptimeMillis()-start) + "ms");
3879 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003880 }
3881
Adam Cohen1462de32012-07-24 22:34:36 -07003882 public void onPageBoundSynchronously(int page) {
3883 mSynchronouslyBoundPages.add(page);
3884 }
3885
Joe Onorato9c1289c2009-08-17 11:03:03 -04003886 /**
3887 * Callback saying that there aren't any more items to bind.
3888 *
3889 * Implementation of the method from LauncherModel.Callbacks.
3890 */
Adam Cohene25af792013-06-06 23:08:25 -07003891 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003892 Runnable r = new Runnable() {
3893 public void run() {
3894 finishBindingItems(upgradePath);
3895 }
3896 };
3897 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003898 return;
3899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900 if (mSavedState != null) {
3901 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003902 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003903 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003904 mSavedState = null;
3905 }
3906
Adam Cohen1462de32012-07-24 22:34:36 -07003907 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003909 // If we received the result of any pending adds while the loader was running (e.g. the
3910 // widget configuration forced an orientation change), process them now.
3911 for (int i = 0; i < sPendingAddList.size(); i++) {
3912 completeAdd(sPendingAddList.get(i));
3913 }
3914 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003915
Winson Chungc51db6a2011-10-05 11:44:49 -07003916 // Update the market app icon as necessary (the other icons will be managed in response to
3917 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003918 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003919
Winson Chungf0c6ae02012-03-21 16:10:31 -07003920 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003921 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003922 mWorkspace.getUniqueComponents(true, null);
3923 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003924 }
Adam Cohen99894d92013-06-14 11:22:59 -07003925
Adam Cohen66a01fd2013-06-11 12:48:00 -07003926 mWorkspace.post(new Runnable() {
3927 @Override
3928 public void run() {
3929 onFinishBindingItems();
3930 }
3931 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003932
3933 // Write all the logs to disk
3934 Launcher.addDumpLog(TAG, "10249126 - finishBindingItems() - dumping logs to disk", true);
3935 dumpLogsToLocalData(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003936 }
3937
Winson Chunga2413752012-04-03 14:22:34 -07003938 private boolean canRunNewAppsAnimation() {
3939 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3940 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3941 }
3942
Winson Chungc9168342013-06-26 14:54:55 -07003943 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3944 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3945 PropertyValuesHolder.ofFloat("alpha", 1f),
3946 PropertyValuesHolder.ofFloat("scaleX", 1f),
3947 PropertyValuesHolder.ofFloat("scaleY", 1f));
3948 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3949 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3950 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3951 return bounceAnim;
3952 }
3953
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003954 @Override
3955 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003956 boolean searchVisible = updateGlobalSearchIcon();
3957 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003958 if (mSearchDropTargetBar != null) {
3959 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3960 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003961 }
3962
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003963 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003964 * Add the icons for all apps.
3965 *
3966 * Implementation of the method from LauncherModel.Callbacks.
3967 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003968 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003969 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3970 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3971 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3972 getHotseat().addAllAppsFolder(mIconCache, apps,
3973 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3974 }
3975 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003976 }
Winson Chungc58497e2013-09-03 17:48:37 -07003977 } else {
3978 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3979 mAppsCustomizeContent != null) {
3980 mAppsCustomizeContent.setApps(apps);
3981 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003983 }
3984
Bjorn Bringert85f418d2013-09-06 12:50:05 +01003985 @Override
3986 public boolean shouldShowApp(ResolveInfo app) {
3987 return true;
3988 }
3989
Joe Onorato9c1289c2009-08-17 11:03:03 -04003990 /**
3991 * A package was updated.
3992 *
3993 * Implementation of the method from LauncherModel.Callbacks.
3994 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003995 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003996 Runnable r = new Runnable() {
3997 public void run() {
3998 bindAppsUpdated(apps);
3999 }
4000 };
4001 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004002 return;
4003 }
4004
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004005 if (mWorkspace != null) {
4006 mWorkspace.updateShortcuts(apps);
4007 }
Winson Chungc58497e2013-09-03 17:48:37 -07004008
4009 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4010 mAppsCustomizeContent != null) {
4011 mAppsCustomizeContent.updateApps(apps);
4012 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004013 }
4014
4015 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004016 * A package was uninstalled. We take both the super set of packageNames
4017 * in addition to specific applications to remove, the reason being that
4018 * this can be called when a package is updated as well. In that scenario,
4019 * we only remove specific components from the workspace, where as
4020 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004021 *
4022 * Implementation of the method from LauncherModel.Callbacks.
4023 */
Winson Chung83892cc2013-05-01 16:53:33 -07004024 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004025 final ArrayList<AppInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07004026 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07004027 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004028 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07004029 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07004030 }
Winson Chungd64d1762013-08-20 14:37:16 -07004031 };
4032 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004033 return;
4034 }
4035
Winson Chung64359a52013-07-08 17:17:08 -07004036 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07004037 mWorkspace.removeItemsByPackageName(packageNames);
4038 } else {
4039 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004040 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004041
Winson Chunga1820962011-10-03 16:31:06 -07004042 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07004043 mDragController.onAppsRemoved(appInfos, this);
Winson Chungc58497e2013-09-03 17:48:37 -07004044
4045 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4046 mAppsCustomizeContent != null) {
4047 mAppsCustomizeContent.removeApps(appInfos);
4048 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004049 }
Joe Onoratobe386092009-11-17 17:32:16 -08004050
4051 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004052 * A number of packages were updated.
4053 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004054 private ArrayList<Object> mWidgetsAndShortcuts;
4055 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004056 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004057 bindPackagesUpdated(mWidgetsAndShortcuts);
4058 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004059 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004060 };
4061
4062 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4063 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4064 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004065 return;
4066 }
4067
Winson Chung64359a52013-07-08 17:17:08 -07004068 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004069 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4070 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004071 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004072 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004073 }
4074
Winson Chung400438b2011-01-16 17:53:48 -08004075 private int mapConfigurationOriActivityInfoOri(int configOri) {
4076 final Display d = getWindowManager().getDefaultDisplay();
4077 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4078 switch (d.getRotation()) {
4079 case Surface.ROTATION_0:
4080 case Surface.ROTATION_180:
4081 // We are currently in the same basic orientation as the natural orientation
4082 naturalOri = configOri;
4083 break;
4084 case Surface.ROTATION_90:
4085 case Surface.ROTATION_270:
4086 // We are currently in the other basic orientation to the natural orientation
4087 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4088 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4089 break;
4090 }
4091
4092 int[] oriMap = {
4093 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4094 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4095 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4096 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4097 };
4098 // Since the map starts at portrait, we need to offset if this device's natural orientation
4099 // is landscape.
4100 int indexOffset = 0;
4101 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4102 indexOffset = 1;
4103 }
4104 return oriMap[(d.getRotation() + indexOffset) % 4];
4105 }
Adam Cohen446e9402011-09-15 18:21:21 -07004106
Winson Chung4b919f82012-05-01 10:44:08 -07004107 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004108 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004109 getResources().getBoolean(R.bool.allow_rotation);
4110 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004111 }
Winson Chung4b919f82012-05-01 10:44:08 -07004112 public void lockScreenOrientation() {
4113 if (isRotationEnabled()) {
4114 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4115 .getConfiguration().orientation));
4116 }
4117 }
4118 public void unlockScreenOrientation(boolean immediate) {
4119 if (isRotationEnabled()) {
4120 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004121 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004122 } else {
4123 mHandler.postDelayed(new Runnable() {
4124 public void run() {
4125 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4126 }
4127 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004128 }
Winson Chung4b919f82012-05-01 10:44:08 -07004129 }
Winson Chung400438b2011-01-16 17:53:48 -08004130 }
4131
Winson Chung82f55532011-08-09 14:14:23 -07004132 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004133 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004134 if (DISABLE_CLINGS) {
4135 return false;
4136 }
4137
Brett Chabot2a9e2282011-08-23 15:03:13 -07004138 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004139 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004140
Michael Jurkae233a8b2013-03-19 13:49:20 +01004141 // Restricted secondary users (child mode) will potentially have very few apps
4142 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004143// boolean supportsLimitedUsers =
4144// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4145// Account[] accounts = AccountManager.get(this).getAccounts();
4146// if (supportsLimitedUsers && accounts.length == 0) {
4147// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4148// Bundle restrictions = um.getUserRestrictions();
4149// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4150// return false;
4151// }
4152// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004153 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004154 }
Michael Jurka22143132012-10-04 17:42:38 +02004155
Winson Chung7d7541e2011-09-16 20:14:36 -07004156 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004157 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004158 if (cling != null) {
4159 cling.init(this, positionData);
4160 cling.setVisibility(View.VISIBLE);
4161 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4162 if (animate) {
4163 cling.buildLayer();
4164 cling.setAlpha(0f);
4165 cling.animate()
4166 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004167 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004168 .setDuration(SHOW_CLING_DURATION)
4169 .setStartDelay(delay)
4170 .start();
4171 } else {
4172 cling.setAlpha(1f);
4173 }
Michael Jurka22143132012-10-04 17:42:38 +02004174 cling.setFocusableInTouchMode(true);
4175 cling.post(new Runnable() {
4176 public void run() {
4177 cling.setFocusable(true);
4178 cling.requestFocus();
4179 }
4180 });
4181 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4182 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004183 }
4184 return cling;
4185 }
Michael Jurka22143132012-10-04 17:42:38 +02004186
Winson Chung7d7541e2011-09-16 20:14:36 -07004187 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004188 // To catch cases where siblings of top-level views are made invisible, just check whether
4189 // the cling is directly set to GONE before dismissing it.
4190 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004191 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004192 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004193 anim.addListener(new AnimatorListenerAdapter() {
4194 public void onAnimationEnd(Animator animation) {
4195 cling.setVisibility(View.GONE);
4196 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004197 // We should update the shared preferences on a background thread
4198 new Thread("dismissClingThread") {
4199 public void run() {
4200 SharedPreferences.Editor editor = mSharedPrefs.edit();
4201 editor.putBoolean(flag, true);
4202 editor.commit();
4203 }
4204 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004205 };
4206 });
4207 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004208 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004209 }
4210 }
Michael Jurka22143132012-10-04 17:42:38 +02004211
Winson Chung9d9d74f2011-09-19 11:49:12 -07004212 private void removeCling(int id) {
4213 final View cling = findViewById(id);
4214 if (cling != null) {
4215 final ViewGroup parent = (ViewGroup) cling.getParent();
4216 parent.post(new Runnable() {
4217 @Override
4218 public void run() {
4219 parent.removeView(cling);
4220 }
4221 });
Michael Jurka22143132012-10-04 17:42:38 +02004222 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004223 }
4224 }
Michael Jurka974c3862012-05-22 22:00:31 -07004225
4226 private boolean skipCustomClingIfNoAccounts() {
4227 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4228 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4229 if (customCling) {
4230 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004231 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004232 Account[] accounts = am.getAccountsByType("com.google");
4233 return accounts.length == 0;
4234 }
4235 return false;
4236 }
4237
Winson Chung7d7541e2011-09-16 20:14:36 -07004238 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004239 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004240 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004241 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4242 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004243 // If we're not using the default workspace layout, replace workspace cling
4244 // with a custom workspace cling (usually specified in an overlay)
4245 // For now, only do this on tablets
4246 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004247 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004248 // Use a custom cling
4249 View cling = findViewById(R.id.workspace_cling);
4250 ViewGroup clingParent = (ViewGroup) cling.getParent();
4251 int clingIndex = clingParent.indexOfChild(cling);
4252 clingParent.removeViewAt(clingIndex);
4253 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4254 clingParent.addView(customCling, clingIndex);
4255 customCling.setId(R.id.workspace_cling);
4256 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004257 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004258 } else {
4259 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004260 }
4261 }
4262 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004263 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004264 if (isClingsEnabled() &&
4265 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004266 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004267 } else {
4268 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004269 }
4270 }
4271 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004272 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004273 if (isClingsEnabled() &&
4274 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4275 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004276 } else {
4277 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004278 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004279 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004280 }
Michael Jurka104c4562013-07-08 18:03:46 -07004281 protected SharedPreferences getSharedPrefs() {
4282 return mSharedPrefs;
4283 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004284 public boolean isFolderClingVisible() {
4285 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004286 if (cling != null) {
4287 return cling.getVisibility() == View.VISIBLE;
4288 }
4289 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004290 }
Winson Chung82f55532011-08-09 14:14:23 -07004291 public void dismissWorkspaceCling(View v) {
4292 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004293 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004294 }
4295 public void dismissAllAppsCling(View v) {
4296 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004297 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4298 }
4299 public void dismissFolderCling(View v) {
4300 Cling cling = (Cling) findViewById(R.id.folder_cling);
4301 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004302 }
4303
Winson Chung80baf5a2010-08-09 16:03:15 -07004304 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004305 * Prints out out state for debugging.
4306 */
4307 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004308 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004309 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004310 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4311 Log.d(TAG, "mRestoring=" + mRestoring);
4312 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4313 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004314 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004315 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004316
Winson Chung785d2eb2011-04-14 16:08:02 -07004317 if (mAppsCustomizeContent != null) {
4318 mAppsCustomizeContent.dumpState();
4319 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004320 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004321 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004322
4323 @Override
4324 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4325 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004326 synchronized (sDumpLogs) {
4327 writer.println(" ");
4328 writer.println("Debug logs: ");
4329 for (int i = 0; i < sDumpLogs.size(); i++) {
4330 writer.println(" " + sDumpLogs.get(i));
4331 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004332 }
4333 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004334
4335 public static void dumpDebugLogsToConsole() {
Adam Cohen4caf2982013-08-20 18:54:31 -07004336 synchronized (sDumpLogs) {
4337 Log.d(TAG, "");
4338 Log.d(TAG, "*********************");
4339 Log.d(TAG, "Launcher debug logs: ");
4340 for (int i = 0; i < sDumpLogs.size(); i++) {
4341 Log.d(TAG, " " + sDumpLogs.get(i));
4342 }
4343 Log.d(TAG, "*********************");
4344 Log.d(TAG, "");
Adam Cohen487f7dd2012-06-28 18:12:10 -07004345 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004346 }
4347
4348 public static void addDumpLog(String tag, String log, boolean debugLog) {
4349 if (debugLog) {
4350 Log.d(tag, log);
4351 }
4352 sDateStamp.setTime(System.currentTimeMillis());
4353 synchronized (sDumpLogs) {
4354 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
4355 }
4356 }
4357
4358 public void dumpLogsToLocalData(final boolean email) {
4359 new Thread("DumpLogsToLocalData") {
4360 @Override
4361 public void run() {
4362 boolean success = false;
4363 sDateStamp.setTime(sRunStart);
4364 String FILENAME = sDateStamp.getMonth() + "-"
4365 + sDateStamp.getDay() + "_"
4366 + sDateStamp.getHours() + "-"
4367 + sDateStamp.getMinutes() + "_"
4368 + sDateStamp.getSeconds() + ".txt";
4369
4370 FileOutputStream fos = null;
4371 File outFile = null;
4372 try {
4373 outFile = new File(getFilesDir(), FILENAME);
4374 outFile.createNewFile();
4375 fos = new FileOutputStream(outFile);
4376 } catch (Exception e) {
4377 e.printStackTrace();
4378 }
4379 if (fos != null) {
4380 PrintWriter writer = new PrintWriter(fos);
4381
4382 writer.println(" ");
4383 writer.println("Debug logs: ");
4384 synchronized (sDumpLogs) {
4385 for (int i = 0; i < sDumpLogs.size(); i++) {
4386 writer.println(" " + sDumpLogs.get(i));
4387 }
4388 }
4389 writer.close();
4390 }
4391 try {
4392 if (fos != null) {
4393 fos.close();
4394 success = true;
4395 }
4396 } catch (IOException e) {
4397 e.printStackTrace();
4398 }
4399
4400 if (success && email) {
4401 if (!emailSent()) {
4402 emailFile(outFile);
4403 }
4404 }
4405 }
4406 }.start();
4407 }
4408
4409 private void emailFile(File file) {
4410 File publicCopy = new File(Environment.getExternalStorageDirectory(), file.getName());
4411 try {
4412 copyFile(file, publicCopy);
4413 } catch (IOException e) {
4414 e.printStackTrace();
4415 return;
4416 }
4417
4418 Intent intent = new Intent(Intent.ACTION_SEND);
4419 intent.setType("text/plain");
4420 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"adamcohen@google.com, winsonc@google.com," +
4421 "mikejurka@google"});
4422 intent.putExtra(Intent.EXTRA_SUBJECT, "Data corruption " + file.getName());
4423 intent.putExtra(Intent.EXTRA_TEXT, "Data corruption has occurred, logs attached");
4424
4425 if (!file.exists() || !file.canRead()) {
4426 Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
4427 finish();
4428 return;
4429 }
4430
4431 Toast.makeText(this, "Data corruption has occurred, please send e-mail", Toast.LENGTH_LONG);
4432 Uri uri = Uri.fromFile(publicCopy);
4433 intent.putExtra(Intent.EXTRA_STREAM, uri);
4434 startActivity(Intent.createChooser(intent, "Please send logs, consider clearing data"));
4435
4436 setEmailSent(true);
4437 }
4438
4439 public void copyFile(File src, File dst) throws IOException {
4440 InputStream in = new FileInputStream(src);
4441 OutputStream out = new FileOutputStream(dst);
4442
4443 // Transfer bytes from in to out
4444 byte[] buf = new byte[1024];
4445 int len;
4446 while ((len = in.read(buf)) > 0) {
4447 out.write(buf, 0, len);
4448 }
4449 in.close();
4450 out.close();
Adam Cohen487f7dd2012-06-28 18:12:10 -07004451 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004452}
Michael Jurka2763be32011-02-24 11:19:57 -08004453
Michael Jurkaabded662011-03-04 12:06:57 -08004454interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004455 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004456 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004457 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004458 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004459 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004460}