blob: daf9f096b7b9def839ff0272b906ebccfeca8a11 [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 Cohendbdff6b2013-09-18 19:09:15 -07001172 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001173
Winson Chung4c98d922011-05-31 16:50:48 -07001174 // Setup the workspace
1175 mWorkspace.setHapticFeedbackEnabled(false);
1176 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001177 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001178 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001179
Winson Chungf0ea4d32011-06-06 14:27:16 -07001180 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001181 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001182
Winson Chungf0ea4d32011-06-06 14:27:16 -07001183 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001184 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001185 mAppsCustomizeContent = (AppsCustomizePagedView)
1186 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1187 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001188
Winson Chung3d503fb2011-07-13 17:25:49 -07001189 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001190 dragController.setDragScoller(mWorkspace);
1191 dragController.setScrollView(mDragLayer);
1192 dragController.setMoveTarget(mWorkspace);
1193 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001194 if (mSearchDropTargetBar != null) {
1195 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001196 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001197
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001198 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001199 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001200 mWeightWatcher = new WeightWatcher(this);
1201 mWeightWatcher.setAlpha(0.5f);
1202 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001203 new FrameLayout.LayoutParams(
1204 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001205 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001206 Gravity.BOTTOM)
1207 );
Adam Cohen39a06042013-07-19 14:30:12 -07001208
1209 boolean show = shouldShowWeightWatcher();
1210 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213
1214 /**
1215 * Creates a view representing a shortcut.
1216 *
1217 * @param info The data structure describing the shortcut.
1218 *
1219 * @return A View inflated from R.layout.application.
1220 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001221 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001223 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225
1226 /**
1227 * Creates a view representing a shortcut inflated from the specified resource.
1228 *
1229 * @param layoutResId The id of the XML layout used to create the shortcut.
1230 * @param parent The group the shortcut belongs to.
1231 * @param info The data structure describing the shortcut.
1232 *
1233 * @return A View inflated from layoutResId.
1234 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001235 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001236 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1237 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 return favorite;
1240 }
1241
1242 /**
1243 * Add an application shortcut to the workspace.
1244 *
1245 * @param data The intent describing the application.
1246 * @param cellInfo The position on screen where to create the shortcut.
1247 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001248 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001249 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001250 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001251
Adam Cohenfbba09b2011-07-18 21:43:05 -07001252 // First we check if we already know the exact location where we want to add this item.
1253 if (cellX >= 0 && cellY >= 0) {
1254 cellXY[0] = cellX;
1255 cellXY[1] = cellY;
1256 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001257 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001258 return;
1259 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001261 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001262
Romain Guy73b979d2009-06-09 12:57:21 -07001263 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001264 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001266 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001267 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001268 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001269 } else {
1270 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272 }
Romain Guycbb89e42009-06-08 15:52:54 -07001273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 /**
1275 * Add a shortcut to the workspace.
1276 *
1277 * @param data The intent describing the shortcut.
1278 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001280 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001281 int cellY) {
1282 int[] cellXY = mTmpAddItemCellCoordinates;
1283 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001284 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001285
Michael Jurkad3ef3062010-11-23 16:23:58 -08001286 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001287
Adam Cohen558baaf2011-08-15 15:22:57 -07001288 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001289 if (info == null) {
1290 return;
1291 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001292 final View view = createShortcut(info);
1293
Adam Cohenfbba09b2011-07-18 21:43:05 -07001294 // First we check if we already know the exact location where we want to add this item.
1295 if (cellX >= 0 && cellY >= 0) {
1296 cellXY[0] = cellX;
1297 cellXY[1] = cellY;
1298 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001299
1300 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001301 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001302 true, null,null)) {
1303 return;
1304 }
1305 DragObject dragObject = new DragObject();
1306 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001307 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1308 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001309 return;
1310 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001311 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001312 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001313 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001314 foundCellSpan = (result != null);
1315 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001316 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001317 }
1318
1319 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001320 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001321 return;
1322 }
1323
Adam Cohendcd297f2013-06-18 13:13:40 -07001324 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
1326 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001327 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001328 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
1330 }
1331
Adam Cohen2f093b62012-04-30 18:59:53 -07001332 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1333 int minHeight) {
1334 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001335 // We want to account for the extra amount of padding that we are adding to the widget
1336 // to ensure that it gets the full amount of space that it has requested
1337 int requiredWidth = minWidth + padding.left + padding.right;
1338 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001339 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001340 }
1341
Adam Cohen2f093b62012-04-30 18:59:53 -07001342 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1343 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001344 }
1345
Adam Cohen2f093b62012-04-30 18:59:53 -07001346 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1347 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001348 }
1349
Adam Cohen2f093b62012-04-30 18:59:53 -07001350 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1351 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001352 }
1353
Adam Cohen2f093b62012-04-30 18:59:53 -07001354 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1355 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001356 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001357 }
1358
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001360 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001362 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001363 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001365 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001366 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1367 if (appWidgetInfo == null) {
1368 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1369 }
Romain Guycbb89e42009-06-08 15:52:54 -07001370
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001371 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001372 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001373
Adam Cohen2f093b62012-04-30 18:59:53 -07001374 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1375 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001378 // We have saved the position to which the widget was dragged-- this really only matters
1379 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001380 int[] cellXY = mTmpAddItemCellCoordinates;
1381 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001382 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001383 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001384 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1385 cellXY[0] = mPendingAddInfo.cellX;
1386 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001387 spanXY[0] = mPendingAddInfo.spanX;
1388 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001389 foundCellSpan = true;
1390 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001391 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001392 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001393 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1394 spanXY[1], cellXY, finalSpan);
1395 spanXY[0] = finalSpan[0];
1396 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001397 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001398 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001399 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001400 }
1401
Michael Jurka0280c3b2010-09-17 15:00:07 -07001402 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001403 if (appWidgetId != -1) {
1404 // Deleting an app widget ID is a void call but writes to disk before returning
1405 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001406 new Thread("deleteAppWidgetId") {
1407 public void run() {
1408 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1409 }
1410 }.start();
1411 }
Winson Chung93eef082012-03-23 15:59:27 -07001412 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001413 return;
1414 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001416 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001417 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1418 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001419 launcherInfo.spanX = spanXY[0];
1420 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001421 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1422 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001423
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001425 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426
1427 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001428 if (hostView == null) {
1429 // Perform actual inflation because we're live
1430 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1431 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1432 } else {
1433 // The AppWidgetHostView has already been inflated and instantiated
1434 launcherInfo.hostView = hostView;
1435 }
Romain Guycbb89e42009-06-08 15:52:54 -07001436
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001438 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001439 launcherInfo.notifyWidgetSizeChanged(this);
1440
Adam Cohendcd297f2013-06-18 13:13:40 -07001441 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001443
1444 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001446 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
Romain Guycbb89e42009-06-08 15:52:54 -07001448
Adam Cohended9f8d2010-11-03 13:25:16 -07001449 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1450 @Override
1451 public void onReceive(Context context, Intent intent) {
1452 final String action = intent.getAction();
1453 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1454 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001455 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001456 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001457
Winson Chungc100e8e2011-08-09 16:02:43 -07001458 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001459 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001460 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1461 mAppsCustomizeTabHost.reset();
Adam Cohen040a5d22013-09-16 17:09:33 -07001462 showWorkspaceAndExitOverviewMode(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001463 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001464 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1465 mUserPresent = true;
1466 updateRunning();
1467 }
1468 }
1469 };
1470
1471 @Override
1472 public void onAttachedToWindow() {
1473 super.onAttachedToWindow();
1474
1475 // Listen for broadcasts related to user-presence
1476 final IntentFilter filter = new IntentFilter();
1477 filter.addAction(Intent.ACTION_SCREEN_OFF);
1478 filter.addAction(Intent.ACTION_USER_PRESENT);
1479 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001480 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001481 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001482 mVisible = true;
1483 }
1484
1485 @Override
1486 public void onDetachedFromWindow() {
1487 super.onDetachedFromWindow();
1488 mVisible = false;
1489
Adam Cohend113e0c2010-11-11 10:48:05 -08001490 if (mAttached) {
1491 unregisterReceiver(mReceiver);
1492 mAttached = false;
1493 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001494 updateRunning();
1495 }
1496
1497 public void onWindowVisibilityChanged(int visibility) {
1498 mVisible = visibility == View.VISIBLE;
1499 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001500 // The following code used to be in onResume, but it turns out onResume is called when
1501 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1502 // is a more appropriate event to handle
1503 if (mVisible) {
1504 mAppsCustomizeTabHost.onWindowVisible();
1505 if (!mWorkspaceLoading) {
1506 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001507 // We want to let Launcher draw itself at least once before we force it to build
1508 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001509 // apps is nice and speedy.
1510 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001511 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001512 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001513 if (mStarted) return;
1514 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001515 // We delay the layer building a bit in order to give
1516 // other message processing a time to run. In particular
1517 // this avoids a delay in hiding the IME if it was
1518 // currently shown, because doing that may involve
1519 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001520 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001521 final ViewTreeObserver.OnDrawListener listener = this;
1522 mWorkspace.post(new Runnable() {
1523 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001524 if (mWorkspace != null &&
1525 mWorkspace.getViewTreeObserver() != null) {
1526 mWorkspace.getViewTreeObserver().
1527 removeOnDrawListener(listener);
1528 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001529 }
1530 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001531 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001532 }
1533 });
1534 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001535 // When Launcher comes back to foreground, a different Activity might be responsible for
1536 // the app market intent, so refresh the icon
1537 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001538 clearTypedText();
1539 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001540 }
1541
1542 private void sendAdvanceMessage(long delay) {
1543 mHandler.removeMessages(ADVANCE_MSG);
1544 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1545 mHandler.sendMessageDelayed(msg, delay);
1546 mAutoAdvanceSentTime = System.currentTimeMillis();
1547 }
1548
1549 private void updateRunning() {
1550 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1551 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1552 mAutoAdvanceRunning = autoAdvanceRunning;
1553 if (autoAdvanceRunning) {
1554 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1555 sendAdvanceMessage(delay);
1556 } else {
1557 if (!mWidgetsToAdvance.isEmpty()) {
1558 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1559 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1560 }
1561 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001562 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001563 }
1564 }
1565 }
1566
1567 private final Handler mHandler = new Handler() {
1568 @Override
1569 public void handleMessage(Message msg) {
1570 if (msg.what == ADVANCE_MSG) {
1571 int i = 0;
1572 for (View key: mWidgetsToAdvance.keySet()) {
1573 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1574 final int delay = mAdvanceStagger * i;
1575 if (v instanceof Advanceable) {
1576 postDelayed(new Runnable() {
1577 public void run() {
1578 ((Advanceable) v).advance();
1579 }
1580 }, delay);
1581 }
1582 i++;
1583 }
1584 sendAdvanceMessage(mAdvanceInterval);
1585 }
1586 }
1587 };
1588
1589 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001590 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001591 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1592 if (v instanceof Advanceable) {
1593 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001594 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001595 updateRunning();
1596 }
1597 }
1598
1599 void removeWidgetToAutoAdvance(View hostView) {
1600 if (mWidgetsToAdvance.containsKey(hostView)) {
1601 mWidgetsToAdvance.remove(hostView);
1602 updateRunning();
1603 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001604 }
1605
Joe Onorato9c1289c2009-08-17 11:03:03 -04001606 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001608 launcherInfo.hostView = null;
1609 }
1610
Winson Chung93eef082012-03-23 15:59:27 -07001611 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1612 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1613 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 }
1615
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001616 public LauncherAppWidgetHost getAppWidgetHost() {
1617 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 }
Romain Guycbb89e42009-06-08 15:52:54 -07001619
Winson Chunga9abd0e2010-10-27 17:18:37 -07001620 public LauncherModel getModel() {
1621 return mModel;
1622 }
1623
Bjorn Bringertc459e522013-06-07 19:36:01 +01001624 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001625 getWindow().closeAllPanels();
1626
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001627 // Whatever we were doing is hereby canceled.
1628 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001629 }
1630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 @Override
1632 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001633 long startTime = 0;
1634 if (DEBUG_RESUME_TIME) {
1635 startTime = System.currentTimeMillis();
1636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 super.onNewIntent(intent);
1638
1639 // Close the menu
1640 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001641 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001642 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001643
Jamie Genniscb222e82012-10-23 15:44:26 -07001644 final boolean alreadyOnHome =
1645 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001646 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001647
Jamie Genniscb222e82012-10-23 15:44:26 -07001648 Runnable processIntent = new Runnable() {
1649 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001650 if (mWorkspace == null) {
1651 // Can be cases where mWorkspace is null, this prevents a NPE
1652 return;
1653 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001654 Folder openFolder = mWorkspace.getOpenFolder();
1655 // In all these cases, only animate if we're already on home
1656 mWorkspace.exitWidgetResizeMode();
1657 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1658 openFolder == null) {
1659 mWorkspace.moveToDefaultScreen(true);
1660 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001661
Jamie Genniscb222e82012-10-23 15:44:26 -07001662 closeFolder();
1663 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001664
Jamie Genniscb222e82012-10-23 15:44:26 -07001665 // If we are already on home, then just animate back to the workspace,
1666 // otherwise, just wait until onResume to set the state back to Workspace
1667 if (alreadyOnHome) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001668 showWorkspaceAndExitOverviewMode(true);
Jamie Genniscb222e82012-10-23 15:44:26 -07001669 } else {
1670 mOnResumeState = State.WORKSPACE;
1671 }
1672
1673 final View v = getWindow().peekDecorView();
1674 if (v != null && v.getWindowToken() != null) {
1675 InputMethodManager imm = (InputMethodManager)getSystemService(
1676 INPUT_METHOD_SERVICE);
1677 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1678 }
1679
1680 // Reset AllApps to its initial state
1681 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1682 mAppsCustomizeTabHost.reset();
1683 }
1684 }
1685 };
1686
1687 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1688 // Delay processing of the intent to allow the status bar animation to finish
1689 // first in order to avoid janky animations.
1690 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001691 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001692 // Process the intent immediately.
1693 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001694 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 }
Michael Jurka447bf842013-05-15 14:52:15 +02001697 if (DEBUG_RESUME_TIME) {
1698 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 }
1701
Adam Cohen040a5d22013-09-16 17:09:33 -07001702 protected void showWorkspaceAndExitOverviewMode(boolean animate) {
1703 showWorkspace(animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001704 if (mWorkspace.isInOverviewMode()) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001705 mWorkspace.exitOverviewMode(animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001706 }
1707 }
Adam Cohen040a5d22013-09-16 17:09:33 -07001708 protected void showWorkspaceAndExitOverviewMode() {
1709 showWorkspaceAndExitOverviewMode(true);
1710 }
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001711
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001713 public void onRestoreInstanceState(Bundle state) {
1714 super.onRestoreInstanceState(state);
1715 for (int page: mSynchronouslyBoundPages) {
1716 mWorkspace.restoreInstanceStateForChild(page);
1717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 }
1719
1720 @Override
1721 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001722 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001723 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724
Michael Jurka883f55b2010-10-21 15:47:14 -07001725 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001726 // We close any open folder since it will not be re-opened, and we need to make sure
1727 // this state is reflected.
1728 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729
Adam Cohendcd297f2013-06-18 13:13:40 -07001730 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001731 mWaitingForResult) {
1732 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001733 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001734 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1735 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001736 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1737 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1738 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 }
1740
1741 if (mFolderInfo != null && mWaitingForResult) {
1742 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1743 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1744 }
Michael Jurka946ad472010-07-09 18:05:18 -07001745
Winson Chung785d2eb2011-04-14 16:08:02 -07001746 // Save the current AppsCustomize tab
1747 if (mAppsCustomizeTabHost != null) {
1748 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1749 if (currentTabTag != null) {
1750 outState.putString("apps_customize_currentTab", currentTabTag);
1751 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001752 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1753 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 }
1756
1757 @Override
1758 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001760
Winson Chunge7a03942011-08-05 15:05:12 -07001761 // Remove all pending runnables
1762 mHandler.removeMessages(ADVANCE_MSG);
1763 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001764 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001765
Winson Chungcd2b0142011-06-08 16:02:26 -07001766 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001767 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001768 mModel.stopLoader();
1769 app.setLauncher(null);
1770
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001772 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001774 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001776 mAppWidgetHost = null;
1777
1778 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779
1780 TextKeyListener.getInstance().release();
1781
Winson Chung81b52252012-08-27 15:34:29 -07001782 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1783 // to prevent leaking Launcher activities on orientation change.
1784 if (mModel != null) {
1785 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1786 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001787
1788 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001789 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001790
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001791 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001792 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1793 mWorkspace.removeAllViews();
1794 mWorkspace = null;
1795 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001796
Michael Jurka2ecf9952012-06-18 12:52:28 -07001797 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
1799
Adam Cohena9cf38f2011-05-02 15:36:58 -07001800 public DragController getDragController() {
1801 return mDragController;
1802 }
1803
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 @Override
1805 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001806 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 super.startActivityForResult(intent, requestCode);
1808 }
1809
Winson Chung70d72102011-08-12 11:24:35 -07001810 /**
1811 * Indicates that we want global search for this activity by setting the globalSearch
1812 * argument for {@link #startSearch} to true.
1813 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001815 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001817
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001818 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001819
Karl Rosaen138a0412009-04-23 19:00:21 -07001820 if (initialQuery == null) {
1821 // Use any text typed in the launcher as the initial query
1822 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001823 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 if (appSearchData == null) {
1825 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001826 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 }
Winson Chungadf0c182012-08-23 14:59:07 -07001828 Rect sourceBounds = new Rect();
1829 if (mSearchDropTargetBar != null) {
1830 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1831 }
Romain Guycbb89e42009-06-08 15:52:54 -07001832
Bjorn Bringertc459e522013-06-07 19:36:01 +01001833 startSearch(initialQuery, selectInitialQuery,
1834 appSearchData, sourceBounds);
1835 }
1836
1837 public void startSearch(String initialQuery,
1838 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001839 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001840 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001841 }
1842
1843 /**
1844 * Starts the global search activity. This code is a copied from SearchManager
1845 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001846 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001847 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001848 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001849 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1850 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1851 if (globalSearchActivity == null) {
1852 Log.w(TAG, "No global search activity found.");
1853 return;
1854 }
1855 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1856 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1857 intent.setComponent(globalSearchActivity);
1858 // Make sure that we have a Bundle to put source in
1859 if (appSearchData == null) {
1860 appSearchData = new Bundle();
1861 } else {
1862 appSearchData = new Bundle(appSearchData);
1863 }
1864 // Set source to package name of app that starts global search, if not set already.
1865 if (!appSearchData.containsKey("source")) {
1866 appSearchData.putString("source", getPackageName());
1867 }
1868 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1869 if (!TextUtils.isEmpty(initialQuery)) {
1870 intent.putExtra(SearchManager.QUERY, initialQuery);
1871 }
1872 if (selectInitialQuery) {
1873 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1874 }
1875 intent.setSourceBounds(sourceBounds);
1876 try {
1877 startActivity(intent);
1878 } catch (ActivityNotFoundException ex) {
1879 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1880 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 }
1882
Winson Chung70d72102011-08-12 11:24:35 -07001883 @Override
1884 public boolean onCreateOptionsMenu(Menu menu) {
1885 if (isWorkspaceLocked()) {
1886 return false;
1887 }
1888
1889 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001890
1891 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1892 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1893 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001894 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1895 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1896 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001897 String helpUrl = getString(R.string.help_url);
1898 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001899 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1900 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1901
Winson Chung70d72102011-08-12 11:24:35 -07001902 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1903 .setIcon(android.R.drawable.ic_menu_gallery)
1904 .setAlphabeticShortcut('W');
1905 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1906 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001907 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001908 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001909 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1910 .setIcon(android.R.drawable.ic_menu_preferences)
1911 .setIntent(settings)
1912 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001913 if (!helpUrl.isEmpty()) {
1914 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1915 .setIcon(android.R.drawable.ic_menu_help)
1916 .setIntent(help)
1917 .setAlphabeticShortcut('H');
1918 }
Winson Chung70d72102011-08-12 11:24:35 -07001919 return true;
1920 }
1921
1922 @Override
1923 public boolean onPrepareOptionsMenu(Menu menu) {
1924 super.onPrepareOptionsMenu(menu);
1925
1926 if (mAppsCustomizeTabHost.isTransitioning()) {
1927 return false;
1928 }
1929 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1930 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1931
1932 return true;
1933 }
1934
1935 @Override
1936 public boolean onOptionsItemSelected(MenuItem item) {
1937 switch (item.getItemId()) {
1938 case MENU_WALLPAPER_SETTINGS:
1939 startWallpaper();
1940 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001941 }
1942
1943 return super.onOptionsItemSelected(item);
1944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001946 @Override
1947 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001948 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001949 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001950 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001951 }
1952
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 public boolean isWorkspaceLocked() {
1954 return mWorkspaceLoading || mWaitingForResult;
1955 }
1956
Michael Jurka0280c3b2010-09-17 15:00:07 -07001957 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001958 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001959 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001960 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1961 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001962 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001963 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001964 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001965
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001966 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1967 AppWidgetProviderInfo appWidgetInfo) {
1968 if (appWidgetInfo.configure != null) {
1969 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001970
Bjorn Bringert7984c942009-12-09 15:38:25 +00001971 // Launch over to configure widget, if needed
1972 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001973 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001974 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001975 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001977 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001978 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001979 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001980 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001981 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
1983 }
Romain Guycbb89e42009-06-08 15:52:54 -07001984
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001985 protected void moveToCustomContentScreen(boolean animate) {
1986 mWorkspace.moveToCustomContentScreen(animate);
1987 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001988 /**
1989 * Process a shortcut drop.
1990 *
1991 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001992 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001993 * @param cell The cell it should be added to, optional
1994 * @param position The location on the screen where it was dropped, optional
1995 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001996 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001997 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001998 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001999 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002000 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002001 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002002
2003 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002004 mPendingAddInfo.cellX = cell[0];
2005 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002006 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002007
2008 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2009 createShortcutIntent.setComponent(componentName);
2010 processShortcut(createShortcutIntent);
2011 }
2012
Adam Cohenfbba09b2011-07-18 21:43:05 -07002013 /**
2014 * Process a widget drop.
2015 *
2016 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002017 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002018 * @param cell The cell it should be added to, optional
2019 * @param position The location on the screen where it was dropped, optional
2020 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002021 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002022 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002023 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002024 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002025 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002026 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002027 mPendingAddInfo.minSpanX = info.minSpanX;
2028 mPendingAddInfo.minSpanY = info.minSpanY;
2029
Adam Cohenfbba09b2011-07-18 21:43:05 -07002030 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002031 mPendingAddInfo.cellX = cell[0];
2032 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002033 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002034 if (span != null) {
2035 mPendingAddInfo.spanX = span[0];
2036 mPendingAddInfo.spanY = span[1];
2037 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002038
Adam Cohened66b2b2012-01-23 17:28:51 -08002039 AppWidgetHostView hostView = info.boundWidget;
2040 int appWidgetId;
2041 if (hostView != null) {
2042 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002043 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002044 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002045 // In this case, we either need to start an activity to get permission to bind
2046 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002047 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002048 Bundle options = info.bindOptions;
2049
2050 boolean success = false;
2051 if (options != null) {
2052 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2053 info.componentName, options);
2054 } else {
2055 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2056 info.componentName);
2057 }
2058 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002059 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002060 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002061 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002062 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2063 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2064 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002065 // TODO: we need to make sure that this accounts for the options bundle.
2066 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002067 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2068 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002069 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002070 }
2071
Joe Onoratodeb98af2010-02-19 14:59:39 -08002072 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002073 // Handle case where user selected "Applications"
2074 String applicationName = getResources().getString(R.string.group_applications);
2075 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002076
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002077 if (applicationName != null && applicationName.equals(shortcutName)) {
2078 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2079 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002080
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002081 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2082 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002083 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002084 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002085 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002086 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 }
2089
Winson Chung24ab2f12010-09-16 14:10:47 -07002090 void processWallpaper(Intent intent) {
2091 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2092 }
2093
Adam Cohendcd297f2013-06-18 13:13:40 -07002094 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002095 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002096 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 folderInfo.title = getText(R.string.folder_name);
2098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002100 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002101 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002102 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103
2104 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002105 FolderIcon newFolder =
2106 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002107 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002108 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002109 // Force measure the new folder icon
2110 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2111 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002112 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
Romain Guycbb89e42009-06-08 15:52:54 -07002114
Joe Onorato9c1289c2009-08-17 11:03:03 -04002115 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002116 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002117 }
2118
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002119 protected void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002120 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002122 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002123 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 }
2125
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002126 protected ComponentName getWallpaperPickerComponent() {
2127 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2128 }
2129
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002130 /**
2131 * Registers various content observers. The current implementation registers
2132 * only a favorites observer to keep track of the favorites applications.
2133 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002134 private void registerContentObservers() {
2135 ContentResolver resolver = getContentResolver();
2136 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2137 true, mWidgetObserver);
2138 }
2139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 @Override
2141 public boolean dispatchKeyEvent(KeyEvent event) {
2142 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2143 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002145 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002146 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002147 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002148 dumpState();
2149 return true;
2150 }
2151 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002152 }
2153 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2154 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002155 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 return true;
2157 }
2158 }
2159
2160 return super.dispatchKeyEvent(event);
2161 }
2162
Joe Onorato88ec0992009-11-19 13:16:06 -08002163 @Override
2164 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002165 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002166 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002167 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002168 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002169 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002170 Folder openFolder = mWorkspace.getOpenFolder();
2171 if (openFolder.isEditingName()) {
2172 openFolder.dismissEditingName();
2173 } else {
2174 closeFolder();
2175 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002176 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002177 mWorkspace.exitWidgetResizeMode();
2178
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002179 // Back button is a no-op here, but give at least some feedback for the button press
2180 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002181 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002182 }
2183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002185 * Re-listen when widgets are reset.
2186 */
2187 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002188 if (mAppWidgetHost != null) {
2189 mAppWidgetHost.startListening();
2190 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002191 }
2192
2193 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 * Launches the intent referred by the clicked shortcut.
2195 *
2196 * @param v The view representing the clicked shortcut.
2197 */
2198 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002199 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2200 // view has detached (it's possible for this to happen if the view is removed mid touch).
2201 if (v.getWindowToken() == null) {
2202 return;
2203 }
2204
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002205 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002206 return;
2207 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002208
Adam Cohen1697b792013-09-17 19:08:21 -07002209 if (v instanceof Workspace) {
2210 if (mWorkspace.isInOverviewMode()) {
2211 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002212 }
2213 return;
2214 }
2215
2216 if (v instanceof CellLayout) {
2217 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002218 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002219 }
2220 }
2221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002223 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002225 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2226 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002227
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002228 // Check for special shortcuts
2229 if (intent.getComponent() != null) {
2230 final String shortcutClass = intent.getComponent().getClassName();
2231
2232 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chungc58497e2013-09-03 17:48:37 -07002233 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002234 return;
2235 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2236 MemoryDumpActivity.startDump(this);
2237 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002238 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2239 toggleShowWeightWatcher();
2240 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002241 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002242 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002243
2244 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002245 int[] pos = new int[2];
2246 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002247 intent.setSourceBounds(new Rect(pos[0], pos[1],
2248 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002249
2250 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002251
Daniel Sandlerff02d492013-08-05 02:12:05 -04002252 mStats.recordLaunch(intent, shortcut);
2253
Michael Jurkaddd62e92011-02-16 17:49:14 -08002254 if (success && v instanceof BubbleTextView) {
2255 mWaitingForResume = (BubbleTextView) v;
2256 mWaitingForResume.setStayPressed(true);
2257 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002259 if (v instanceof FolderIcon) {
2260 FolderIcon fi = (FolderIcon) v;
2261 handleFolderClick(fi);
2262 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002263 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002264 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002265 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002266 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002267 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 }
2270 }
2271
Michael Jurka0e260592010-06-30 17:07:39 -07002272 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002273 return false;
2274 }
2275
Michael Jurkaaf442092010-06-10 17:01:57 -07002276 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002277 * Event handler for the search button
2278 *
2279 * @param v The view that was clicked.
2280 */
2281 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002282 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2283
Amith Yamasania135ba82011-08-09 17:42:01 -07002284 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002285 }
2286
2287 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002288 * Event handler for the voice button
2289 *
2290 * @param v The view that was clicked.
2291 */
2292 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002293 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002294
Bjorn Bringertc459e522013-06-07 19:36:01 +01002295 startVoice();
2296 }
2297
2298 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002299 try {
2300 final SearchManager searchManager =
2301 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2302 ComponentName activityName = searchManager.getGlobalSearchActivity();
2303 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002304 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002305 if (activityName != null) {
2306 intent.setPackage(activityName.getPackageName());
2307 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002308 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002309 } catch (ActivityNotFoundException e) {
2310 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002311 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002312 startActivitySafely(null, intent, "onClickVoiceButton");
2313 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002314 }
2315
2316 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002317 * Event handler for the "grid" button that appears on the home screen, which
2318 * enters all apps mode.
2319 *
2320 * @param v The view that was clicked.
2321 */
2322 public void onClickAllAppsButton(View v) {
Winson Chungc58497e2013-09-03 17:48:37 -07002323 showAllApps(true, AppsCustomizePagedView.ContentType.Applications);
Michael Jurka5130e402011-10-13 04:55:35 -07002324 }
2325
2326 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002327 // Provide the same haptic feedback that the system offers for virtual keys.
2328 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002329 }
2330
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002331 public void onClickAppMarketButton(View v) {
2332 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002333 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002334 } else {
2335 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002336 }
2337 }
2338
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002339 /**
2340 * Called when the user stops interacting with the launcher.
2341 * This implies that the user is now on the homescreen and is not doing housekeeping.
2342 */
2343 protected void onInteractionEnd() {}
2344
2345 /**
2346 * Called when the user starts interacting with the launcher.
2347 * The possible interactions are:
2348 * - open all apps
2349 * - reorder an app shortcut, or a widget
2350 * - open the overview mode.
2351 * This is a good time to stop doing things that only make sense
2352 * when the user is on the homescreen and not doing housekeeping.
2353 */
2354 protected void onInteractionBegin() {}
2355
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002356 void startApplicationDetailsActivity(ComponentName componentName) {
2357 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002358 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2359 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002360 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002361 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002362 }
2363
Michael Jurka1e2f4652013-07-08 18:03:46 -07002364 // returns true if the activity was started
2365 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002366 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002367 // System applications cannot be installed. For now, show a toast explaining that.
2368 // We may give them the option of disabling apps this way.
2369 int messageId = R.string.uninstall_system_app_text;
2370 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002371 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002372 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002373 String packageName = componentName.getPackageName();
2374 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002375 Intent intent = new Intent(
2376 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002377 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2378 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002379 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002380 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002381 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002382 }
2383
Michael Jurka86a720e2012-05-09 11:23:23 -07002384 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002388 // Only launch using the new animation if the shortcut has not opted out (this is a
2389 // private contract between launcher and may be ignored in the future).
2390 boolean useLaunchAnimation = (v != null) &&
2391 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2392 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002393 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2394 v.getMeasuredWidth(), v.getMeasuredHeight());
2395
2396 startActivity(intent, opts.toBundle());
2397 } else {
2398 startActivity(intent);
2399 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002400 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 } catch (SecurityException e) {
2402 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002403 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002405 "or use the exported attribute for this activity. "
2406 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002408 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002410
Michael Jurka86a720e2012-05-09 11:23:23 -07002411 boolean startActivitySafely(View v, Intent intent, Object tag) {
2412 boolean success = false;
2413 try {
2414 success = startActivity(v, intent, tag);
2415 } catch (ActivityNotFoundException e) {
2416 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2417 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2418 }
2419 return success;
2420 }
2421
Adam Cohena9cf38f2011-05-02 15:36:58 -07002422 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002423 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002424 Folder openFolder = mWorkspace.getFolderForTag(info);
2425
2426 // If the folder info reports that the associated folder is open, then verify that
2427 // it is actually opened. There have been a few instances where this gets out of sync.
2428 if (info.opened && openFolder == null) {
2429 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002430 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002431 info.opened = false;
2432 }
2433
Adam Cohenfb91f302012-06-11 15:45:18 -07002434 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 // Close any open folder
2436 closeFolder();
2437 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002438 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 } else {
2440 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 int folderScreen;
2442 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002443 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 // .. and close it
2445 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002446 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 // Close any folder open on the current screen
2448 closeFolder();
2449 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002450 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 }
2452 }
2453 }
2454 }
2455
Adam Cohen268c4752012-06-06 17:47:33 -07002456 /**
2457 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2458 * in the DragLayer in the exact absolute location of the original FolderIcon.
2459 */
2460 private void copyFolderIconToImage(FolderIcon fi) {
2461 final int width = fi.getMeasuredWidth();
2462 final int height = fi.getMeasuredHeight();
2463
2464 // Lazy load ImageView, Bitmap and Canvas
2465 if (mFolderIconImageView == null) {
2466 mFolderIconImageView = new ImageView(this);
2467 }
2468 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2469 mFolderIconBitmap.getHeight() != height) {
2470 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2471 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2472 }
2473
2474 DragLayer.LayoutParams lp;
2475 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2476 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2477 } else {
2478 lp = new DragLayer.LayoutParams(width, height);
2479 }
2480
Adam Cohen307fe232012-08-16 17:55:58 -07002481 // The layout from which the folder is being opened may be scaled, adjust the starting
2482 // view size by this scale factor.
2483 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002484 lp.customPosition = true;
2485 lp.x = mRectForFolderAnimation.left;
2486 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002487 lp.width = (int) (scale * width);
2488 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002489
2490 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2491 fi.draw(mFolderIconCanvas);
2492 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002493 if (fi.getFolder() != null) {
2494 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2495 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002496 }
Adam Cohen268c4752012-06-06 17:47:33 -07002497 // Just in case this image view is still in the drag layer from a previous animation,
2498 // we remove it and re-add it.
2499 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2500 mDragLayer.removeView(mFolderIconImageView);
2501 }
2502 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002503 if (fi.getFolder() != null) {
2504 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002505 }
Adam Cohen268c4752012-06-06 17:47:33 -07002506 }
2507
Adam Cohen2801caf2011-05-13 20:57:39 -07002508 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002509 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002510 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2511 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2512 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2513
Adam Cohenc51934b2011-07-26 21:07:43 -07002514 FolderInfo info = (FolderInfo) fi.getTag();
2515 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2516 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002517 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2518 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002519 }
2520
Adam Cohen268c4752012-06-06 17:47:33 -07002521 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2522 copyFolderIconToImage(fi);
2523 fi.setVisibility(View.INVISIBLE);
2524
Michael Jurka2ecf9952012-06-18 12:52:28 -07002525 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002526 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002527 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2528 oa.start();
2529 }
2530
Adam Cohen268c4752012-06-06 17:47:33 -07002531 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002532 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002533 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2534 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2535 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2536
Adam Cohen268c4752012-06-06 17:47:33 -07002537 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002538
Adam Cohen268c4752012-06-06 17:47:33 -07002539 // We remove and re-draw the FolderIcon in-case it has changed
2540 mDragLayer.removeView(mFolderIconImageView);
2541 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002542 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002543 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002544 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002545 oa.addListener(new AnimatorListenerAdapter() {
2546 @Override
2547 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002548 if (cl != null) {
2549 cl.clearFolderLeaveBehind();
2550 // Remove the ImageView copy of the FolderIcon and make the original visible.
2551 mDragLayer.removeView(mFolderIconImageView);
2552 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002553 }
2554 }
2555 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002556 oa.start();
2557 }
2558
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002560 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 * is animated relative to the specified View. If the View is null, no animation
2562 * is played.
2563 *
2564 * @param folderInfo The FolderInfo describing the folder to open.
2565 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002566 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002567 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002568 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569
Adam Cohena9cf38f2011-05-02 15:36:58 -07002570 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571
Adam Cohen4554ee12011-08-03 16:13:21 -07002572 // Just verify that the folder hasn't already been added to the DragLayer.
2573 // There was a one-off crash where the folder had a parent already.
2574 if (folder.getParent() == null) {
2575 mDragLayer.addView(folder);
2576 mDragController.addDropTarget((DropTarget) folder);
2577 } else {
2578 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2579 folder.getParent() + ").");
2580 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002581 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002582 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002583
2584 // Notify the accessibility manager that this folder "window" has appeared and occluded
2585 // the workspace items
2586 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2587 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002588 }
2589
2590 public void closeFolder() {
2591 Folder folder = mWorkspace.getOpenFolder();
2592 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002593 if (folder.isEditingName()) {
2594 folder.dismissEditingName();
2595 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002596 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002597
2598 // Dismiss the folder cling
2599 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002600 }
2601 }
2602
2603 void closeFolder(Folder folder) {
2604 folder.getInfo().opened = false;
2605
2606 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2607 if (parent != null) {
2608 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2609 shrinkAndFadeInFolderIcon(fi);
2610 }
2611 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002612
2613 // Notify the accessibility manager that this folder "window" has disappeard and no
2614 // longer occludeds the workspace items
2615 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002616 }
2617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002618 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002619 if (!isDraggingEnabled()) return false;
2620 if (isWorkspaceLocked()) return false;
2621 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622
Adam Cohen1697b792013-09-17 19:08:21 -07002623 if (v instanceof Workspace) {
2624 if (!mWorkspace.isInOverviewMode()) {
2625 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2626 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2627 mWorkspace.enterOverviewMode();
2628 }
2629 return true;
2630 }
2631
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002632 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002633 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 }
2635
Michael Jurka0280c3b2010-09-17 15:00:07 -07002636 resetAddInfo();
2637 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002638 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002639 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002640 return true;
2641 }
2642
Winson Chung3d503fb2011-07-13 17:25:49 -07002643 // The hotseat touch handling does not go through Workspace, and we always allow long press
2644 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002645 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002646 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2647 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002648 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002649 // User long pressed on empty space
2650 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2651 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2652 // Disabling reordering until we sort out some issues.
2653 if (mWorkspace.isInOverviewMode()) {
2654 mWorkspace.startReordering(v);
2655 } else {
2656 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002659 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002661 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002662 }
2663 }
2664 }
2665 return true;
2666 }
2667
Winson Chung3d503fb2011-07-13 17:25:49 -07002668 boolean isHotseatLayout(View layout) {
2669 return mHotseat != null && layout != null &&
2670 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2671 }
2672 Hotseat getHotseat() {
2673 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002674 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002675 View getOverviewPanel() {
2676 return mOverviewPanel;
2677 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002678 SearchDropTargetBar getSearchBar() {
2679 return mSearchDropTargetBar;
2680 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002681
Winson Chung3d503fb2011-07-13 17:25:49 -07002682 /**
2683 * Returns the CellLayout of the specified container at the specified screen.
2684 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002685 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002686 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2687 if (mHotseat != null) {
2688 return mHotseat.getLayout();
2689 } else {
2690 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002691 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002692 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002693 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002694 }
2695 }
2696
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 Workspace getWorkspace() {
2698 return mWorkspace;
2699 }
2700
Daniel Sandler843e8602010-06-07 14:59:01 -04002701 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002702 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002703 }
2704
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002705 /**
2706 * Helper method for the cameraZoomIn/cameraZoomOut animations
2707 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002708 * @param scaleFactor The scale factor used for the zoom
2709 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002710 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002711 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002712 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002713 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002714
Winson Chung18f41f82012-05-09 13:28:10 -07002715 void disableWallpaperIfInAllApps() {
2716 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002717 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002718 if (mAppsCustomizeTabHost != null &&
2719 !mAppsCustomizeTabHost.isTransitioning()) {
2720 updateWallpaperVisibility(false);
2721 }
2722 }
2723 }
2724
Craig Mautner360310b2012-10-26 15:13:08 -07002725 private void setWorkspaceBackground(boolean workspace) {
2726 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002727 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002728 }
2729
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002730 void updateWallpaperVisibility(boolean visible) {
2731 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2732 int curflags = getWindow().getAttributes().flags
2733 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2734 if (wpflags != curflags) {
2735 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2736 }
Craig Mautner360310b2012-10-26 15:13:08 -07002737 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002738 }
2739
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002740 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2741 if (v instanceof LauncherTransitionable) {
2742 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2743 }
2744 }
2745
Michael Jurkabed61d22012-02-14 22:51:29 -08002746 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2747 if (v instanceof LauncherTransitionable) {
2748 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2749 }
Winson Chung70442722012-02-10 15:43:22 -08002750
2751 // Update the workspace transition step as well
2752 dispatchOnLauncherTransitionStep(v, 0f);
2753 }
2754
2755 private void dispatchOnLauncherTransitionStep(View v, float t) {
2756 if (v instanceof LauncherTransitionable) {
2757 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2758 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002759 }
2760
2761 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2762 if (v instanceof LauncherTransitionable) {
2763 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2764 }
Winson Chung70442722012-02-10 15:43:22 -08002765
2766 // Update the workspace transition step as well
2767 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002768 }
2769
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002770 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002771 * Things to test when changing the following seven functions.
2772 * - Home from workspace
2773 * - from center screen
2774 * - from other screens
2775 * - Home from all apps
2776 * - from center screen
2777 * - from other screens
2778 * - Back from all apps
2779 * - from center screen
2780 * - from other screens
2781 * - Launch app from workspace and quit
2782 * - with back
2783 * - with home
2784 * - Launch app from all apps and quit
2785 * - with back
2786 * - with home
2787 * - Go to a screen that's not the default, then all
2788 * apps, and launch and app, and go back
2789 * - with back
2790 * -with home
2791 * - On workspace, long press power and go back
2792 * - with back
2793 * - with home
2794 * - On all apps, long press power and go back
2795 * - with back
2796 * - with home
2797 * - On workspace, power off
2798 * - On all apps, power off
2799 * - Launch an app and turn off the screen while in that app
2800 * - Go back with home key
2801 * - Go back with back key TODO: make this not go to workspace
2802 * - From all apps
2803 * - From workspace
2804 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2805 * - From all apps
2806 * - From the center workspace
2807 * - From another workspace
2808 */
2809
2810 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002811 * Zoom the camera out from the workspace to reveal 'toView'.
2812 * Assumes that the view to show is anchored at either the very top or very bottom
2813 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002814 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002815 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002816 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2817 showAppsCustomizeHelper(animated, springLoaded, contentType);
2818 }
2819 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2820 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002821 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002822 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002823 mStateAnimation.cancel();
2824 mStateAnimation = null;
2825 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002826 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002827
Winson Chungf0ea4d32011-06-06 14:27:16 -07002828 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2829 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2830 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002831 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002832 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002833 final int startDelay =
2834 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002835
Michael Jurkab3e22d92011-10-31 15:58:33 -07002836 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002837
Michael Jurkad74c9842011-07-10 12:44:21 -07002838 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002839 Animator workspaceAnim =
2840 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002841 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2842 // Set the content type for the all apps space
2843 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2844 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002845
2846 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002847 toView.setScaleX(scale);
2848 toView.setScaleY(scale);
2849 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2850 scaleAnim.
2851 scaleX(1f).scaleY(1f).
2852 setDuration(duration).
2853 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002854
Winson Chungf0ea4d32011-06-06 14:27:16 -07002855 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002856 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002857 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002858 .ofFloat(toView, "alpha", 0f, 1f)
2859 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002860 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002861 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2862 @Override
2863 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002864 if (animation == null) {
2865 throw new RuntimeException("animation is null");
2866 }
Winson Chung70442722012-02-10 15:43:22 -08002867 float t = (Float) animation.getAnimatedValue();
2868 dispatchOnLauncherTransitionStep(fromView, t);
2869 dispatchOnLauncherTransitionStep(toView, t);
2870 }
2871 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002872
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002873 // toView should appear right at the end of the workspace shrink
2874 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002875 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002876 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002877 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002878
2879 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002880 boolean animationCancelled = false;
2881
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002882 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002883 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002884 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002885 // Prepare the position
2886 toView.setTranslationX(0.0f);
2887 toView.setTranslationY(0.0f);
2888 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002889 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002890 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002891 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002892 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002893 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2894 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002895
Adam Cohenf4ddea32011-09-12 13:46:42 -07002896 if (!animationCancelled) {
2897 updateWallpaperVisibility(false);
2898 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002899
2900 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002901 if (mSearchDropTargetBar != null) {
2902 mSearchDropTargetBar.hideSearchBar(false);
2903 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002904 }
2905
Adam Cohencff6af82011-09-13 14:51:53 -07002906 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002907 public void onAnimationCancel(Animator animation) {
2908 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002909 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002910 });
2911
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002912 if (workspaceAnim != null) {
2913 mStateAnimation.play(workspaceAnim);
2914 }
Michael Jurka1899a362011-11-03 13:50:45 -07002915
2916 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002917
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002918 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2919 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002920
2921 // If any of the objects being animated haven't been measured/laid out
2922 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002923 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002924 (mWorkspace.getMeasuredWidth() == 0) ||
2925 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002926 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002927 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002928
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002929 final AnimatorSet stateAnimation = mStateAnimation;
2930 final Runnable startAnimRunnable = new Runnable() {
2931 public void run() {
2932 // Check that mStateAnimation hasn't changed while
2933 // we waited for a layout/draw pass
2934 if (mStateAnimation != stateAnimation)
2935 return;
2936 setPivotsForZoom(toView, scale);
2937 dispatchOnLauncherTransitionStart(fromView, animated, false);
2938 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002939 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002940 }
2941 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002942 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002943 final ViewTreeObserver observer = toView.getViewTreeObserver();
2944 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2945 public void onGlobalLayout() {
2946 startAnimRunnable.run();
2947 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2948 }
2949 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002950 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002951 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002952 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002953 } else {
2954 toView.setTranslationX(0.0f);
2955 toView.setTranslationY(0.0f);
2956 toView.setScaleX(1.0f);
2957 toView.setScaleY(1.0f);
2958 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002959 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002960
Daniel Sandlere4f98912013-06-25 15:13:26 -04002961 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002962 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002963 if (mSearchDropTargetBar != null) {
2964 mSearchDropTargetBar.hideSearchBar(false);
2965 }
Michael Jurkaabded662011-03-04 12:06:57 -08002966 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002967 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002968 dispatchOnLauncherTransitionStart(fromView, animated, false);
2969 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002970 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002971 dispatchOnLauncherTransitionStart(toView, animated, false);
2972 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002973 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002974 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002975 }
2976
2977 /**
2978 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002979 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002980 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002981 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002982 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2983 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002984
Michael Jurkab3e22d92011-10-31 15:58:33 -07002985 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002986 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002987 mStateAnimation.cancel();
2988 mStateAnimation = null;
2989 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002990 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002991
Winson Chungf0ea4d32011-06-06 14:27:16 -07002992 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002993 final int fadeOutDuration =
2994 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002995 final float scaleFactor = (float)
2996 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2997 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002998 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002999 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003000 if (toState == State.WORKSPACE) {
3001 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3002 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07003003 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003004 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
3005 workspaceAnim = mWorkspace.getChangeStateAnimation(
3006 Workspace.State.SPRING_LOADED, animated);
3007 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003008
Michael Jurkab3e22d92011-10-31 15:58:33 -07003009 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003010 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07003011 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003012 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003013 final LauncherViewPropertyAnimator scaleAnim =
3014 new LauncherViewPropertyAnimator(fromView);
3015 scaleAnim.
3016 scaleX(scaleFactor).scaleY(scaleFactor).
3017 setDuration(duration).
3018 setInterpolator(new Workspace.ZoomInInterpolator());
3019
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003020 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003021 .ofFloat(fromView, "alpha", 1f, 0f)
3022 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003023 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003024 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3025 @Override
3026 public void onAnimationUpdate(ValueAnimator animation) {
3027 float t = 1f - (Float) animation.getAnimatedValue();
3028 dispatchOnLauncherTransitionStep(fromView, t);
3029 dispatchOnLauncherTransitionStep(toView, t);
3030 }
3031 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003032
Michael Jurka2ecf9952012-06-18 12:52:28 -07003033 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003034
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003035 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3036 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07003037 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003038
3039 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003040 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003041 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003042 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003043 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003044 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3045 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003046 if (onCompleteRunnable != null) {
3047 onCompleteRunnable.run();
3048 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003049 mAppsCustomizeContent.updateCurrentPageScroll();
3050 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003051 }
3052 });
3053
Winson Chungf0ea4d32011-06-06 14:27:16 -07003054 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003055 if (workspaceAnim != null) {
3056 mStateAnimation.play(workspaceAnim);
3057 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003058 dispatchOnLauncherTransitionStart(fromView, animated, true);
3059 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003060 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003061 } else {
3062 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003063 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003064 dispatchOnLauncherTransitionStart(fromView, animated, true);
3065 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003066 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003067 dispatchOnLauncherTransitionStart(toView, animated, true);
3068 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003069 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003070 }
3071
Michael Jurkae326f182011-11-21 14:05:46 -08003072 @Override
3073 public void onTrimMemory(int level) {
3074 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003075 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003076 mAppsCustomizeTabHost.onTrimMemory();
3077 }
3078 }
3079
Winson Chung18f41f82012-05-09 13:28:10 -07003080 @Override
3081 public void onWindowFocusChanged(boolean hasFocus) {
3082 if (!hasFocus) {
3083 // When another window occludes launcher (like the notification shade, or recents),
3084 // ensure that we enable the wallpaper flag so that transitions are done correctly.
3085 updateWallpaperVisibility(true);
3086 } else {
3087 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07003088 mWorkspace.postDelayed(new Runnable() {
3089 @Override
3090 public void run() {
3091 disableWallpaperIfInAllApps();
3092 }
3093 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07003094 }
3095 }
3096
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003097 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003098 showWorkspace(animated, null);
3099 }
3100
3101 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003102 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003103 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003104 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003105 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003106
Winson Chungc7d2b602012-05-16 17:02:20 -07003107 // Show the search bar (only animate if we were showing the drop target bar in spring
3108 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003109 if (mSearchDropTargetBar != null) {
3110 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3111 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003112
Michael Jurkab3e22d92011-10-31 15:58:33 -07003113 // Set focus to the AppsCustomize button
3114 if (mAllAppsButton != null) {
3115 mAllAppsButton.requestFocus();
3116 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003117 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003118
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003119 // Change the state *after* we've called all the transition code
3120 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003121
Winson Chung5fb63472011-02-02 17:03:37 -08003122 // Resume the auto-advance of widgets
3123 mUserPresent = true;
3124 updateRunning();
3125
alanv1d4fde62012-10-17 13:15:47 -07003126 // Send an accessibility event to announce the context change
3127 getWindow().getDecorView()
3128 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003129
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003130 onWorkspaceShown(animated);
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07003131 onInteractionEnd();
Bjorn Bringertc459e522013-06-07 19:36:01 +01003132 }
3133
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003134 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003135 }
3136
Winson Chungc58497e2013-09-03 17:48:37 -07003137 void showAllApps(boolean animated,
3138 AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003139 if (mState != State.WORKSPACE) return;
3140
Winson Chungc58497e2013-09-03 17:48:37 -07003141 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003142 mAppsCustomizeTabHost.requestFocus();
3143
Michael Jurkab3e22d92011-10-31 15:58:33 -07003144 // Change the state *after* we've called all the transition code
3145 mState = State.APPS_CUSTOMIZE;
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07003146 onInteractionBegin();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003147
3148 // Pause the auto-advance of widgets until we are out of AllApps
3149 mUserPresent = false;
3150 updateRunning();
3151 closeFolder();
3152
3153 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003154 getWindow().getDecorView()
3155 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003156 }
3157
Adam Cohen7777d962011-08-18 18:58:38 -07003158 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003159 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003160 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003161 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003162 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003163 }
Adam Cohen7777d962011-08-18 18:58:38 -07003164
Adam Cohened66b2b2012-01-23 17:28:51 -08003165 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3166 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003167 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3168
Winson Chunge7a03942011-08-05 15:05:12 -07003169 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003170 @Override
3171 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003172 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003173 // Before we show workspace, hide all apps again because
3174 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3175 // clean up our state transition functions
3176 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003177 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003178 } else {
3179 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003180 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003181 }
3182 }, (extendedDelay ?
3183 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3184 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3185 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003186
Michael Jurkad3ef3062010-11-23 16:23:58 -08003187 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003188 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003189 final boolean animated = true;
3190 final boolean springLoaded = true;
3191 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003192 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003193 }
3194 // Otherwise, we are not in spring loaded mode, so don't do anything.
3195 }
3196
Michael Jurkab3e22d92011-10-31 15:58:33 -07003197 void lockAllApps() {
3198 // TODO
3199 }
3200
3201 void unlockAllApps() {
3202 // TODO
3203 }
3204
Winson Chungf0ea4d32011-06-06 14:27:16 -07003205 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003206 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003207 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003209 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003210 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003211 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003212 int duration = 0;
3213 if (mSearchDropTargetBar != null) {
3214 duration = mSearchDropTargetBar.getTransitionInDuration();
3215 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003216 mHotseat.animate().alpha(1f).setDuration(duration);
3217 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003218 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003219 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003220 }
3221 }
3222 }
3223
3224 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003225 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003226 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003227 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003228 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003229 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003230 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003231 int duration = 0;
3232 if (mSearchDropTargetBar != null) {
3233 duration = mSearchDropTargetBar.getTransitionOutDuration();
3234 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003235 mHotseat.animate().alpha(0f).setDuration(duration);
3236 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003237 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003238 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003239 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003240 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003241 }
3242
Patrick Dubroy5f445422011-02-18 14:35:21 -08003243 /**
3244 * Add an item from all apps or customize onto the given workspace screen.
3245 * If layout is null, add to the current screen.
3246 */
3247 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003248 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003249 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003250 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003251 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003252
Winson Chungdff8ebb2011-09-08 17:25:31 -07003253 /** Maps the current orientation to an index for referencing orientation correct global icons */
3254 private int getCurrentOrientationIndexForGlobalIcons() {
3255 // default - 0, landscape - 1
3256 switch (getResources().getConfiguration().orientation) {
3257 case Configuration.ORIENTATION_LANDSCAPE:
3258 return 1;
3259 default:
3260 return 0;
3261 }
3262 }
3263
Michael Jurkaae65ee32012-04-30 11:45:54 -07003264 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003265 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003266 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003267 // Look for the toolbar icon specified in the activity meta-data
3268 Bundle metaData = packageManager.getActivityInfo(
3269 activityName, PackageManager.GET_META_DATA).metaData;
3270 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003271 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003272 if (iconResId != 0) {
3273 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003274 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003275 }
3276 }
3277 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003278 // This can happen if the activity defines an invalid drawable
3279 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3280 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003281 } catch (Resources.NotFoundException nfe) {
3282 // This can happen if the activity defines an invalid drawable
3283 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3284 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003285 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003286 return null;
3287 }
3288
3289 // if successful in getting icon, return it; otherwise, set button to use default drawable
3290 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003291 int buttonId, ComponentName activityName, int fallbackDrawableId,
3292 String toolbarResourceName) {
3293 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003294 Resources r = getResources();
3295 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3296 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003297
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003298 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003299 // If we were unable to find the icon via the meta-data, use a generic one
3300 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003301 toolbarIcon = r.getDrawable(fallbackDrawableId);
3302 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003303 if (button != null) {
3304 button.setCompoundDrawables(toolbarIcon, null, null, null);
3305 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003306 return null;
3307 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003308 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003309 if (button != null) {
3310 button.setCompoundDrawables(toolbarIcon, null, null, null);
3311 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003312 return toolbarIcon.getConstantState();
3313 }
3314 }
3315
3316 // if successful in getting icon, return it; otherwise, set button to use default drawable
3317 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003318 int buttonId, ComponentName activityName, int fallbackDrawableId,
3319 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003320 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003321 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003322
Michael Jurka19e0fc52011-07-22 18:00:21 -07003323 if (button != null) {
3324 // If we were unable to find the icon via the meta-data, use a
3325 // generic one
3326 if (toolbarIcon == null) {
3327 button.setImageResource(fallbackDrawableId);
3328 } else {
3329 button.setImageDrawable(toolbarIcon);
3330 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003331 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003332
3333 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3334
Michael Jurka0423dcf2010-10-05 14:56:18 -07003335 }
3336
Winson Chung1b884092012-06-08 17:13:02 -07003337 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003338 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003339 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003340 }
3341
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003342 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003343 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003344 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003345 }
3346
Winson Chungbb185bd2011-11-21 12:31:42 -08003347 private void invalidatePressedFocusedStates(View container, View button) {
3348 if (container instanceof HolographicLinearLayout) {
3349 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3350 layout.invalidatePressedFocusedStates();
3351 } else if (button instanceof HolographicImageView) {
3352 HolographicImageView view = (HolographicImageView) button;
3353 view.invalidatePressedFocusedStates();
3354 }
3355 }
3356
Cristina Stancu476493b2013-08-07 17:20:14 +01003357 public View getQsbBar() {
3358 if (mQsbBar == null) {
3359 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3360 }
3361 return mQsbBar;
3362 }
3363
3364 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003365 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003366 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003367 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003368 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003369
Winson Chung1cad91e2011-05-25 17:41:01 -07003370 final SearchManager searchManager =
3371 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3372 ComponentName activityName = searchManager.getGlobalSearchActivity();
3373 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003374 int coi = getCurrentOrientationIndexForGlobalIcons();
3375 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003376 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3377 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3378 if (sGlobalSearchIcon[coi] == null) {
3379 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3380 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3381 TOOLBAR_ICON_METADATA_NAME);
3382 }
3383
Winson Chungc51db6a2011-10-05 11:44:49 -07003384 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3385 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003386 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003387 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003388 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003389 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003390 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3391 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003392 if (searchButton != null) searchButton.setVisibility(View.GONE);
3393 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003394 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003395 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003396 }
3397 }
3398
Cristina Stancu476493b2013-08-07 17:20:14 +01003399 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003400 final View searchButtonContainer = findViewById(R.id.search_button_container);
3401 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003402 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003403 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003404 }
3405
Cristina Stancu476493b2013-08-07 17:20:14 +01003406 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003407 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003408 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003409
Winson Chungc51db6a2011-10-05 11:44:49 -07003410 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003411 final SearchManager searchManager =
3412 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3413 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3414
3415 ComponentName activityName = null;
3416 if (globalSearchActivity != null) {
3417 // Check if the global search activity handles voice search
3418 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3419 intent.setPackage(globalSearchActivity.getPackageName());
3420 activityName = intent.resolveActivity(getPackageManager());
3421 }
3422
3423 if (activityName == null) {
3424 // Fallback: check if an activity other than the global search activity
3425 // resolves this
3426 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3427 activityName = intent.resolveActivity(getPackageManager());
3428 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003429 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003430 int coi = getCurrentOrientationIndexForGlobalIcons();
3431 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003432 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3433 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3434 if (sVoiceSearchIcon[coi] == null) {
3435 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3436 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3437 TOOLBAR_ICON_METADATA_NAME);
3438 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003439 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003440 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003441 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003442 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003443 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003444 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003445 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003446 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003447 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003448 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003449 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003450 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003451
Cristina Stancu476493b2013-08-07 17:20:14 +01003452 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003453 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3454 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003455 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003456 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003457 }
3458
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003459 public void setVoiceButtonProxyVisible(boolean visible) {
3460 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3461 if (voiceButtonProxy != null) {
3462 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3463 }
3464 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003465 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003466 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003467 */
3468 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003469 final View marketButton = findViewById(R.id.market_button);
3470 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3471 // Find the app market activity by resolving an intent.
3472 // (If multiple app markets are installed, it will return the ResolverActivity.)
3473 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003474 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003475 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003476 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003477 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003478 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3479 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003480 marketButton.setVisibility(View.VISIBLE);
3481 } else {
3482 // We should hide and disable the view so that we don't try and restore the visibility
3483 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3484 marketButton.setVisibility(View.GONE);
3485 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003486 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003487 }
3488
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003489 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003490 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3491 Resources r = getResources();
3492 Drawable marketIconDrawable = d.newDrawable(r);
3493 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3494 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3495 marketIconDrawable.setBounds(0, 0, w, h);
3496
3497 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003498 }
3499
Michael Jurkad7c28052012-04-27 15:43:36 -07003500 @Override
3501 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003502 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003503 final List<CharSequence> text = event.getText();
3504 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003505 // Populate event with a fake title based on the current state.
3506 if (mState == State.APPS_CUSTOMIZE) {
3507 text.add(getString(R.string.all_apps_button_label));
3508 } else {
3509 text.add(getString(R.string.all_apps_home_button_label));
3510 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003511 return result;
3512 }
3513
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003514 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003515 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003516 */
3517 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3518 @Override
3519 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003520 closeSystemDialogs();
3521 }
3522 }
3523
3524 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003525 * Receives notifications whenever the appwidgets are reset.
3526 */
3527 private class AppWidgetResetObserver extends ContentObserver {
3528 public AppWidgetResetObserver() {
3529 super(new Handler());
3530 }
3531
3532 @Override
3533 public void onChange(boolean selfChange) {
3534 onAppWidgetReset();
3535 }
3536 }
3537
3538 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003539 * If the activity is currently paused, signal that we need to run the passed Runnable
3540 * in onResume.
3541 *
3542 * This needs to be called from incoming places where resources might have been loaded
3543 * while we are paused. That is becaues the Configuration might be wrong
3544 * when we're not running, and if it comes back to what it was when we
3545 * were paused, we are not restarted.
3546 *
3547 * Implementation of the method from LauncherModel.Callbacks.
3548 *
3549 * @return true if we are currently paused. The caller might be able to
3550 * skip some work in that case since we will come back again.
3551 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003552 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003553 if (mPaused) {
3554 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003555 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003556 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003557 }
3558 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003559 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003560 return true;
3561 } else {
3562 return false;
3563 }
3564 }
3565
Michael Jurkac402cd92013-05-20 15:49:32 +02003566 private boolean waitUntilResume(Runnable run) {
3567 return waitUntilResume(run, false);
3568 }
3569
Michael Jurka1e2f4652013-07-08 18:03:46 -07003570 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003571 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003572 }
3573
Michael Jurka7607c2f2013-04-03 14:33:19 -07003574 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003575 * If the activity is currently paused, signal that we need to re-run the loader
3576 * in onResume.
3577 *
3578 * This needs to be called from incoming places where resources might have been loaded
3579 * while we are paused. That is becaues the Configuration might be wrong
3580 * when we're not running, and if it comes back to what it was when we
3581 * were paused, we are not restarted.
3582 *
3583 * Implementation of the method from LauncherModel.Callbacks.
3584 *
3585 * @return true if we are currently paused. The caller might be able to
3586 * skip some work in that case since we will come back again.
3587 */
3588 public boolean setLoadOnResume() {
3589 if (mPaused) {
3590 Log.i(TAG, "setLoadOnResume");
3591 mOnResumeNeedsLoad = true;
3592 return true;
3593 } else {
3594 return false;
3595 }
3596 }
3597
3598 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003600 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003602 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003603 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003604 } else {
3605 return SCREEN_COUNT / 2;
3606 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003608
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609 /**
3610 * Refreshes the shortcuts shown on the workspace.
3611 *
3612 * Implementation of the method from LauncherModel.Callbacks.
3613 */
3614 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003615 // If we're starting binding all over again, clear any bind calls we'd postponed in
3616 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3617 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003618 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003619
Winson Chungd64d1762013-08-20 14:37:16 -07003620 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003621 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003622 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003623
Michael Jurka05bf644e2011-11-30 20:28:53 -08003624 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003625 if (mHotseat != null) {
3626 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003627 }
3628 }
3629
Adam Cohendcd297f2013-06-18 13:13:40 -07003630 @Override
3631 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003632 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003633
Adam Cohen5084cba2013-09-03 12:01:16 -07003634 // If there are no screens, we need to have an empty screen
3635 if (orderedScreenIds.size() == 0) {
3636 mWorkspace.addExtraEmptyScreen();
3637 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003638
3639 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003640 // setCurrentPage() so ensure that all pages are added before calling this).
3641 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003642 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3643 mWorkspace.createCustomContentPage();
3644 }
Winson Chung64359a52013-07-08 17:17:08 -07003645 }
3646
3647 @Override
3648 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003649 int count = orderedScreenIds.size();
3650 for (int i = 0; i < count; i++) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003651 Launcher.addDumpLog(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")", true);
Adam Cohen89bddfa2013-08-20 11:57:13 -07003652 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003653 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003654 }
3655
Adam Cohen39a06042013-07-19 14:30:12 -07003656 private boolean shouldShowWeightWatcher() {
3657 String spKey = LauncherAppState.getSharedPreferencesKey();
3658 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003659 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003660
3661 return show;
3662 }
3663
Adam Cohen4caf2982013-08-20 18:54:31 -07003664 private boolean emailSent() {
3665 String spKey = LauncherAppState.getSharedPreferencesKey();
3666 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3667 boolean show = sp.getBoolean(CORRUPTION_EMAIL_SENT_KEY, false);
3668 return show;
3669 }
3670
3671 private void setEmailSent(boolean sent) {
3672 String spKey = LauncherAppState.getSharedPreferencesKey();
3673 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3674
3675 SharedPreferences.Editor editor = sp.edit();
3676 editor.putBoolean(CORRUPTION_EMAIL_SENT_KEY, sent);
3677 editor.commit();
3678 }
3679
Adam Cohen39a06042013-07-19 14:30:12 -07003680 private void toggleShowWeightWatcher() {
3681 String spKey = LauncherAppState.getSharedPreferencesKey();
3682 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3683 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3684
3685 show = !show;
3686
3687 SharedPreferences.Editor editor = sp.edit();
3688 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3689 editor.commit();
3690
3691 if (mWeightWatcher != null) {
3692 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3693 }
3694 }
3695
Winson Chungd64d1762013-08-20 14:37:16 -07003696 public void bindAppsAdded(final ArrayList<Long> newScreens,
3697 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003698 final ArrayList<ItemInfo> addAnimated,
3699 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003700 Runnable r = new Runnable() {
3701 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003702 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003703 }
3704 };
3705 if (waitUntilResume(r)) {
3706 return;
3707 }
3708
Adam Cohen4caf2982013-08-20 18:54:31 -07003709 Launcher.addDumpLog(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -07003710
3711 // Add the new screens
3712 bindAddScreens(newScreens);
3713
3714 // We add the items without animation on non-visible pages, and with
3715 // animations on the new page (which we will try and snap to).
3716 if (!addNotAnimated.isEmpty()) {
3717 bindItems(addNotAnimated, 0,
3718 addNotAnimated.size(), false);
3719 }
3720 if (!addAnimated.isEmpty()) {
3721 bindItems(addAnimated, 0,
3722 addAnimated.size(), true);
3723 }
Winson Chungc58497e2013-09-03 17:48:37 -07003724
3725 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3726 addedApps != null && mAppsCustomizeContent != null) {
3727 mAppsCustomizeContent.addApps(addedApps);
3728 }
Winson Chungd64d1762013-08-20 14:37:16 -07003729 }
3730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003731 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 * Bind the items start-end from the list.
3733 *
3734 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003735 */
Winson Chung64359a52013-07-08 17:17:08 -07003736 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3737 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003738 Runnable r = new Runnable() {
3739 public void run() {
3740 bindItems(shortcuts, start, end, forceAnimateIcons);
3741 }
3742 };
3743 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003744 return;
3745 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003746
Winson Chungf0c6ae02012-03-21 16:10:31 -07003747 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003748 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3749 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003750 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003751 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003752 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003753 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003754 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003755
3756 // Short circuit if we are loading dock items for a configuration which has no dock
3757 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3758 mHotseat == null) {
3759 continue;
3760 }
3761
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 switch (item.itemType) {
3763 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3764 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003765 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003766 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003767
Winson Chung64359a52013-07-08 17:17:08 -07003768 /*
3769 * TODO: FIX collision case
3770 */
Winson Chungce376632013-07-11 16:12:41 -07003771 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3772 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3773 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3774 throw new RuntimeException("OCCUPIED");
3775 }
Winson Chung64359a52013-07-08 17:17:08 -07003776 }
3777
Adam Cohendcd297f2013-06-18 13:13:40 -07003778 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3779 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003780 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003781 // Animate all the applications up now
3782 shortcut.setAlpha(0f);
3783 shortcut.setScaleX(0f);
3784 shortcut.setScaleY(0f);
3785 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003786 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003789 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003790 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003791 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003792 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003793 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3794 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003796 default:
3797 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003799 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003800
Winson Chung997a9232013-07-24 15:33:46 -07003801 if (animateIcons) {
3802 // Animate to the correct page
3803 if (newShortcutsScreenId > -1) {
3804 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3805 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3806 if (newShortcutsScreenId != currentScreenId) {
3807 mWorkspace.snapToPage(newScreenIndex);
3808 }
3809 }
3810
Winson Chung64359a52013-07-08 17:17:08 -07003811 // We post the animation slightly delayed to prevent slowdowns when we are loading
3812 // right after we return to launcher.
3813 mWorkspace.postDelayed(new Runnable() {
3814 public void run() {
3815 anim.playTogether(bounceAnims);
3816 anim.start();
3817 }
3818 }, NEW_APPS_ANIMATION_DELAY);
3819 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003820 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003821 }
3822
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 /**
3824 * Implementation of the method from LauncherModel.Callbacks.
3825 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003826 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003827 Runnable r = new Runnable() {
3828 public void run() {
3829 bindFolders(folders);
3830 }
3831 };
3832 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003833 return;
3834 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003835 sFolders.clear();
3836 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003838
3839 /**
3840 * Add the views for a widget to the workspace.
3841 *
3842 * Implementation of the method from LauncherModel.Callbacks.
3843 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003844 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003845 Runnable r = new Runnable() {
3846 public void run() {
3847 bindAppWidget(item);
3848 }
3849 };
3850 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003851 return;
3852 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003853
Daniel Sandler843e8602010-06-07 14:59:01 -04003854 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3855 if (DEBUG_WIDGETS) {
3856 Log.d(TAG, "bindAppWidget: " + item);
3857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003858 final Workspace workspace = mWorkspace;
3859
3860 final int appWidgetId = item.appWidgetId;
3861 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003862 if (DEBUG_WIDGETS) {
3863 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3864 }
3865
Joe Onorato9c1289c2009-08-17 11:03:03 -04003866 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3867
Joe Onorato9c1289c2009-08-17 11:03:03 -04003868 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003869 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870
Adam Cohendcd297f2013-06-18 13:13:40 -07003871 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003872 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003873 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3874
Joe Onorato9c1289c2009-08-17 11:03:03 -04003875 workspace.requestLayout();
3876
Daniel Sandler843e8602010-06-07 14:59:01 -04003877 if (DEBUG_WIDGETS) {
3878 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3879 + (SystemClock.uptimeMillis()-start) + "ms");
3880 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 }
3882
Adam Cohen1462de32012-07-24 22:34:36 -07003883 public void onPageBoundSynchronously(int page) {
3884 mSynchronouslyBoundPages.add(page);
3885 }
3886
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 /**
3888 * Callback saying that there aren't any more items to bind.
3889 *
3890 * Implementation of the method from LauncherModel.Callbacks.
3891 */
Adam Cohene25af792013-06-06 23:08:25 -07003892 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003893 Runnable r = new Runnable() {
3894 public void run() {
3895 finishBindingItems(upgradePath);
3896 }
3897 };
3898 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003899 return;
3900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 if (mSavedState != null) {
3902 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003903 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003904 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003905 mSavedState = null;
3906 }
3907
Adam Cohen1462de32012-07-24 22:34:36 -07003908 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003909
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003910 // If we received the result of any pending adds while the loader was running (e.g. the
3911 // widget configuration forced an orientation change), process them now.
3912 for (int i = 0; i < sPendingAddList.size(); i++) {
3913 completeAdd(sPendingAddList.get(i));
3914 }
3915 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003916
Winson Chungc51db6a2011-10-05 11:44:49 -07003917 // Update the market app icon as necessary (the other icons will be managed in response to
3918 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003919 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003920
Winson Chungf0c6ae02012-03-21 16:10:31 -07003921 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003922 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003923 mWorkspace.getUniqueComponents(true, null);
3924 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003925 }
Adam Cohen99894d92013-06-14 11:22:59 -07003926
Adam Cohen66a01fd2013-06-11 12:48:00 -07003927 mWorkspace.post(new Runnable() {
3928 @Override
3929 public void run() {
3930 onFinishBindingItems();
3931 }
3932 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003933
3934 // Write all the logs to disk
3935 Launcher.addDumpLog(TAG, "10249126 - finishBindingItems() - dumping logs to disk", true);
3936 dumpLogsToLocalData(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003937 }
3938
Winson Chunga2413752012-04-03 14:22:34 -07003939 private boolean canRunNewAppsAnimation() {
3940 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3941 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3942 }
3943
Winson Chungc9168342013-06-26 14:54:55 -07003944 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3945 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3946 PropertyValuesHolder.ofFloat("alpha", 1f),
3947 PropertyValuesHolder.ofFloat("scaleX", 1f),
3948 PropertyValuesHolder.ofFloat("scaleY", 1f));
3949 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3950 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3951 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3952 return bounceAnim;
3953 }
3954
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003955 @Override
3956 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003957 boolean searchVisible = updateGlobalSearchIcon();
3958 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003959 if (mSearchDropTargetBar != null) {
3960 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3961 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003962 }
3963
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003964 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 * Add the icons for all apps.
3966 *
3967 * Implementation of the method from LauncherModel.Callbacks.
3968 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003969 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003970 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3971 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3972 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3973 getHotseat().addAllAppsFolder(mIconCache, apps,
3974 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3975 }
3976 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003977 }
Winson Chungc58497e2013-09-03 17:48:37 -07003978 } else {
3979 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3980 mAppsCustomizeContent != null) {
3981 mAppsCustomizeContent.setApps(apps);
3982 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 }
3985
Bjorn Bringert85f418d2013-09-06 12:50:05 +01003986 @Override
3987 public boolean shouldShowApp(ResolveInfo app) {
3988 return true;
3989 }
3990
Joe Onorato9c1289c2009-08-17 11:03:03 -04003991 /**
3992 * A package was updated.
3993 *
3994 * Implementation of the method from LauncherModel.Callbacks.
3995 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003996 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003997 Runnable r = new Runnable() {
3998 public void run() {
3999 bindAppsUpdated(apps);
4000 }
4001 };
4002 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004003 return;
4004 }
4005
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004006 if (mWorkspace != null) {
4007 mWorkspace.updateShortcuts(apps);
4008 }
Winson Chungc58497e2013-09-03 17:48:37 -07004009
4010 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4011 mAppsCustomizeContent != null) {
4012 mAppsCustomizeContent.updateApps(apps);
4013 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004014 }
4015
4016 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004017 * A package was uninstalled. We take both the super set of packageNames
4018 * in addition to specific applications to remove, the reason being that
4019 * this can be called when a package is updated as well. In that scenario,
4020 * we only remove specific components from the workspace, where as
4021 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004022 *
4023 * Implementation of the method from LauncherModel.Callbacks.
4024 */
Winson Chung83892cc2013-05-01 16:53:33 -07004025 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004026 final ArrayList<AppInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07004027 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07004028 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004029 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07004030 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07004031 }
Winson Chungd64d1762013-08-20 14:37:16 -07004032 };
4033 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004034 return;
4035 }
4036
Winson Chung64359a52013-07-08 17:17:08 -07004037 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07004038 mWorkspace.removeItemsByPackageName(packageNames);
4039 } else {
4040 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004041 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004042
Winson Chunga1820962011-10-03 16:31:06 -07004043 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07004044 mDragController.onAppsRemoved(appInfos, this);
Winson Chungc58497e2013-09-03 17:48:37 -07004045
4046 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4047 mAppsCustomizeContent != null) {
4048 mAppsCustomizeContent.removeApps(appInfos);
4049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004050 }
Joe Onoratobe386092009-11-17 17:32:16 -08004051
4052 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004053 * A number of packages were updated.
4054 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004055 private ArrayList<Object> mWidgetsAndShortcuts;
4056 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004057 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004058 bindPackagesUpdated(mWidgetsAndShortcuts);
4059 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004060 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004061 };
4062
4063 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4064 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4065 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004066 return;
4067 }
4068
Winson Chung64359a52013-07-08 17:17:08 -07004069 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004070 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4071 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004072 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004073 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004074 }
4075
Winson Chung400438b2011-01-16 17:53:48 -08004076 private int mapConfigurationOriActivityInfoOri(int configOri) {
4077 final Display d = getWindowManager().getDefaultDisplay();
4078 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4079 switch (d.getRotation()) {
4080 case Surface.ROTATION_0:
4081 case Surface.ROTATION_180:
4082 // We are currently in the same basic orientation as the natural orientation
4083 naturalOri = configOri;
4084 break;
4085 case Surface.ROTATION_90:
4086 case Surface.ROTATION_270:
4087 // We are currently in the other basic orientation to the natural orientation
4088 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4089 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4090 break;
4091 }
4092
4093 int[] oriMap = {
4094 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4095 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4096 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4097 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4098 };
4099 // Since the map starts at portrait, we need to offset if this device's natural orientation
4100 // is landscape.
4101 int indexOffset = 0;
4102 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4103 indexOffset = 1;
4104 }
4105 return oriMap[(d.getRotation() + indexOffset) % 4];
4106 }
Adam Cohen446e9402011-09-15 18:21:21 -07004107
Winson Chung4b919f82012-05-01 10:44:08 -07004108 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004109 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004110 getResources().getBoolean(R.bool.allow_rotation);
4111 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004112 }
Winson Chung4b919f82012-05-01 10:44:08 -07004113 public void lockScreenOrientation() {
4114 if (isRotationEnabled()) {
4115 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4116 .getConfiguration().orientation));
4117 }
4118 }
4119 public void unlockScreenOrientation(boolean immediate) {
4120 if (isRotationEnabled()) {
4121 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004122 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004123 } else {
4124 mHandler.postDelayed(new Runnable() {
4125 public void run() {
4126 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4127 }
4128 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004129 }
Winson Chung4b919f82012-05-01 10:44:08 -07004130 }
Winson Chung400438b2011-01-16 17:53:48 -08004131 }
4132
Winson Chung82f55532011-08-09 14:14:23 -07004133 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004134 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004135 if (DISABLE_CLINGS) {
4136 return false;
4137 }
4138
Brett Chabot2a9e2282011-08-23 15:03:13 -07004139 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004140 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004141
Michael Jurkae233a8b2013-03-19 13:49:20 +01004142 // Restricted secondary users (child mode) will potentially have very few apps
4143 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004144// boolean supportsLimitedUsers =
4145// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4146// Account[] accounts = AccountManager.get(this).getAccounts();
4147// if (supportsLimitedUsers && accounts.length == 0) {
4148// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4149// Bundle restrictions = um.getUserRestrictions();
4150// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4151// return false;
4152// }
4153// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004154 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004155 }
Michael Jurka22143132012-10-04 17:42:38 +02004156
Winson Chung7d7541e2011-09-16 20:14:36 -07004157 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004158 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004159 if (cling != null) {
4160 cling.init(this, positionData);
4161 cling.setVisibility(View.VISIBLE);
4162 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4163 if (animate) {
4164 cling.buildLayer();
4165 cling.setAlpha(0f);
4166 cling.animate()
4167 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004168 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004169 .setDuration(SHOW_CLING_DURATION)
4170 .setStartDelay(delay)
4171 .start();
4172 } else {
4173 cling.setAlpha(1f);
4174 }
Michael Jurka22143132012-10-04 17:42:38 +02004175 cling.setFocusableInTouchMode(true);
4176 cling.post(new Runnable() {
4177 public void run() {
4178 cling.setFocusable(true);
4179 cling.requestFocus();
4180 }
4181 });
4182 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4183 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004184 }
4185 return cling;
4186 }
Michael Jurka22143132012-10-04 17:42:38 +02004187
Winson Chung7d7541e2011-09-16 20:14:36 -07004188 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004189 // To catch cases where siblings of top-level views are made invisible, just check whether
4190 // the cling is directly set to GONE before dismissing it.
4191 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004192 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004193 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004194 anim.addListener(new AnimatorListenerAdapter() {
4195 public void onAnimationEnd(Animator animation) {
4196 cling.setVisibility(View.GONE);
4197 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004198 // We should update the shared preferences on a background thread
4199 new Thread("dismissClingThread") {
4200 public void run() {
4201 SharedPreferences.Editor editor = mSharedPrefs.edit();
4202 editor.putBoolean(flag, true);
4203 editor.commit();
4204 }
4205 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004206 };
4207 });
4208 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004209 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004210 }
4211 }
Michael Jurka22143132012-10-04 17:42:38 +02004212
Winson Chung9d9d74f2011-09-19 11:49:12 -07004213 private void removeCling(int id) {
4214 final View cling = findViewById(id);
4215 if (cling != null) {
4216 final ViewGroup parent = (ViewGroup) cling.getParent();
4217 parent.post(new Runnable() {
4218 @Override
4219 public void run() {
4220 parent.removeView(cling);
4221 }
4222 });
Michael Jurka22143132012-10-04 17:42:38 +02004223 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004224 }
4225 }
Michael Jurka974c3862012-05-22 22:00:31 -07004226
4227 private boolean skipCustomClingIfNoAccounts() {
4228 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4229 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4230 if (customCling) {
4231 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004232 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004233 Account[] accounts = am.getAccountsByType("com.google");
4234 return accounts.length == 0;
4235 }
4236 return false;
4237 }
4238
Winson Chung7d7541e2011-09-16 20:14:36 -07004239 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004240 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004241 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004242 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4243 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004244 // If we're not using the default workspace layout, replace workspace cling
4245 // with a custom workspace cling (usually specified in an overlay)
4246 // For now, only do this on tablets
4247 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004248 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004249 // Use a custom cling
4250 View cling = findViewById(R.id.workspace_cling);
4251 ViewGroup clingParent = (ViewGroup) cling.getParent();
4252 int clingIndex = clingParent.indexOfChild(cling);
4253 clingParent.removeViewAt(clingIndex);
4254 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4255 clingParent.addView(customCling, clingIndex);
4256 customCling.setId(R.id.workspace_cling);
4257 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004258 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004259 } else {
4260 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004261 }
4262 }
4263 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004264 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004265 if (isClingsEnabled() &&
4266 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004267 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004268 } else {
4269 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004270 }
4271 }
4272 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004273 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004274 if (isClingsEnabled() &&
4275 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4276 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004277 } else {
4278 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004279 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004280 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004281 }
Michael Jurka104c4562013-07-08 18:03:46 -07004282 protected SharedPreferences getSharedPrefs() {
4283 return mSharedPrefs;
4284 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004285 public boolean isFolderClingVisible() {
4286 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004287 if (cling != null) {
4288 return cling.getVisibility() == View.VISIBLE;
4289 }
4290 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004291 }
Winson Chung82f55532011-08-09 14:14:23 -07004292 public void dismissWorkspaceCling(View v) {
4293 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004294 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004295 }
4296 public void dismissAllAppsCling(View v) {
4297 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004298 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4299 }
4300 public void dismissFolderCling(View v) {
4301 Cling cling = (Cling) findViewById(R.id.folder_cling);
4302 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004303 }
4304
Winson Chung80baf5a2010-08-09 16:03:15 -07004305 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004306 * Prints out out state for debugging.
4307 */
4308 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004309 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004310 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004311 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4312 Log.d(TAG, "mRestoring=" + mRestoring);
4313 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4314 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004315 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004316 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004317
Winson Chung785d2eb2011-04-14 16:08:02 -07004318 if (mAppsCustomizeContent != null) {
4319 mAppsCustomizeContent.dumpState();
4320 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004321 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004322 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004323
4324 @Override
4325 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4326 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004327 synchronized (sDumpLogs) {
4328 writer.println(" ");
4329 writer.println("Debug logs: ");
4330 for (int i = 0; i < sDumpLogs.size(); i++) {
4331 writer.println(" " + sDumpLogs.get(i));
4332 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004333 }
4334 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004335
4336 public static void dumpDebugLogsToConsole() {
Adam Cohen4caf2982013-08-20 18:54:31 -07004337 synchronized (sDumpLogs) {
4338 Log.d(TAG, "");
4339 Log.d(TAG, "*********************");
4340 Log.d(TAG, "Launcher debug logs: ");
4341 for (int i = 0; i < sDumpLogs.size(); i++) {
4342 Log.d(TAG, " " + sDumpLogs.get(i));
4343 }
4344 Log.d(TAG, "*********************");
4345 Log.d(TAG, "");
Adam Cohen487f7dd2012-06-28 18:12:10 -07004346 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004347 }
4348
4349 public static void addDumpLog(String tag, String log, boolean debugLog) {
4350 if (debugLog) {
4351 Log.d(tag, log);
4352 }
4353 sDateStamp.setTime(System.currentTimeMillis());
4354 synchronized (sDumpLogs) {
4355 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
4356 }
4357 }
4358
4359 public void dumpLogsToLocalData(final boolean email) {
4360 new Thread("DumpLogsToLocalData") {
4361 @Override
4362 public void run() {
4363 boolean success = false;
4364 sDateStamp.setTime(sRunStart);
4365 String FILENAME = sDateStamp.getMonth() + "-"
4366 + sDateStamp.getDay() + "_"
4367 + sDateStamp.getHours() + "-"
4368 + sDateStamp.getMinutes() + "_"
4369 + sDateStamp.getSeconds() + ".txt";
4370
4371 FileOutputStream fos = null;
4372 File outFile = null;
4373 try {
4374 outFile = new File(getFilesDir(), FILENAME);
4375 outFile.createNewFile();
4376 fos = new FileOutputStream(outFile);
4377 } catch (Exception e) {
4378 e.printStackTrace();
4379 }
4380 if (fos != null) {
4381 PrintWriter writer = new PrintWriter(fos);
4382
4383 writer.println(" ");
4384 writer.println("Debug logs: ");
4385 synchronized (sDumpLogs) {
4386 for (int i = 0; i < sDumpLogs.size(); i++) {
4387 writer.println(" " + sDumpLogs.get(i));
4388 }
4389 }
4390 writer.close();
4391 }
4392 try {
4393 if (fos != null) {
4394 fos.close();
4395 success = true;
4396 }
4397 } catch (IOException e) {
4398 e.printStackTrace();
4399 }
4400
4401 if (success && email) {
4402 if (!emailSent()) {
4403 emailFile(outFile);
4404 }
4405 }
4406 }
4407 }.start();
4408 }
4409
4410 private void emailFile(File file) {
4411 File publicCopy = new File(Environment.getExternalStorageDirectory(), file.getName());
4412 try {
4413 copyFile(file, publicCopy);
4414 } catch (IOException e) {
4415 e.printStackTrace();
4416 return;
4417 }
4418
4419 Intent intent = new Intent(Intent.ACTION_SEND);
4420 intent.setType("text/plain");
4421 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"adamcohen@google.com, winsonc@google.com," +
4422 "mikejurka@google"});
4423 intent.putExtra(Intent.EXTRA_SUBJECT, "Data corruption " + file.getName());
4424 intent.putExtra(Intent.EXTRA_TEXT, "Data corruption has occurred, logs attached");
4425
4426 if (!file.exists() || !file.canRead()) {
4427 Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
4428 finish();
4429 return;
4430 }
4431
4432 Toast.makeText(this, "Data corruption has occurred, please send e-mail", Toast.LENGTH_LONG);
4433 Uri uri = Uri.fromFile(publicCopy);
4434 intent.putExtra(Intent.EXTRA_STREAM, uri);
4435 startActivity(Intent.createChooser(intent, "Please send logs, consider clearing data"));
4436
4437 setEmailSent(true);
4438 }
4439
4440 public void copyFile(File src, File dst) throws IOException {
4441 InputStream in = new FileInputStream(src);
4442 OutputStream out = new FileOutputStream(dst);
4443
4444 // Transfer bytes from in to out
4445 byte[] buf = new byte[1024];
4446 int len;
4447 while ((len = in.read(buf)) > 0) {
4448 out.write(buf, 0, len);
4449 }
4450 in.close();
4451 out.close();
Adam Cohen487f7dd2012-06-28 18:12:10 -07004452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004453}
Michael Jurka2763be32011-02-24 11:19:57 -08004454
Michael Jurkaabded662011-03-04 12:06:57 -08004455interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004456 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004457 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004458 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004459 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004460 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004461}