blob: 0a92f35632dbb88fbc2e3f9eeb78a44a2cb93f4f [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
Adam Cohenf9426d52012-06-04 17:26:21 -0700485 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700486 boolean searchVisible = false;
487 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800488 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700489 int coi = getCurrentOrientationIndexForGlobalIcons();
490 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
491 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700492 updateAppMarketIcon();
493 searchVisible = updateGlobalSearchIcon();
494 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700495 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700496 if (sGlobalSearchIcon[coi] != null) {
497 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700498 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700499 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700500 if (sVoiceSearchIcon[coi] != null) {
501 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700502 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700503 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700504 if (sAppMarketIcon[coi] != null) {
505 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800506 }
Winson Chungadf0c182012-08-23 14:59:07 -0700507 if (mSearchDropTargetBar != null) {
508 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
509 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 }
Romain Guycbb89e42009-06-08 15:52:54 -0700511
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700513 if (sLocaleConfiguration == null) {
514 new AsyncTask<Void, Void, LocaleConfiguration>() {
515 @Override
516 protected LocaleConfiguration doInBackground(Void... unused) {
517 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
518 readConfiguration(Launcher.this, localeConfiguration);
519 return localeConfiguration;
520 }
521
522 @Override
523 protected void onPostExecute(LocaleConfiguration result) {
524 sLocaleConfiguration = result;
525 checkForLocaleChange(); // recursive, but now with a locale configuration
526 }
527 }.execute();
528 return;
529 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700530
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 final Configuration configuration = getResources().getConfiguration();
532
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700533 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 final String locale = configuration.locale.toString();
535
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700536 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 final int mcc = configuration.mcc;
538
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700539 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 final int mnc = configuration.mnc;
541
Romain Guy84f296c2009-11-04 15:00:44 -0800542 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
Romain Guy84f296c2009-11-04 15:00:44 -0800544 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700545 sLocaleConfiguration.locale = locale;
546 sLocaleConfiguration.mcc = mcc;
547 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700548
Joe Onorato0589f0f2010-02-08 13:44:00 -0800549 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700550
551 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
552 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700553 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700554 public void run() {
555 writeConfiguration(Launcher.this, localeConfiguration);
556 }
557 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700558 }
559 }
560
561 private static class LocaleConfiguration {
562 public String locale;
563 public int mcc = -1;
564 public int mnc = -1;
565 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700566
Romain Guy98d01652009-06-30 16:21:04 -0700567 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
568 DataInputStream in = null;
569 try {
570 in = new DataInputStream(context.openFileInput(PREFERENCES));
571 configuration.locale = in.readUTF();
572 configuration.mcc = in.readInt();
573 configuration.mnc = in.readInt();
574 } catch (FileNotFoundException e) {
575 // Ignore
576 } catch (IOException e) {
577 // Ignore
578 } finally {
579 if (in != null) {
580 try {
581 in.close();
582 } catch (IOException e) {
583 // Ignore
584 }
585 }
586 }
587 }
588
589 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
590 DataOutputStream out = null;
591 try {
592 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
593 out.writeUTF(configuration.locale);
594 out.writeInt(configuration.mcc);
595 out.writeInt(configuration.mnc);
596 out.flush();
597 } catch (FileNotFoundException e) {
598 // Ignore
599 } catch (IOException e) {
600 //noinspection ResultOfMethodCallIgnored
601 context.getFileStreamPath(PREFERENCES).delete();
602 } finally {
603 if (out != null) {
604 try {
605 out.close();
606 } catch (IOException e) {
607 // Ignore
608 }
609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 }
611 }
612
Bjorn Bringertc459e522013-06-07 19:36:01 +0100613 public LayoutInflater getInflater() {
614 return mInflater;
615 }
616
Adam Cohen716b51e2011-06-30 12:09:54 -0700617 public DragLayer getDragLayer() {
618 return mDragLayer;
619 }
620
Winson Chung36a62fe2012-05-06 18:04:42 -0700621 boolean isDraggingEnabled() {
622 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
623 // that is subsequently removed from the workspace in startBinding().
624 return !mModel.isLoadingWorkspace();
625 }
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 static int getScreen() {
628 synchronized (sLock) {
629 return sScreen;
630 }
631 }
632
633 static void setScreen(int screen) {
634 synchronized (sLock) {
635 sScreen = screen;
636 }
637 }
638
Winson Chung557d6ed2011-07-08 15:34:52 -0700639 /**
640 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
641 * a configuration step, this allows the proper animations to run after other transitions.
642 */
643 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700644 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800645 switch (args.requestCode) {
646 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800649 break;
650 case REQUEST_PICK_SHORTCUT:
651 processShortcut(args.intent);
652 break;
653 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700654 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700655 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700656 result = true;
657 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 case REQUEST_CREATE_APPWIDGET:
659 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700660 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700661 result = true;
662 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 }
Michael Jurka27614d22012-04-02 06:40:22 -0700664 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
665 // if you turned the screen off and then back while in All Apps, Launcher would not
666 // return to the workspace. Clearing mAddInfo.container here fixes this issue
667 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700668 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 }
670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700672 protected void onActivityResult(
673 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700674 // Reset the startActivity waiting flag
675 mWaitingForResult = false;
676
Michael Jurka8b805b12012-04-18 14:23:14 -0700677 if (requestCode == REQUEST_BIND_APPWIDGET) {
678 int appWidgetId = data != null ?
679 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
680 if (resultCode == RESULT_CANCELED) {
681 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
682 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700683 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700684 }
685 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200686 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
687 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
688 mWorkspace.exitOverviewMode(false);
689 }
690 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700691 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200692
Winson Chung557d6ed2011-07-08 15:34:52 -0700693 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800694 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
695 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700696
Adam Cohened66b2b2012-01-23 17:28:51 -0800697 // We have special handling for widgets
698 if (isWidgetDrop) {
699 int appWidgetId = data != null ?
700 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700701 if (appWidgetId < 0) {
702 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
703 "widget configuration activity.");
704 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
705 } else {
706 completeTwoStageWidgetDrop(resultCode, appWidgetId);
707 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800708 return;
709 }
710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // The pattern used here is that a user PICKs a specific application,
712 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
715 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700716 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800717 final PendingAddArguments args = new PendingAddArguments();
718 args.requestCode = requestCode;
719 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700720 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700721 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700722 args.cellX = mPendingAddInfo.cellX;
723 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800724 if (isWorkspaceLocked()) {
725 sPendingAddList.add(args);
726 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700727 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700731 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
733 null);
734 }
735
736 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700738 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 Runnable onCompleteRunnable = null;
740 int animationType = 0;
741
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700742 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 if (resultCode == RESULT_OK) {
744 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
745 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700746 mPendingAddWidgetInfo);
747 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 onCompleteRunnable = new Runnable() {
749 @Override
750 public void run() {
751 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700752 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800753 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
754 null);
755 }
756 };
757 } else if (resultCode == RESULT_CANCELED) {
758 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
759 onCompleteRunnable = new Runnable() {
760 @Override
761 public void run() {
762 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
763 null);
764 }
765 };
766 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700767 if (mDragLayer.getAnimatedView() != null) {
768 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
769 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
770 animationType, boundWidget, true);
771 } else {
772 // The animated view may be null in the case of a rotation during widget configuration
773 onCompleteRunnable.run();
774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 }
776
777 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700778 protected void onStop() {
779 super.onStop();
780 FirstFrameAnimatorHelper.setIsVisible(false);
781 }
782
783 @Override
784 protected void onStart() {
785 super.onStart();
786 FirstFrameAnimatorHelper.setIsVisible(true);
787 }
788
789 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200791 long startTime = 0;
792 if (DEBUG_RESUME_TIME) {
793 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400794 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200795 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700797
Winson Chung4a2afa32012-07-19 14:53:05 -0700798 // Restore the previous launcher state
799 if (mOnResumeState == State.WORKSPACE) {
800 showWorkspace(false);
801 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chungc58497e2013-09-03 17:48:37 -0700802 showAllApps(false, AppsCustomizePagedView.ContentType.Applications);
Winson Chung4a2afa32012-07-19 14:53:05 -0700803 }
804 mOnResumeState = State.NONE;
805
Craig Mautner360310b2012-10-26 15:13:08 -0700806 // Background was set to gradient in onPause(), restore to black if in all apps.
807 setWorkspaceBackground(mState == State.WORKSPACE);
808
Winson Chungde0fb8f2012-05-08 14:37:08 -0700809 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700810 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700811
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800812 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700813 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700814 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700816 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400817 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700818 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700820 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200821 // We might have postponed some bind calls until onResume (see waitUntilResume) --
822 // execute them here
823 long startTimeCallbacks = 0;
824 if (DEBUG_RESUME_TIME) {
825 startTimeCallbacks = System.currentTimeMillis();
826 }
827
828 if (mAppsCustomizeContent != null) {
829 mAppsCustomizeContent.setBulkBind(true);
830 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700831 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
832 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200833 }
834 if (mAppsCustomizeContent != null) {
835 mAppsCustomizeContent.setBulkBind(false);
836 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700837 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200838 if (DEBUG_RESUME_TIME) {
839 Log.d(TAG, "Time spent processing callbacks in onResume: " +
840 (System.currentTimeMillis() - startTimeCallbacks));
841 }
Michael Jurka447bf842013-05-15 14:52:15 +0200842 }
Michael Jurka54554252013-08-01 12:52:23 +0200843 if (mOnResumeCallbacks.size() > 0) {
844 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
845 mOnResumeCallbacks.get(i).run();
846 }
847 mOnResumeCallbacks.clear();
848 }
Winson Chunge4e50662012-01-23 14:45:13 -0800849
850 // Reset the pressed state of icons that were locked in the press state while activities
851 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800852 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800853 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800854 mWaitingForResume.setStayPressed(false);
855 }
Winson Chunge4e50662012-01-23 14:45:13 -0800856 if (mAppsCustomizeContent != null) {
857 // Resets the previous all apps icon press state
858 mAppsCustomizeContent.resetDrawableState();
859 }
Adam Cohen06dff352012-06-01 17:17:08 -0700860 // It is possible that widgets can receive updates while launcher is not in the foreground.
861 // Consequently, the widgets will be inflated in the orientation of the foreground activity
862 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
863 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700864 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700865
866 // Again, as with the above scenario, it's possible that one or more of the global icons
867 // were updated in the wrong orientation.
868 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200869 if (DEBUG_RESUME_TIME) {
870 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
871 }
Adam Cohen4caf2982013-08-20 18:54:31 -0700872
873 // Write all the logs to disk
874 Launcher.addDumpLog(TAG, "10249126 - onResume() - dumping logs to disk", true);
875 dumpLogsToLocalData(false);
Adam Cohen06dff352012-06-01 17:17:08 -0700876 }
877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700879 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700880 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
881 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
882 // when Launcher resumes and we are still in AllApps.
883 updateWallpaperVisibility(true);
884
Winson Chung997a9232013-07-24 15:33:46 -0700885 // Ensure that items added to Launcher are queued until Launcher returns
886 InstallShortcutReceiver.enableInstallQueue();
887
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700888 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700889 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800890 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700891 mDragController.resetLastGestureUpTime();
Adam Cohen4caf2982013-08-20 18:54:31 -0700892
893 // Write all the logs to disk
894 Launcher.addDumpLog(TAG, "10249126 - onPause() - dumping logs to disk", true);
895 dumpLogsToLocalData(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700896 }
Romain Guycbb89e42009-06-08 15:52:54 -0700897
Adam Cohen66a01fd2013-06-11 12:48:00 -0700898 protected void onFinishBindingItems() {
899 }
900
Adam Cohenbffe7452013-07-22 18:21:45 -0700901 QSBScroller mQsbScroller = new QSBScroller() {
902 int scrollY = 0;
903
904 @Override
905 public void setScrollY(int scroll) {
906 scrollY = scroll;
907
908 if (mWorkspace.isOnOrMovingToCustomContent()) {
909 mSearchDropTargetBar.setTranslationY(- scrollY);
910 }
911 }
912 };
913
914 public void resetQSBScroll() {
915 mSearchDropTargetBar.animate().translationY(0).start();
916 }
917
918 public interface CustomContentCallbacks {
919 // Custom content is completely shown
920 public void onShow();
921
922 // Custom content is completely hidden
923 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700924
925 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
926 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700927 }
928
Adam Cohen30bacb22013-08-29 14:25:25 -0700929 protected void startSettings() {
930 }
931
Adam Cohenbffe7452013-07-22 18:21:45 -0700932 public interface QSBScroller {
933 public void setScrollY(int scrollY);
934 }
935
Adam Cohen66a01fd2013-06-11 12:48:00 -0700936 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700937 public QSBScroller addToCustomContentPage(View customContent) {
938 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700939 }
940
Winson Chung98ca0f72013-07-29 12:58:51 -0700941 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700942 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700943 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700944 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700945 }
946
Adam Cohenedb40762013-07-18 16:45:45 -0700947 // The custom content needs to offset its content to account for the QSB
948 public int getTopOffsetForCustomContent() {
949 return mWorkspace.getPaddingTop();
950 }
951
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700952 @Override
953 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 // Flag the loader to stop early before switching
955 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700956 if (mAppsCustomizeContent != null) {
957 mAppsCustomizeContent.surrender();
958 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800959 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700960 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700961
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800962 // We can't hide the IME if it was forced open. So don't bother
963 /*
964 @Override
965 public void onWindowFocusChanged(boolean hasFocus) {
966 super.onWindowFocusChanged(hasFocus);
967
968 if (hasFocus) {
969 final InputMethodManager inputManager = (InputMethodManager)
970 getSystemService(Context.INPUT_METHOD_SERVICE);
971 WindowManager.LayoutParams lp = getWindow().getAttributes();
972 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
973 android.os.Handler()) {
974 protected void onReceiveResult(int resultCode, Bundle resultData) {
975 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
976 }
977 });
978 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
979 }
980 }
981 */
982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 private boolean acceptFilter() {
984 final InputMethodManager inputManager = (InputMethodManager)
985 getSystemService(Context.INPUT_METHOD_SERVICE);
986 return !inputManager.isFullscreenMode();
987 }
988
989 @Override
990 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700991 final int uniChar = event.getUnicodeChar();
992 final boolean handled = super.onKeyDown(keyCode, event);
993 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
994 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
996 keyCode, event);
997 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700998 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700999 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001000 // showSearchDialog()
1001 // If there are multiple keystrokes before the search dialog takes focus,
1002 // onSearchRequested() will be called for every keystroke,
1003 // but it is idempotent, so it's fine.
1004 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
1006 }
1007
Joe Onorato8a9625e2010-01-28 15:55:35 -08001008 // Eat the long press event so the keyboard doesn't come up.
1009 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1010 return true;
1011 }
1012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 return handled;
1014 }
1015
Karl Rosaen138a0412009-04-23 19:00:21 -07001016 private String getTypedText() {
1017 return mDefaultKeySsb.toString();
1018 }
1019
1020 private void clearTypedText() {
1021 mDefaultKeySsb.clear();
1022 mDefaultKeySsb.clearSpans();
1023 Selection.setSelection(mDefaultKeySsb, 0);
1024 }
1025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001027 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1028 * State
1029 */
1030 private static State intToState(int stateOrdinal) {
1031 State state = State.WORKSPACE;
1032 final State[] stateValues = State.values();
1033 for (int i = 0; i < stateValues.length; i++) {
1034 if (stateValues[i].ordinal() == stateOrdinal) {
1035 state = stateValues[i];
1036 break;
1037 }
1038 }
1039 return state;
1040 }
1041
1042 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 * Restores the previous state, if it exists.
1044 *
1045 * @param savedState The previous state.
1046 */
1047 private void restoreState(Bundle savedState) {
1048 if (savedState == null) {
1049 return;
1050 }
1051
Michael Jurka883f55b2010-10-21 15:47:14 -07001052 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001053 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001054 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001055 }
1056
Winson Chungf0c6ae02012-03-21 16:10:31 -07001057 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001059 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 }
1061
Winson Chung3d503fb2011-07-13 17:25:49 -07001062 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001064
Winson Chung3d503fb2011-07-13 17:25:49 -07001065 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1066 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001067 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001068 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1069 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001070 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1071 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1072 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001073 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 mRestoring = true;
1075 }
1076
1077 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1078 if (renameFolder) {
1079 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001080 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 mRestoring = true;
1082 }
Winson Chunga12a2502010-12-20 14:41:35 -08001083
Winson Chung785d2eb2011-04-14 16:08:02 -07001084 // Restore the AppsCustomize tab
1085 if (mAppsCustomizeTabHost != null) {
1086 String curTab = savedState.getString("apps_customize_currentTab");
1087 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001088 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001089 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001090 mAppsCustomizeContent.loadAssociatedPages(
1091 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001092 }
1093
Winson Chung5afbf7b2011-07-25 11:53:08 -07001094 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1095 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
1098
1099 /**
1100 * Finds all the views we need and configure them properly.
1101 */
1102 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001103 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001104
Craig Mautner360310b2012-10-26 15:13:08 -07001105 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001106 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1107 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001108
John Spurlock77e1f472013-09-11 10:09:51 -04001109 mLauncherView.setSystemUiVisibility(
1110 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001111 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112
Winson Chung4c98d922011-05-31 16:50:48 -07001113 // Setup the drag layer
1114 mDragLayer.setup(this, dragController);
1115
Winson Chung3d503fb2011-07-13 17:25:49 -07001116 // Setup the hotseat
1117 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1118 if (mHotseat != null) {
1119 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001120 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001121 }
1122
Adam Cohenf358a4b2013-07-23 16:47:31 -07001123 mOverviewPanel = findViewById(R.id.overview_panel);
1124 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1125 @Override
1126 public void onClick(View arg0) {
Winson Chungc58497e2013-09-03 17:48:37 -07001127 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001128 }
1129 });
1130 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1131 @Override
1132 public void onClick(View arg0) {
1133 startWallpaper();
1134 }
1135 });
Adam Cohen30bacb22013-08-29 14:25:25 -07001136 findViewById(R.id.settings_button).setOnClickListener(new OnClickListener() {
1137 @Override
1138 public void onClick(View arg0) {
1139 startSettings();
1140 }
1141 });
Adam Cohenf358a4b2013-07-23 16:47:31 -07001142
Winson Chung4c98d922011-05-31 16:50:48 -07001143 // Setup the workspace
1144 mWorkspace.setHapticFeedbackEnabled(false);
1145 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001146 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001147 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001148
Winson Chungf0ea4d32011-06-06 14:27:16 -07001149 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001150 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001151
Winson Chungf0ea4d32011-06-06 14:27:16 -07001152 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001153 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001154 mAppsCustomizeContent = (AppsCustomizePagedView)
1155 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1156 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001157
Winson Chung3d503fb2011-07-13 17:25:49 -07001158 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001159 dragController.setDragScoller(mWorkspace);
1160 dragController.setScrollView(mDragLayer);
1161 dragController.setMoveTarget(mWorkspace);
1162 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001163 if (mSearchDropTargetBar != null) {
1164 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001165 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001166
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001167 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001168 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001169 mWeightWatcher = new WeightWatcher(this);
1170 mWeightWatcher.setAlpha(0.5f);
1171 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001172 new FrameLayout.LayoutParams(
1173 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001174 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001175 Gravity.BOTTOM)
1176 );
Adam Cohen39a06042013-07-19 14:30:12 -07001177
1178 boolean show = shouldShowWeightWatcher();
1179 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001180 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 }
1182
1183 /**
1184 * Creates a view representing a shortcut.
1185 *
1186 * @param info The data structure describing the shortcut.
1187 *
1188 * @return A View inflated from R.layout.application.
1189 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001190 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001192 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 }
1194
1195 /**
1196 * Creates a view representing a shortcut inflated from the specified resource.
1197 *
1198 * @param layoutResId The id of the XML layout used to create the shortcut.
1199 * @param parent The group the shortcut belongs to.
1200 * @param info The data structure describing the shortcut.
1201 *
1202 * @return A View inflated from layoutResId.
1203 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001204 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001205 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1206 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 return favorite;
1209 }
1210
1211 /**
1212 * Add an application shortcut to the workspace.
1213 *
1214 * @param data The intent describing the application.
1215 * @param cellInfo The position on screen where to create the shortcut.
1216 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001217 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001218 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001219 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220
Adam Cohenfbba09b2011-07-18 21:43:05 -07001221 // First we check if we already know the exact location where we want to add this item.
1222 if (cellX >= 0 && cellY >= 0) {
1223 cellXY[0] = cellX;
1224 cellXY[1] = cellY;
1225 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001226 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001227 return;
1228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001230 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001231
Romain Guy73b979d2009-06-09 12:57:21 -07001232 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001233 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001235 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001236 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001237 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001238 } else {
1239 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 }
1241 }
Romain Guycbb89e42009-06-08 15:52:54 -07001242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 /**
1244 * Add a shortcut to the workspace.
1245 *
1246 * @param data The intent describing the shortcut.
1247 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001249 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001250 int cellY) {
1251 int[] cellXY = mTmpAddItemCellCoordinates;
1252 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001253 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001254
Michael Jurkad3ef3062010-11-23 16:23:58 -08001255 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001256
Adam Cohen558baaf2011-08-15 15:22:57 -07001257 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001258 if (info == null) {
1259 return;
1260 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001261 final View view = createShortcut(info);
1262
Adam Cohenfbba09b2011-07-18 21:43:05 -07001263 // First we check if we already know the exact location where we want to add this item.
1264 if (cellX >= 0 && cellY >= 0) {
1265 cellXY[0] = cellX;
1266 cellXY[1] = cellY;
1267 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001268
1269 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001270 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001271 true, null,null)) {
1272 return;
1273 }
1274 DragObject dragObject = new DragObject();
1275 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001276 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1277 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001278 return;
1279 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001280 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001281 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001282 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001283 foundCellSpan = (result != null);
1284 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001285 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001286 }
1287
1288 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001289 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001290 return;
1291 }
1292
Adam Cohendcd297f2013-06-18 13:13:40 -07001293 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294
1295 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001296 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001297 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
1299 }
1300
Adam Cohen2f093b62012-04-30 18:59:53 -07001301 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1302 int minHeight) {
1303 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001304 // We want to account for the extra amount of padding that we are adding to the widget
1305 // to ensure that it gets the full amount of space that it has requested
1306 int requiredWidth = minWidth + padding.left + padding.right;
1307 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001308 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001309 }
1310
Adam Cohen2f093b62012-04-30 18:59:53 -07001311 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1312 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001313 }
1314
Adam Cohen2f093b62012-04-30 18:59:53 -07001315 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1316 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001317 }
1318
Adam Cohen2f093b62012-04-30 18:59:53 -07001319 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1320 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001321 }
1322
Adam Cohen2f093b62012-04-30 18:59:53 -07001323 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1324 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001325 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001326 }
1327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001329 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001331 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001332 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001334 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001335 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1336 if (appWidgetInfo == null) {
1337 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1338 }
Romain Guycbb89e42009-06-08 15:52:54 -07001339
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001340 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001341 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001342
Adam Cohen2f093b62012-04-30 18:59:53 -07001343 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1344 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001345
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001347 // We have saved the position to which the widget was dragged-- this really only matters
1348 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001349 int[] cellXY = mTmpAddItemCellCoordinates;
1350 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001351 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001352 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001353 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1354 cellXY[0] = mPendingAddInfo.cellX;
1355 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001356 spanXY[0] = mPendingAddInfo.spanX;
1357 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001358 foundCellSpan = true;
1359 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001360 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001361 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001362 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1363 spanXY[1], cellXY, finalSpan);
1364 spanXY[0] = finalSpan[0];
1365 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001366 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001367 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001368 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001369 }
1370
Michael Jurka0280c3b2010-09-17 15:00:07 -07001371 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001372 if (appWidgetId != -1) {
1373 // Deleting an app widget ID is a void call but writes to disk before returning
1374 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001375 new Thread("deleteAppWidgetId") {
1376 public void run() {
1377 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1378 }
1379 }.start();
1380 }
Winson Chung93eef082012-03-23 15:59:27 -07001381 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001382 return;
1383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001385 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001386 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1387 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001388 launcherInfo.spanX = spanXY[0];
1389 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001390 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1391 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001394 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395
1396 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001397 if (hostView == null) {
1398 // Perform actual inflation because we're live
1399 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1400 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1401 } else {
1402 // The AppWidgetHostView has already been inflated and instantiated
1403 launcherInfo.hostView = hostView;
1404 }
Romain Guycbb89e42009-06-08 15:52:54 -07001405
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001407 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001408 launcherInfo.notifyWidgetSizeChanged(this);
1409
Adam Cohendcd297f2013-06-18 13:13:40 -07001410 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001411 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001412
1413 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001415 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
Romain Guycbb89e42009-06-08 15:52:54 -07001417
Adam Cohended9f8d2010-11-03 13:25:16 -07001418 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1419 @Override
1420 public void onReceive(Context context, Intent intent) {
1421 final String action = intent.getAction();
1422 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1423 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001424 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001425 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001426
Winson Chungc100e8e2011-08-09 16:02:43 -07001427 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001428 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001429 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1430 mAppsCustomizeTabHost.reset();
1431 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001432 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001433 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1434 mUserPresent = true;
1435 updateRunning();
1436 }
1437 }
1438 };
1439
1440 @Override
1441 public void onAttachedToWindow() {
1442 super.onAttachedToWindow();
1443
1444 // Listen for broadcasts related to user-presence
1445 final IntentFilter filter = new IntentFilter();
1446 filter.addAction(Intent.ACTION_SCREEN_OFF);
1447 filter.addAction(Intent.ACTION_USER_PRESENT);
1448 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001449 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001450 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001451 mVisible = true;
1452 }
1453
1454 @Override
1455 public void onDetachedFromWindow() {
1456 super.onDetachedFromWindow();
1457 mVisible = false;
1458
Adam Cohend113e0c2010-11-11 10:48:05 -08001459 if (mAttached) {
1460 unregisterReceiver(mReceiver);
1461 mAttached = false;
1462 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001463 updateRunning();
1464 }
1465
1466 public void onWindowVisibilityChanged(int visibility) {
1467 mVisible = visibility == View.VISIBLE;
1468 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001469 // The following code used to be in onResume, but it turns out onResume is called when
1470 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1471 // is a more appropriate event to handle
1472 if (mVisible) {
1473 mAppsCustomizeTabHost.onWindowVisible();
1474 if (!mWorkspaceLoading) {
1475 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001476 // We want to let Launcher draw itself at least once before we force it to build
1477 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001478 // apps is nice and speedy.
1479 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001480 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001481 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001482 if (mStarted) return;
1483 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001484 // We delay the layer building a bit in order to give
1485 // other message processing a time to run. In particular
1486 // this avoids a delay in hiding the IME if it was
1487 // currently shown, because doing that may involve
1488 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001489 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001490 final ViewTreeObserver.OnDrawListener listener = this;
1491 mWorkspace.post(new Runnable() {
1492 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001493 if (mWorkspace != null &&
1494 mWorkspace.getViewTreeObserver() != null) {
1495 mWorkspace.getViewTreeObserver().
1496 removeOnDrawListener(listener);
1497 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001498 }
1499 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001500 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001501 }
1502 });
1503 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001504 // When Launcher comes back to foreground, a different Activity might be responsible for
1505 // the app market intent, so refresh the icon
1506 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001507 clearTypedText();
1508 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001509 }
1510
1511 private void sendAdvanceMessage(long delay) {
1512 mHandler.removeMessages(ADVANCE_MSG);
1513 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1514 mHandler.sendMessageDelayed(msg, delay);
1515 mAutoAdvanceSentTime = System.currentTimeMillis();
1516 }
1517
1518 private void updateRunning() {
1519 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1520 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1521 mAutoAdvanceRunning = autoAdvanceRunning;
1522 if (autoAdvanceRunning) {
1523 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1524 sendAdvanceMessage(delay);
1525 } else {
1526 if (!mWidgetsToAdvance.isEmpty()) {
1527 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1528 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1529 }
1530 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001531 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001532 }
1533 }
1534 }
1535
1536 private final Handler mHandler = new Handler() {
1537 @Override
1538 public void handleMessage(Message msg) {
1539 if (msg.what == ADVANCE_MSG) {
1540 int i = 0;
1541 for (View key: mWidgetsToAdvance.keySet()) {
1542 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1543 final int delay = mAdvanceStagger * i;
1544 if (v instanceof Advanceable) {
1545 postDelayed(new Runnable() {
1546 public void run() {
1547 ((Advanceable) v).advance();
1548 }
1549 }, delay);
1550 }
1551 i++;
1552 }
1553 sendAdvanceMessage(mAdvanceInterval);
1554 }
1555 }
1556 };
1557
1558 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001559 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001560 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1561 if (v instanceof Advanceable) {
1562 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001563 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001564 updateRunning();
1565 }
1566 }
1567
1568 void removeWidgetToAutoAdvance(View hostView) {
1569 if (mWidgetsToAdvance.containsKey(hostView)) {
1570 mWidgetsToAdvance.remove(hostView);
1571 updateRunning();
1572 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001573 }
1574
Joe Onorato9c1289c2009-08-17 11:03:03 -04001575 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001576 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001577 launcherInfo.hostView = null;
1578 }
1579
Winson Chung93eef082012-03-23 15:59:27 -07001580 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1581 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1582 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001583 }
1584
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001585 public LauncherAppWidgetHost getAppWidgetHost() {
1586 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
Romain Guycbb89e42009-06-08 15:52:54 -07001588
Winson Chunga9abd0e2010-10-27 17:18:37 -07001589 public LauncherModel getModel() {
1590 return mModel;
1591 }
1592
Bjorn Bringertc459e522013-06-07 19:36:01 +01001593 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001594 getWindow().closeAllPanels();
1595
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001596 // Whatever we were doing is hereby canceled.
1597 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001598 }
1599
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 @Override
1601 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001602 long startTime = 0;
1603 if (DEBUG_RESUME_TIME) {
1604 startTime = System.currentTimeMillis();
1605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 super.onNewIntent(intent);
1607
1608 // Close the menu
1609 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001610 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001611 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001612
Jamie Genniscb222e82012-10-23 15:44:26 -07001613 final boolean alreadyOnHome =
1614 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001615 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001616
Jamie Genniscb222e82012-10-23 15:44:26 -07001617 Runnable processIntent = new Runnable() {
1618 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001619 if (mWorkspace == null) {
1620 // Can be cases where mWorkspace is null, this prevents a NPE
1621 return;
1622 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001623 Folder openFolder = mWorkspace.getOpenFolder();
1624 // In all these cases, only animate if we're already on home
1625 mWorkspace.exitWidgetResizeMode();
1626 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1627 openFolder == null) {
1628 mWorkspace.moveToDefaultScreen(true);
1629 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001630
Jamie Genniscb222e82012-10-23 15:44:26 -07001631 closeFolder();
1632 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001633
Jamie Genniscb222e82012-10-23 15:44:26 -07001634 // If we are already on home, then just animate back to the workspace,
1635 // otherwise, just wait until onResume to set the state back to Workspace
1636 if (alreadyOnHome) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02001637 showWorkspaceAndExitOverviewMode();
Jamie Genniscb222e82012-10-23 15:44:26 -07001638 } else {
1639 mOnResumeState = State.WORKSPACE;
1640 }
1641
1642 final View v = getWindow().peekDecorView();
1643 if (v != null && v.getWindowToken() != null) {
1644 InputMethodManager imm = (InputMethodManager)getSystemService(
1645 INPUT_METHOD_SERVICE);
1646 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1647 }
1648
1649 // Reset AllApps to its initial state
1650 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1651 mAppsCustomizeTabHost.reset();
1652 }
1653 }
1654 };
1655
1656 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1657 // Delay processing of the intent to allow the status bar animation to finish
1658 // first in order to avoid janky animations.
1659 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001660 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001661 // Process the intent immediately.
1662 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001663 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 }
Michael Jurka447bf842013-05-15 14:52:15 +02001666 if (DEBUG_RESUME_TIME) {
1667 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 }
1670
Michael Jurka336fd4f2013-09-12 00:05:02 +02001671 protected void showWorkspaceAndExitOverviewMode() {
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001672 showWorkspace(true);
1673 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02001674 mWorkspace.exitOverviewMode(true);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001675 }
1676 }
1677
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001679 public void onRestoreInstanceState(Bundle state) {
1680 super.onRestoreInstanceState(state);
1681 for (int page: mSynchronouslyBoundPages) {
1682 mWorkspace.restoreInstanceStateForChild(page);
1683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 }
1685
1686 @Override
1687 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001688 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001689 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690
Michael Jurka883f55b2010-10-21 15:47:14 -07001691 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001692 // We close any open folder since it will not be re-opened, and we need to make sure
1693 // this state is reflected.
1694 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695
Adam Cohendcd297f2013-06-18 13:13:40 -07001696 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001697 mWaitingForResult) {
1698 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001699 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001700 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1701 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001702 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1703 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1704 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 }
1706
1707 if (mFolderInfo != null && mWaitingForResult) {
1708 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1709 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1710 }
Michael Jurka946ad472010-07-09 18:05:18 -07001711
Winson Chung785d2eb2011-04-14 16:08:02 -07001712 // Save the current AppsCustomize tab
1713 if (mAppsCustomizeTabHost != null) {
1714 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1715 if (currentTabTag != null) {
1716 outState.putString("apps_customize_currentTab", currentTabTag);
1717 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001718 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1719 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001720 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
1722
1723 @Override
1724 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001726
Winson Chunge7a03942011-08-05 15:05:12 -07001727 // Remove all pending runnables
1728 mHandler.removeMessages(ADVANCE_MSG);
1729 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001730 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001731
Winson Chungcd2b0142011-06-08 16:02:26 -07001732 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001733 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001734 mModel.stopLoader();
1735 app.setLauncher(null);
1736
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001738 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001740 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001742 mAppWidgetHost = null;
1743
1744 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745
1746 TextKeyListener.getInstance().release();
1747
Winson Chung81b52252012-08-27 15:34:29 -07001748 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1749 // to prevent leaking Launcher activities on orientation change.
1750 if (mModel != null) {
1751 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1752 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001753
1754 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001755 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001756
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001757 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001758 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1759 mWorkspace.removeAllViews();
1760 mWorkspace = null;
1761 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001762
Michael Jurka2ecf9952012-06-18 12:52:28 -07001763 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 }
1765
Adam Cohena9cf38f2011-05-02 15:36:58 -07001766 public DragController getDragController() {
1767 return mDragController;
1768 }
1769
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 @Override
1771 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001772 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 super.startActivityForResult(intent, requestCode);
1774 }
1775
Winson Chung70d72102011-08-12 11:24:35 -07001776 /**
1777 * Indicates that we want global search for this activity by setting the globalSearch
1778 * argument for {@link #startSearch} to true.
1779 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001781 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001783
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001784 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001785
Karl Rosaen138a0412009-04-23 19:00:21 -07001786 if (initialQuery == null) {
1787 // Use any text typed in the launcher as the initial query
1788 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 if (appSearchData == null) {
1791 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001792 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 }
Winson Chungadf0c182012-08-23 14:59:07 -07001794 Rect sourceBounds = new Rect();
1795 if (mSearchDropTargetBar != null) {
1796 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1797 }
Romain Guycbb89e42009-06-08 15:52:54 -07001798
Bjorn Bringertc459e522013-06-07 19:36:01 +01001799 startSearch(initialQuery, selectInitialQuery,
1800 appSearchData, sourceBounds);
1801 }
1802
1803 public void startSearch(String initialQuery,
1804 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001805 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001806 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001807 }
1808
1809 /**
1810 * Starts the global search activity. This code is a copied from SearchManager
1811 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001812 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001813 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001814 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001815 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1816 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1817 if (globalSearchActivity == null) {
1818 Log.w(TAG, "No global search activity found.");
1819 return;
1820 }
1821 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1822 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1823 intent.setComponent(globalSearchActivity);
1824 // Make sure that we have a Bundle to put source in
1825 if (appSearchData == null) {
1826 appSearchData = new Bundle();
1827 } else {
1828 appSearchData = new Bundle(appSearchData);
1829 }
1830 // Set source to package name of app that starts global search, if not set already.
1831 if (!appSearchData.containsKey("source")) {
1832 appSearchData.putString("source", getPackageName());
1833 }
1834 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1835 if (!TextUtils.isEmpty(initialQuery)) {
1836 intent.putExtra(SearchManager.QUERY, initialQuery);
1837 }
1838 if (selectInitialQuery) {
1839 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1840 }
1841 intent.setSourceBounds(sourceBounds);
1842 try {
1843 startActivity(intent);
1844 } catch (ActivityNotFoundException ex) {
1845 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
1848
Winson Chung70d72102011-08-12 11:24:35 -07001849 @Override
1850 public boolean onCreateOptionsMenu(Menu menu) {
1851 if (isWorkspaceLocked()) {
1852 return false;
1853 }
1854
1855 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001856
1857 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1858 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1859 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001860 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1861 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1862 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001863 String helpUrl = getString(R.string.help_url);
1864 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001865 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1866 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1867
Winson Chung70d72102011-08-12 11:24:35 -07001868 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1869 .setIcon(android.R.drawable.ic_menu_gallery)
1870 .setAlphabeticShortcut('W');
1871 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1872 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001873 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001874 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001875 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1876 .setIcon(android.R.drawable.ic_menu_preferences)
1877 .setIntent(settings)
1878 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001879 if (!helpUrl.isEmpty()) {
1880 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1881 .setIcon(android.R.drawable.ic_menu_help)
1882 .setIntent(help)
1883 .setAlphabeticShortcut('H');
1884 }
Winson Chung70d72102011-08-12 11:24:35 -07001885 return true;
1886 }
1887
1888 @Override
1889 public boolean onPrepareOptionsMenu(Menu menu) {
1890 super.onPrepareOptionsMenu(menu);
1891
1892 if (mAppsCustomizeTabHost.isTransitioning()) {
1893 return false;
1894 }
1895 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1896 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1897
1898 return true;
1899 }
1900
1901 @Override
1902 public boolean onOptionsItemSelected(MenuItem item) {
1903 switch (item.getItemId()) {
1904 case MENU_WALLPAPER_SETTINGS:
1905 startWallpaper();
1906 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001907 }
1908
1909 return super.onOptionsItemSelected(item);
1910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001912 @Override
1913 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001914 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001915 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001916 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001917 }
1918
Joe Onorato9c1289c2009-08-17 11:03:03 -04001919 public boolean isWorkspaceLocked() {
1920 return mWorkspaceLoading || mWaitingForResult;
1921 }
1922
Michael Jurka0280c3b2010-09-17 15:00:07 -07001923 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001924 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001925 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001926 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1927 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001928 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001929 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001930 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001931
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001932 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1933 AppWidgetProviderInfo appWidgetInfo) {
1934 if (appWidgetInfo.configure != null) {
1935 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001936
Bjorn Bringert7984c942009-12-09 15:38:25 +00001937 // Launch over to configure widget, if needed
1938 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001939 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001940 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001941 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001943 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001944 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001945 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001946 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001947 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949 }
Romain Guycbb89e42009-06-08 15:52:54 -07001950
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001951 protected void moveToCustomContentScreen(boolean animate) {
1952 mWorkspace.moveToCustomContentScreen(animate);
1953 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001954 /**
1955 * Process a shortcut drop.
1956 *
1957 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001958 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001959 * @param cell The cell it should be added to, optional
1960 * @param position The location on the screen where it was dropped, optional
1961 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001962 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001963 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001964 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001965 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001966 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001967 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001968
1969 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001970 mPendingAddInfo.cellX = cell[0];
1971 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001972 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001973
1974 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1975 createShortcutIntent.setComponent(componentName);
1976 processShortcut(createShortcutIntent);
1977 }
1978
Adam Cohenfbba09b2011-07-18 21:43:05 -07001979 /**
1980 * Process a widget drop.
1981 *
1982 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001983 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001984 * @param cell The cell it should be added to, optional
1985 * @param position The location on the screen where it was dropped, optional
1986 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001988 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001989 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001990 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001991 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001992 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001993 mPendingAddInfo.minSpanX = info.minSpanX;
1994 mPendingAddInfo.minSpanY = info.minSpanY;
1995
Adam Cohenfbba09b2011-07-18 21:43:05 -07001996 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001997 mPendingAddInfo.cellX = cell[0];
1998 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001999 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002000 if (span != null) {
2001 mPendingAddInfo.spanX = span[0];
2002 mPendingAddInfo.spanY = span[1];
2003 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002004
Adam Cohened66b2b2012-01-23 17:28:51 -08002005 AppWidgetHostView hostView = info.boundWidget;
2006 int appWidgetId;
2007 if (hostView != null) {
2008 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002009 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002010 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002011 // In this case, we either need to start an activity to get permission to bind
2012 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002013 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002014 Bundle options = info.bindOptions;
2015
2016 boolean success = false;
2017 if (options != null) {
2018 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2019 info.componentName, options);
2020 } else {
2021 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2022 info.componentName);
2023 }
2024 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002025 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002026 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002027 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002028 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2029 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2030 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002031 // TODO: we need to make sure that this accounts for the options bundle.
2032 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002033 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2034 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002035 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002036 }
2037
Joe Onoratodeb98af2010-02-19 14:59:39 -08002038 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002039 // Handle case where user selected "Applications"
2040 String applicationName = getResources().getString(R.string.group_applications);
2041 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002042
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002043 if (applicationName != null && applicationName.equals(shortcutName)) {
2044 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2045 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002046
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002047 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2048 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002049 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002050 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002051 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002052 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
2055
Winson Chung24ab2f12010-09-16 14:10:47 -07002056 void processWallpaper(Intent intent) {
2057 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2058 }
2059
Adam Cohendcd297f2013-06-18 13:13:40 -07002060 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002061 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002062 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 folderInfo.title = getText(R.string.folder_name);
2064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002066 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002067 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002068 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069
2070 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002071 FolderIcon newFolder =
2072 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002073 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002074 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002075 // Force measure the new folder icon
2076 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2077 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002078 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 }
Romain Guycbb89e42009-06-08 15:52:54 -07002080
Joe Onorato9c1289c2009-08-17 11:03:03 -04002081 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002082 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083 }
2084
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002085 protected void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002086 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002088 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002089 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 }
2091
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002092 protected ComponentName getWallpaperPickerComponent() {
2093 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2094 }
2095
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002096 /**
2097 * Registers various content observers. The current implementation registers
2098 * only a favorites observer to keep track of the favorites applications.
2099 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002100 private void registerContentObservers() {
2101 ContentResolver resolver = getContentResolver();
2102 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2103 true, mWidgetObserver);
2104 }
2105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 @Override
2107 public boolean dispatchKeyEvent(KeyEvent event) {
2108 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2109 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002111 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002112 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002113 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002114 dumpState();
2115 return true;
2116 }
2117 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002118 }
2119 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2120 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002121 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 return true;
2123 }
2124 }
2125
2126 return super.dispatchKeyEvent(event);
2127 }
2128
Joe Onorato88ec0992009-11-19 13:16:06 -08002129 @Override
2130 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002131 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002132 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002133 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002134 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002135 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002136 Folder openFolder = mWorkspace.getOpenFolder();
2137 if (openFolder.isEditingName()) {
2138 openFolder.dismissEditingName();
2139 } else {
2140 closeFolder();
2141 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002142 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002143 mWorkspace.exitWidgetResizeMode();
2144
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002145 // Back button is a no-op here, but give at least some feedback for the button press
2146 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002147 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002148 }
2149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002151 * Re-listen when widgets are reset.
2152 */
2153 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002154 if (mAppWidgetHost != null) {
2155 mAppWidgetHost.startListening();
2156 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002157 }
2158
2159 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 * Launches the intent referred by the clicked shortcut.
2161 *
2162 * @param v The view representing the clicked shortcut.
2163 */
2164 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002165 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2166 // view has detached (it's possible for this to happen if the view is removed mid touch).
2167 if (v.getWindowToken() == null) {
2168 return;
2169 }
2170
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002171 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002172 return;
2173 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002174
Adam Cohenf358a4b2013-07-23 16:47:31 -07002175 if (v instanceof PageIndicator) {
2176 if (!mWorkspace.isInOverviewMode()) {
2177 mWorkspace.enterOverviewMode();
2178 }
2179 return;
2180 }
2181
2182 if (v instanceof CellLayout) {
2183 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002184 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002185 }
2186 }
2187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002189 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002191 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2192 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002193
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002194 // Check for special shortcuts
2195 if (intent.getComponent() != null) {
2196 final String shortcutClass = intent.getComponent().getClassName();
2197
2198 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chungc58497e2013-09-03 17:48:37 -07002199 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002200 return;
2201 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2202 MemoryDumpActivity.startDump(this);
2203 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002204 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2205 toggleShowWeightWatcher();
2206 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002207 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002208 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002209
2210 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002211 int[] pos = new int[2];
2212 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002213 intent.setSourceBounds(new Rect(pos[0], pos[1],
2214 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002215
2216 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002217
Daniel Sandlerff02d492013-08-05 02:12:05 -04002218 mStats.recordLaunch(intent, shortcut);
2219
Michael Jurkaddd62e92011-02-16 17:49:14 -08002220 if (success && v instanceof BubbleTextView) {
2221 mWaitingForResume = (BubbleTextView) v;
2222 mWaitingForResume.setStayPressed(true);
2223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002225 if (v instanceof FolderIcon) {
2226 FolderIcon fi = (FolderIcon) v;
2227 handleFolderClick(fi);
2228 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002229 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002230 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002231 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002232 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002233 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 }
2236 }
2237
Michael Jurka0e260592010-06-30 17:07:39 -07002238 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002239 return false;
2240 }
2241
Michael Jurkaaf442092010-06-10 17:01:57 -07002242 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002243 * Event handler for the search button
2244 *
2245 * @param v The view that was clicked.
2246 */
2247 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002248 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2249
Amith Yamasania135ba82011-08-09 17:42:01 -07002250 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002251 }
2252
2253 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002254 * Event handler for the voice button
2255 *
2256 * @param v The view that was clicked.
2257 */
2258 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002259 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002260
Bjorn Bringertc459e522013-06-07 19:36:01 +01002261 startVoice();
2262 }
2263
2264 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002265 try {
2266 final SearchManager searchManager =
2267 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2268 ComponentName activityName = searchManager.getGlobalSearchActivity();
2269 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002270 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002271 if (activityName != null) {
2272 intent.setPackage(activityName.getPackageName());
2273 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002274 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002275 } catch (ActivityNotFoundException e) {
2276 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002277 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002278 startActivitySafely(null, intent, "onClickVoiceButton");
2279 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002280 }
2281
2282 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002283 * Event handler for the "grid" button that appears on the home screen, which
2284 * enters all apps mode.
2285 *
2286 * @param v The view that was clicked.
2287 */
2288 public void onClickAllAppsButton(View v) {
Winson Chungc58497e2013-09-03 17:48:37 -07002289 showAllApps(true, AppsCustomizePagedView.ContentType.Applications);
Michael Jurka5130e402011-10-13 04:55:35 -07002290 }
2291
2292 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002293 // Provide the same haptic feedback that the system offers for virtual keys.
2294 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002295 }
2296
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002297 public void onClickAppMarketButton(View v) {
2298 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002299 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002300 } else {
2301 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002302 }
2303 }
2304
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002305 void startApplicationDetailsActivity(ComponentName componentName) {
2306 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002307 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2308 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002309 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002310 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002311 }
2312
Michael Jurka1e2f4652013-07-08 18:03:46 -07002313 // returns true if the activity was started
2314 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002315 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002316 // System applications cannot be installed. For now, show a toast explaining that.
2317 // We may give them the option of disabling apps this way.
2318 int messageId = R.string.uninstall_system_app_text;
2319 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002320 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002321 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002322 String packageName = componentName.getPackageName();
2323 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002324 Intent intent = new Intent(
2325 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002326 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2327 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002328 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002329 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002330 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002331 }
2332
Michael Jurka86a720e2012-05-09 11:23:23 -07002333 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002337 // Only launch using the new animation if the shortcut has not opted out (this is a
2338 // private contract between launcher and may be ignored in the future).
2339 boolean useLaunchAnimation = (v != null) &&
2340 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2341 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002342 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2343 v.getMeasuredWidth(), v.getMeasuredHeight());
2344
2345 startActivity(intent, opts.toBundle());
2346 } else {
2347 startActivity(intent);
2348 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002349 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 } catch (SecurityException e) {
2351 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002352 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002354 "or use the exported attribute for this activity. "
2355 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002357 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002359
Michael Jurka86a720e2012-05-09 11:23:23 -07002360 boolean startActivitySafely(View v, Intent intent, Object tag) {
2361 boolean success = false;
2362 try {
2363 success = startActivity(v, intent, tag);
2364 } catch (ActivityNotFoundException e) {
2365 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2366 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2367 }
2368 return success;
2369 }
2370
Adam Cohena9cf38f2011-05-02 15:36:58 -07002371 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002372 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002373 Folder openFolder = mWorkspace.getFolderForTag(info);
2374
2375 // If the folder info reports that the associated folder is open, then verify that
2376 // it is actually opened. There have been a few instances where this gets out of sync.
2377 if (info.opened && openFolder == null) {
2378 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002379 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002380 info.opened = false;
2381 }
2382
Adam Cohenfb91f302012-06-11 15:45:18 -07002383 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 // Close any open folder
2385 closeFolder();
2386 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002387 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 } else {
2389 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 int folderScreen;
2391 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002392 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 // .. and close it
2394 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002395 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 // Close any folder open on the current screen
2397 closeFolder();
2398 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002399 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 }
2401 }
2402 }
2403 }
2404
Adam Cohen268c4752012-06-06 17:47:33 -07002405 /**
2406 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2407 * in the DragLayer in the exact absolute location of the original FolderIcon.
2408 */
2409 private void copyFolderIconToImage(FolderIcon fi) {
2410 final int width = fi.getMeasuredWidth();
2411 final int height = fi.getMeasuredHeight();
2412
2413 // Lazy load ImageView, Bitmap and Canvas
2414 if (mFolderIconImageView == null) {
2415 mFolderIconImageView = new ImageView(this);
2416 }
2417 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2418 mFolderIconBitmap.getHeight() != height) {
2419 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2420 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2421 }
2422
2423 DragLayer.LayoutParams lp;
2424 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2425 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2426 } else {
2427 lp = new DragLayer.LayoutParams(width, height);
2428 }
2429
Adam Cohen307fe232012-08-16 17:55:58 -07002430 // The layout from which the folder is being opened may be scaled, adjust the starting
2431 // view size by this scale factor.
2432 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002433 lp.customPosition = true;
2434 lp.x = mRectForFolderAnimation.left;
2435 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002436 lp.width = (int) (scale * width);
2437 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002438
2439 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2440 fi.draw(mFolderIconCanvas);
2441 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002442 if (fi.getFolder() != null) {
2443 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2444 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002445 }
Adam Cohen268c4752012-06-06 17:47:33 -07002446 // Just in case this image view is still in the drag layer from a previous animation,
2447 // we remove it and re-add it.
2448 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2449 mDragLayer.removeView(mFolderIconImageView);
2450 }
2451 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002452 if (fi.getFolder() != null) {
2453 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002454 }
Adam Cohen268c4752012-06-06 17:47:33 -07002455 }
2456
Adam Cohen2801caf2011-05-13 20:57:39 -07002457 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002458 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002459 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2460 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2461 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2462
Adam Cohenc51934b2011-07-26 21:07:43 -07002463 FolderInfo info = (FolderInfo) fi.getTag();
2464 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2465 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002466 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2467 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002468 }
2469
Adam Cohen268c4752012-06-06 17:47:33 -07002470 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2471 copyFolderIconToImage(fi);
2472 fi.setVisibility(View.INVISIBLE);
2473
Michael Jurka2ecf9952012-06-18 12:52:28 -07002474 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002475 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002476 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2477 oa.start();
2478 }
2479
Adam Cohen268c4752012-06-06 17:47:33 -07002480 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002481 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002482 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2483 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2484 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2485
Adam Cohen268c4752012-06-06 17:47:33 -07002486 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002487
Adam Cohen268c4752012-06-06 17:47:33 -07002488 // We remove and re-draw the FolderIcon in-case it has changed
2489 mDragLayer.removeView(mFolderIconImageView);
2490 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002491 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002492 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002493 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002494 oa.addListener(new AnimatorListenerAdapter() {
2495 @Override
2496 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002497 if (cl != null) {
2498 cl.clearFolderLeaveBehind();
2499 // Remove the ImageView copy of the FolderIcon and make the original visible.
2500 mDragLayer.removeView(mFolderIconImageView);
2501 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002502 }
2503 }
2504 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002505 oa.start();
2506 }
2507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002509 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 * is animated relative to the specified View. If the View is null, no animation
2511 * is played.
2512 *
2513 * @param folderInfo The FolderInfo describing the folder to open.
2514 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002515 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002516 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002517 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518
Adam Cohena9cf38f2011-05-02 15:36:58 -07002519 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520
Adam Cohen4554ee12011-08-03 16:13:21 -07002521 // Just verify that the folder hasn't already been added to the DragLayer.
2522 // There was a one-off crash where the folder had a parent already.
2523 if (folder.getParent() == null) {
2524 mDragLayer.addView(folder);
2525 mDragController.addDropTarget((DropTarget) folder);
2526 } else {
2527 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2528 folder.getParent() + ").");
2529 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002530 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002531 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002532
2533 // Notify the accessibility manager that this folder "window" has appeared and occluded
2534 // the workspace items
2535 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2536 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002537 }
2538
2539 public void closeFolder() {
2540 Folder folder = mWorkspace.getOpenFolder();
2541 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002542 if (folder.isEditingName()) {
2543 folder.dismissEditingName();
2544 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002545 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002546
2547 // Dismiss the folder cling
2548 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002549 }
2550 }
2551
2552 void closeFolder(Folder folder) {
2553 folder.getInfo().opened = false;
2554
2555 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2556 if (parent != null) {
2557 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2558 shrinkAndFadeInFolderIcon(fi);
2559 }
2560 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002561
2562 // Notify the accessibility manager that this folder "window" has disappeard and no
2563 // longer occludeds the workspace items
2564 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565 }
2566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002568 if (!isDraggingEnabled()) return false;
2569 if (isWorkspaceLocked()) return false;
2570 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571
2572 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002573 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 }
2575
Michael Jurka0280c3b2010-09-17 15:00:07 -07002576 resetAddInfo();
2577 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002579 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 return true;
2581 }
2582
Winson Chung3d503fb2011-07-13 17:25:49 -07002583 // The hotseat touch handling does not go through Workspace, and we always allow long press
2584 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002585 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002586 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2587 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002588 if (itemUnderLongClick == null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07002589 if (mWorkspace.hasNonCustomEmptyScreens()) {
2590 // User long pressed on empty space
2591 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2592 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2593 // Disabling reordering until we sort out some issues.
2594 if (mWorkspace.isInOverviewMode()) {
2595 mWorkspace.startReordering(v);
2596 } else {
2597 mWorkspace.enterOverviewMode();
2598 }
Adam Cohendedbd962013-07-11 14:21:49 -07002599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002601 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002603 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 }
2605 }
2606 }
2607 return true;
2608 }
2609
Winson Chung3d503fb2011-07-13 17:25:49 -07002610 boolean isHotseatLayout(View layout) {
2611 return mHotseat != null && layout != null &&
2612 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2613 }
2614 Hotseat getHotseat() {
2615 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002616 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002617 View getOverviewPanel() {
2618 return mOverviewPanel;
2619 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002620 SearchDropTargetBar getSearchBar() {
2621 return mSearchDropTargetBar;
2622 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002623
Winson Chung3d503fb2011-07-13 17:25:49 -07002624 /**
2625 * Returns the CellLayout of the specified container at the specified screen.
2626 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002627 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002628 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2629 if (mHotseat != null) {
2630 return mHotseat.getLayout();
2631 } else {
2632 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002633 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002634 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002635 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002636 }
2637 }
2638
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002639 Workspace getWorkspace() {
2640 return mWorkspace;
2641 }
2642
Daniel Sandler843e8602010-06-07 14:59:01 -04002643 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002644 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002645 }
2646
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002647 /**
2648 * Helper method for the cameraZoomIn/cameraZoomOut animations
2649 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002650 * @param scaleFactor The scale factor used for the zoom
2651 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002652 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002653 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002654 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002656
Winson Chung18f41f82012-05-09 13:28:10 -07002657 void disableWallpaperIfInAllApps() {
2658 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002659 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002660 if (mAppsCustomizeTabHost != null &&
2661 !mAppsCustomizeTabHost.isTransitioning()) {
2662 updateWallpaperVisibility(false);
2663 }
2664 }
2665 }
2666
Craig Mautner360310b2012-10-26 15:13:08 -07002667 private void setWorkspaceBackground(boolean workspace) {
2668 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002669 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002670 }
2671
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002672 void updateWallpaperVisibility(boolean visible) {
2673 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2674 int curflags = getWindow().getAttributes().flags
2675 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2676 if (wpflags != curflags) {
2677 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2678 }
Craig Mautner360310b2012-10-26 15:13:08 -07002679 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002680 }
2681
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002682 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2683 if (v instanceof LauncherTransitionable) {
2684 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2685 }
2686 }
2687
Michael Jurkabed61d22012-02-14 22:51:29 -08002688 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2689 if (v instanceof LauncherTransitionable) {
2690 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2691 }
Winson Chung70442722012-02-10 15:43:22 -08002692
2693 // Update the workspace transition step as well
2694 dispatchOnLauncherTransitionStep(v, 0f);
2695 }
2696
2697 private void dispatchOnLauncherTransitionStep(View v, float t) {
2698 if (v instanceof LauncherTransitionable) {
2699 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2700 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002701 }
2702
2703 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2704 if (v instanceof LauncherTransitionable) {
2705 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2706 }
Winson Chung70442722012-02-10 15:43:22 -08002707
2708 // Update the workspace transition step as well
2709 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002710 }
2711
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002712 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002713 * Things to test when changing the following seven functions.
2714 * - Home from workspace
2715 * - from center screen
2716 * - from other screens
2717 * - Home from all apps
2718 * - from center screen
2719 * - from other screens
2720 * - Back from all apps
2721 * - from center screen
2722 * - from other screens
2723 * - Launch app from workspace and quit
2724 * - with back
2725 * - with home
2726 * - Launch app from all apps and quit
2727 * - with back
2728 * - with home
2729 * - Go to a screen that's not the default, then all
2730 * apps, and launch and app, and go back
2731 * - with back
2732 * -with home
2733 * - On workspace, long press power and go back
2734 * - with back
2735 * - with home
2736 * - On all apps, long press power and go back
2737 * - with back
2738 * - with home
2739 * - On workspace, power off
2740 * - On all apps, power off
2741 * - Launch an app and turn off the screen while in that app
2742 * - Go back with home key
2743 * - Go back with back key TODO: make this not go to workspace
2744 * - From all apps
2745 * - From workspace
2746 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2747 * - From all apps
2748 * - From the center workspace
2749 * - From another workspace
2750 */
2751
2752 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002753 * Zoom the camera out from the workspace to reveal 'toView'.
2754 * Assumes that the view to show is anchored at either the very top or very bottom
2755 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002756 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002757 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002758 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2759 showAppsCustomizeHelper(animated, springLoaded, contentType);
2760 }
2761 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2762 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002763 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002764 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002765 mStateAnimation.cancel();
2766 mStateAnimation = null;
2767 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002768 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002769
Winson Chungf0ea4d32011-06-06 14:27:16 -07002770 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2771 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2772 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002773 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002774 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002775 final int startDelay =
2776 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002777
Michael Jurkab3e22d92011-10-31 15:58:33 -07002778 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002779
Michael Jurkad74c9842011-07-10 12:44:21 -07002780 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002781 Animator workspaceAnim =
2782 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002783 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2784 // Set the content type for the all apps space
2785 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2786 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002787
2788 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002789 toView.setScaleX(scale);
2790 toView.setScaleY(scale);
2791 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2792 scaleAnim.
2793 scaleX(1f).scaleY(1f).
2794 setDuration(duration).
2795 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002796
Winson Chungf0ea4d32011-06-06 14:27:16 -07002797 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002798 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002799 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002800 .ofFloat(toView, "alpha", 0f, 1f)
2801 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002802 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002803 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2804 @Override
2805 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002806 if (animation == null) {
2807 throw new RuntimeException("animation is null");
2808 }
Winson Chung70442722012-02-10 15:43:22 -08002809 float t = (Float) animation.getAnimatedValue();
2810 dispatchOnLauncherTransitionStep(fromView, t);
2811 dispatchOnLauncherTransitionStep(toView, t);
2812 }
2813 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002814
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002815 // toView should appear right at the end of the workspace shrink
2816 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002817 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002818 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002819 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002820
2821 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002822 boolean animationCancelled = false;
2823
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002824 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002825 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002826 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002827 // Prepare the position
2828 toView.setTranslationX(0.0f);
2829 toView.setTranslationY(0.0f);
2830 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002831 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002832 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002833 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002834 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002835 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2836 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002837
Adam Cohenf4ddea32011-09-12 13:46:42 -07002838 if (!animationCancelled) {
2839 updateWallpaperVisibility(false);
2840 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002841
2842 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002843 if (mSearchDropTargetBar != null) {
2844 mSearchDropTargetBar.hideSearchBar(false);
2845 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002846 }
2847
Adam Cohencff6af82011-09-13 14:51:53 -07002848 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002849 public void onAnimationCancel(Animator animation) {
2850 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002851 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002852 });
2853
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002854 if (workspaceAnim != null) {
2855 mStateAnimation.play(workspaceAnim);
2856 }
Michael Jurka1899a362011-11-03 13:50:45 -07002857
2858 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002859
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002860 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2861 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002862
2863 // If any of the objects being animated haven't been measured/laid out
2864 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002865 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002866 (mWorkspace.getMeasuredWidth() == 0) ||
2867 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002868 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002869 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002870
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002871 final AnimatorSet stateAnimation = mStateAnimation;
2872 final Runnable startAnimRunnable = new Runnable() {
2873 public void run() {
2874 // Check that mStateAnimation hasn't changed while
2875 // we waited for a layout/draw pass
2876 if (mStateAnimation != stateAnimation)
2877 return;
2878 setPivotsForZoom(toView, scale);
2879 dispatchOnLauncherTransitionStart(fromView, animated, false);
2880 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002881 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002882 }
2883 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002884 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002885 final ViewTreeObserver observer = toView.getViewTreeObserver();
2886 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2887 public void onGlobalLayout() {
2888 startAnimRunnable.run();
2889 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2890 }
2891 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002892 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002893 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002894 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002895 } else {
2896 toView.setTranslationX(0.0f);
2897 toView.setTranslationY(0.0f);
2898 toView.setScaleX(1.0f);
2899 toView.setScaleY(1.0f);
2900 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002901 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002902
Daniel Sandlere4f98912013-06-25 15:13:26 -04002903 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002904 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002905 if (mSearchDropTargetBar != null) {
2906 mSearchDropTargetBar.hideSearchBar(false);
2907 }
Michael Jurkaabded662011-03-04 12:06:57 -08002908 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002909 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002910 dispatchOnLauncherTransitionStart(fromView, animated, false);
2911 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002912 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002913 dispatchOnLauncherTransitionStart(toView, animated, false);
2914 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002915 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002916 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002917 }
2918
2919 /**
2920 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002921 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002922 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002923 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002924 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2925 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002926
Michael Jurkab3e22d92011-10-31 15:58:33 -07002927 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002928 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002929 mStateAnimation.cancel();
2930 mStateAnimation = null;
2931 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002932 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002933
Winson Chungf0ea4d32011-06-06 14:27:16 -07002934 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002935 final int fadeOutDuration =
2936 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002937 final float scaleFactor = (float)
2938 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2939 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002940 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002941 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002942 if (toState == State.WORKSPACE) {
2943 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2944 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002945 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002946 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2947 workspaceAnim = mWorkspace.getChangeStateAnimation(
2948 Workspace.State.SPRING_LOADED, animated);
2949 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002950
Michael Jurkab3e22d92011-10-31 15:58:33 -07002951 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002952 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002953 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002954 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002955 final LauncherViewPropertyAnimator scaleAnim =
2956 new LauncherViewPropertyAnimator(fromView);
2957 scaleAnim.
2958 scaleX(scaleFactor).scaleY(scaleFactor).
2959 setDuration(duration).
2960 setInterpolator(new Workspace.ZoomInInterpolator());
2961
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002962 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002963 .ofFloat(fromView, "alpha", 1f, 0f)
2964 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002965 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002966 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2967 @Override
2968 public void onAnimationUpdate(ValueAnimator animation) {
2969 float t = 1f - (Float) animation.getAnimatedValue();
2970 dispatchOnLauncherTransitionStep(fromView, t);
2971 dispatchOnLauncherTransitionStep(toView, t);
2972 }
2973 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002974
Michael Jurka2ecf9952012-06-18 12:52:28 -07002975 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002976
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002977 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2978 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002979 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002980
2981 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002982 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002983 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002984 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002985 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002986 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2987 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002988 if (onCompleteRunnable != null) {
2989 onCompleteRunnable.run();
2990 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002991 mAppsCustomizeContent.updateCurrentPageScroll();
2992 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002993 }
2994 });
2995
Winson Chungf0ea4d32011-06-06 14:27:16 -07002996 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002997 if (workspaceAnim != null) {
2998 mStateAnimation.play(workspaceAnim);
2999 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003000 dispatchOnLauncherTransitionStart(fromView, animated, true);
3001 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003002 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003003 } else {
3004 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003005 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003006 dispatchOnLauncherTransitionStart(fromView, animated, true);
3007 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003008 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003009 dispatchOnLauncherTransitionStart(toView, animated, true);
3010 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003011 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003012 }
3013
Michael Jurkae326f182011-11-21 14:05:46 -08003014 @Override
3015 public void onTrimMemory(int level) {
3016 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003017 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003018 mAppsCustomizeTabHost.onTrimMemory();
3019 }
3020 }
3021
Winson Chung18f41f82012-05-09 13:28:10 -07003022 @Override
3023 public void onWindowFocusChanged(boolean hasFocus) {
3024 if (!hasFocus) {
3025 // When another window occludes launcher (like the notification shade, or recents),
3026 // ensure that we enable the wallpaper flag so that transitions are done correctly.
3027 updateWallpaperVisibility(true);
3028 } else {
3029 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07003030 mWorkspace.postDelayed(new Runnable() {
3031 @Override
3032 public void run() {
3033 disableWallpaperIfInAllApps();
3034 }
3035 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07003036 }
3037 }
3038
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003039 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003040 showWorkspace(animated, null);
3041 }
3042
3043 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003044 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003045 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003046 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003047 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003048
Winson Chungc7d2b602012-05-16 17:02:20 -07003049 // Show the search bar (only animate if we were showing the drop target bar in spring
3050 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003051 if (mSearchDropTargetBar != null) {
3052 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3053 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003054
Michael Jurkab3e22d92011-10-31 15:58:33 -07003055 // Set focus to the AppsCustomize button
3056 if (mAllAppsButton != null) {
3057 mAllAppsButton.requestFocus();
3058 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003059 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003060
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003061 // Change the state *after* we've called all the transition code
3062 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003063
Winson Chung5fb63472011-02-02 17:03:37 -08003064 // Resume the auto-advance of widgets
3065 mUserPresent = true;
3066 updateRunning();
3067
alanv1d4fde62012-10-17 13:15:47 -07003068 // Send an accessibility event to announce the context change
3069 getWindow().getDecorView()
3070 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003071
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003072 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003073 }
3074
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003075 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003076 }
3077
Winson Chungc58497e2013-09-03 17:48:37 -07003078 void showAllApps(boolean animated,
3079 AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003080 if (mState != State.WORKSPACE) return;
3081
Winson Chungc58497e2013-09-03 17:48:37 -07003082 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003083 mAppsCustomizeTabHost.requestFocus();
3084
Michael Jurkab3e22d92011-10-31 15:58:33 -07003085 // Change the state *after* we've called all the transition code
3086 mState = State.APPS_CUSTOMIZE;
3087
3088 // Pause the auto-advance of widgets until we are out of AllApps
3089 mUserPresent = false;
3090 updateRunning();
3091 closeFolder();
3092
3093 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003094 getWindow().getDecorView()
3095 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003096 }
3097
Adam Cohen7777d962011-08-18 18:58:38 -07003098 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003099 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003100 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003101 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003102 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003103 }
Adam Cohen7777d962011-08-18 18:58:38 -07003104
Adam Cohened66b2b2012-01-23 17:28:51 -08003105 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3106 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003107 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3108
Winson Chunge7a03942011-08-05 15:05:12 -07003109 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003110 @Override
3111 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003112 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003113 // Before we show workspace, hide all apps again because
3114 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3115 // clean up our state transition functions
3116 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003117 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003118 } else {
3119 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003120 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003121 }
3122 }, (extendedDelay ?
3123 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3124 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3125 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003126
Michael Jurkad3ef3062010-11-23 16:23:58 -08003127 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003128 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003129 final boolean animated = true;
3130 final boolean springLoaded = true;
3131 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003132 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003133 }
3134 // Otherwise, we are not in spring loaded mode, so don't do anything.
3135 }
3136
Michael Jurkab3e22d92011-10-31 15:58:33 -07003137 void lockAllApps() {
3138 // TODO
3139 }
3140
3141 void unlockAllApps() {
3142 // TODO
3143 }
3144
Winson Chungf0ea4d32011-06-06 14:27:16 -07003145 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003146 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003147 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003148 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003149 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003150 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003151 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003152 int duration = 0;
3153 if (mSearchDropTargetBar != null) {
3154 duration = mSearchDropTargetBar.getTransitionInDuration();
3155 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003156 mHotseat.animate().alpha(1f).setDuration(duration);
3157 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003158 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003159 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003160 }
3161 }
3162 }
3163
3164 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003165 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003166 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003167 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003168 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003169 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003170 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003171 int duration = 0;
3172 if (mSearchDropTargetBar != null) {
3173 duration = mSearchDropTargetBar.getTransitionOutDuration();
3174 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003175 mHotseat.animate().alpha(0f).setDuration(duration);
3176 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003177 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003178 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003179 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003180 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003181 }
3182
Patrick Dubroy5f445422011-02-18 14:35:21 -08003183 /**
3184 * Add an item from all apps or customize onto the given workspace screen.
3185 * If layout is null, add to the current screen.
3186 */
3187 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003188 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003189 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003190 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003191 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003192
Winson Chungdff8ebb2011-09-08 17:25:31 -07003193 /** Maps the current orientation to an index for referencing orientation correct global icons */
3194 private int getCurrentOrientationIndexForGlobalIcons() {
3195 // default - 0, landscape - 1
3196 switch (getResources().getConfiguration().orientation) {
3197 case Configuration.ORIENTATION_LANDSCAPE:
3198 return 1;
3199 default:
3200 return 0;
3201 }
3202 }
3203
Michael Jurkaae65ee32012-04-30 11:45:54 -07003204 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003205 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003206 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003207 // Look for the toolbar icon specified in the activity meta-data
3208 Bundle metaData = packageManager.getActivityInfo(
3209 activityName, PackageManager.GET_META_DATA).metaData;
3210 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003211 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003212 if (iconResId != 0) {
3213 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003214 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003215 }
3216 }
3217 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003218 // This can happen if the activity defines an invalid drawable
3219 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3220 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003221 } catch (Resources.NotFoundException nfe) {
3222 // This can happen if the activity defines an invalid drawable
3223 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3224 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003225 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003226 return null;
3227 }
3228
3229 // if successful in getting icon, return it; otherwise, set button to use default drawable
3230 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003231 int buttonId, ComponentName activityName, int fallbackDrawableId,
3232 String toolbarResourceName) {
3233 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003234 Resources r = getResources();
3235 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3236 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003237
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003238 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003239 // If we were unable to find the icon via the meta-data, use a generic one
3240 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003241 toolbarIcon = r.getDrawable(fallbackDrawableId);
3242 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003243 if (button != null) {
3244 button.setCompoundDrawables(toolbarIcon, null, null, null);
3245 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003246 return null;
3247 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003248 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003249 if (button != null) {
3250 button.setCompoundDrawables(toolbarIcon, null, null, null);
3251 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003252 return toolbarIcon.getConstantState();
3253 }
3254 }
3255
3256 // if successful in getting icon, return it; otherwise, set button to use default drawable
3257 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003258 int buttonId, ComponentName activityName, int fallbackDrawableId,
3259 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003260 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003261 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003262
Michael Jurka19e0fc52011-07-22 18:00:21 -07003263 if (button != null) {
3264 // If we were unable to find the icon via the meta-data, use a
3265 // generic one
3266 if (toolbarIcon == null) {
3267 button.setImageResource(fallbackDrawableId);
3268 } else {
3269 button.setImageDrawable(toolbarIcon);
3270 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003271 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003272
3273 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3274
Michael Jurka0423dcf2010-10-05 14:56:18 -07003275 }
3276
Winson Chung1b884092012-06-08 17:13:02 -07003277 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003278 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003279 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003280 }
3281
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003282 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003283 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003284 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003285 }
3286
Winson Chungbb185bd2011-11-21 12:31:42 -08003287 private void invalidatePressedFocusedStates(View container, View button) {
3288 if (container instanceof HolographicLinearLayout) {
3289 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3290 layout.invalidatePressedFocusedStates();
3291 } else if (button instanceof HolographicImageView) {
3292 HolographicImageView view = (HolographicImageView) button;
3293 view.invalidatePressedFocusedStates();
3294 }
3295 }
3296
Cristina Stancu476493b2013-08-07 17:20:14 +01003297 public View getQsbBar() {
3298 if (mQsbBar == null) {
3299 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3300 }
3301 return mQsbBar;
3302 }
3303
3304 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003305 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003306 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003307 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003308 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003309
Winson Chung1cad91e2011-05-25 17:41:01 -07003310 final SearchManager searchManager =
3311 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3312 ComponentName activityName = searchManager.getGlobalSearchActivity();
3313 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003314 int coi = getCurrentOrientationIndexForGlobalIcons();
3315 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003316 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3317 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3318 if (sGlobalSearchIcon[coi] == null) {
3319 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3320 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3321 TOOLBAR_ICON_METADATA_NAME);
3322 }
3323
Winson Chungc51db6a2011-10-05 11:44:49 -07003324 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3325 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003326 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003327 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003328 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003329 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003330 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3331 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3332 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003333 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003334 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003335 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003336 }
3337 }
3338
Cristina Stancu476493b2013-08-07 17:20:14 +01003339 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003340 final View searchButtonContainer = findViewById(R.id.search_button_container);
3341 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003342 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003343 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003344 }
3345
Cristina Stancu476493b2013-08-07 17:20:14 +01003346 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003347 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003348 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003349
Winson Chungc51db6a2011-10-05 11:44:49 -07003350 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003351 final SearchManager searchManager =
3352 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3353 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3354
3355 ComponentName activityName = null;
3356 if (globalSearchActivity != null) {
3357 // Check if the global search activity handles voice search
3358 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3359 intent.setPackage(globalSearchActivity.getPackageName());
3360 activityName = intent.resolveActivity(getPackageManager());
3361 }
3362
3363 if (activityName == null) {
3364 // Fallback: check if an activity other than the global search activity
3365 // resolves this
3366 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3367 activityName = intent.resolveActivity(getPackageManager());
3368 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003369 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003370 int coi = getCurrentOrientationIndexForGlobalIcons();
3371 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003372 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3373 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3374 if (sVoiceSearchIcon[coi] == null) {
3375 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3376 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3377 TOOLBAR_ICON_METADATA_NAME);
3378 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003379 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003380 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003381 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003382 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003383 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003384 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003385 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003386 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003387 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003388 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003389 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003390 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003391
Cristina Stancu476493b2013-08-07 17:20:14 +01003392 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003393 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3394 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003395 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003396 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003397 }
3398
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003399 public void setVoiceButtonProxyVisible(boolean visible) {
3400 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3401 if (voiceButtonProxy != null) {
3402 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3403 }
3404 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003405 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003406 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003407 */
3408 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003409 final View marketButton = findViewById(R.id.market_button);
3410 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3411 // Find the app market activity by resolving an intent.
3412 // (If multiple app markets are installed, it will return the ResolverActivity.)
3413 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003414 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003415 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003416 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003417 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003418 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3419 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003420 marketButton.setVisibility(View.VISIBLE);
3421 } else {
3422 // We should hide and disable the view so that we don't try and restore the visibility
3423 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3424 marketButton.setVisibility(View.GONE);
3425 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003426 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003427 }
3428
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003429 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003430 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3431 Resources r = getResources();
3432 Drawable marketIconDrawable = d.newDrawable(r);
3433 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3434 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3435 marketIconDrawable.setBounds(0, 0, w, h);
3436
3437 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003438 }
3439
Michael Jurkad7c28052012-04-27 15:43:36 -07003440 @Override
3441 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003442 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003443 final List<CharSequence> text = event.getText();
3444 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003445 // Populate event with a fake title based on the current state.
3446 if (mState == State.APPS_CUSTOMIZE) {
3447 text.add(getString(R.string.all_apps_button_label));
3448 } else {
3449 text.add(getString(R.string.all_apps_home_button_label));
3450 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003451 return result;
3452 }
3453
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003454 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003455 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003456 */
3457 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3458 @Override
3459 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003460 closeSystemDialogs();
3461 }
3462 }
3463
3464 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003465 * Receives notifications whenever the appwidgets are reset.
3466 */
3467 private class AppWidgetResetObserver extends ContentObserver {
3468 public AppWidgetResetObserver() {
3469 super(new Handler());
3470 }
3471
3472 @Override
3473 public void onChange(boolean selfChange) {
3474 onAppWidgetReset();
3475 }
3476 }
3477
3478 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003479 * If the activity is currently paused, signal that we need to run the passed Runnable
3480 * in onResume.
3481 *
3482 * This needs to be called from incoming places where resources might have been loaded
3483 * while we are paused. That is becaues the Configuration might be wrong
3484 * when we're not running, and if it comes back to what it was when we
3485 * were paused, we are not restarted.
3486 *
3487 * Implementation of the method from LauncherModel.Callbacks.
3488 *
3489 * @return true if we are currently paused. The caller might be able to
3490 * skip some work in that case since we will come back again.
3491 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003492 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003493 if (mPaused) {
3494 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003495 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003496 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003497 }
3498 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003499 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003500 return true;
3501 } else {
3502 return false;
3503 }
3504 }
3505
Michael Jurkac402cd92013-05-20 15:49:32 +02003506 private boolean waitUntilResume(Runnable run) {
3507 return waitUntilResume(run, false);
3508 }
3509
Michael Jurka1e2f4652013-07-08 18:03:46 -07003510 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003511 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003512 }
3513
Michael Jurka7607c2f2013-04-03 14:33:19 -07003514 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003515 * If the activity is currently paused, signal that we need to re-run the loader
3516 * in onResume.
3517 *
3518 * This needs to be called from incoming places where resources might have been loaded
3519 * while we are paused. That is becaues the Configuration might be wrong
3520 * when we're not running, and if it comes back to what it was when we
3521 * were paused, we are not restarted.
3522 *
3523 * Implementation of the method from LauncherModel.Callbacks.
3524 *
3525 * @return true if we are currently paused. The caller might be able to
3526 * skip some work in that case since we will come back again.
3527 */
3528 public boolean setLoadOnResume() {
3529 if (mPaused) {
3530 Log.i(TAG, "setLoadOnResume");
3531 mOnResumeNeedsLoad = true;
3532 return true;
3533 } else {
3534 return false;
3535 }
3536 }
3537
3538 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003540 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003541 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003542 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003543 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003544 } else {
3545 return SCREEN_COUNT / 2;
3546 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003548
Joe Onorato9c1289c2009-08-17 11:03:03 -04003549 /**
3550 * Refreshes the shortcuts shown on the workspace.
3551 *
3552 * Implementation of the method from LauncherModel.Callbacks.
3553 */
3554 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003555 // If we're starting binding all over again, clear any bind calls we'd postponed in
3556 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3557 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003558 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003559
Winson Chungd64d1762013-08-20 14:37:16 -07003560 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003561 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003562 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003563
Michael Jurka05bf644e2011-11-30 20:28:53 -08003564 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003565 if (mHotseat != null) {
3566 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003567 }
3568 }
3569
Adam Cohendcd297f2013-06-18 13:13:40 -07003570 @Override
3571 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003572 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003573
Adam Cohen5084cba2013-09-03 12:01:16 -07003574 // If there are no screens, we need to have an empty screen
3575 if (orderedScreenIds.size() == 0) {
3576 mWorkspace.addExtraEmptyScreen();
3577 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003578
3579 // Create the custom content page (this call updates mDefaultScreen which calls
3580 // setCurrentPage() so ensure that all pages are added before calling this)
3581 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3582 mWorkspace.createCustomContentPage();
3583 }
Winson Chung64359a52013-07-08 17:17:08 -07003584 }
3585
3586 @Override
3587 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003588 int count = orderedScreenIds.size();
3589 for (int i = 0; i < count; i++) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003590 Launcher.addDumpLog(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")", true);
Adam Cohen89bddfa2013-08-20 11:57:13 -07003591 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003592 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003593 }
3594
Adam Cohen39a06042013-07-19 14:30:12 -07003595 private boolean shouldShowWeightWatcher() {
3596 String spKey = LauncherAppState.getSharedPreferencesKey();
3597 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003598 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003599
3600 return show;
3601 }
3602
Adam Cohen4caf2982013-08-20 18:54:31 -07003603 private boolean emailSent() {
3604 String spKey = LauncherAppState.getSharedPreferencesKey();
3605 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3606 boolean show = sp.getBoolean(CORRUPTION_EMAIL_SENT_KEY, false);
3607 return show;
3608 }
3609
3610 private void setEmailSent(boolean sent) {
3611 String spKey = LauncherAppState.getSharedPreferencesKey();
3612 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3613
3614 SharedPreferences.Editor editor = sp.edit();
3615 editor.putBoolean(CORRUPTION_EMAIL_SENT_KEY, sent);
3616 editor.commit();
3617 }
3618
Adam Cohen39a06042013-07-19 14:30:12 -07003619 private void toggleShowWeightWatcher() {
3620 String spKey = LauncherAppState.getSharedPreferencesKey();
3621 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3622 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3623
3624 show = !show;
3625
3626 SharedPreferences.Editor editor = sp.edit();
3627 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3628 editor.commit();
3629
3630 if (mWeightWatcher != null) {
3631 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3632 }
3633 }
3634
Winson Chungd64d1762013-08-20 14:37:16 -07003635 public void bindAppsAdded(final ArrayList<Long> newScreens,
3636 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003637 final ArrayList<ItemInfo> addAnimated,
3638 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003639 Runnable r = new Runnable() {
3640 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003641 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003642 }
3643 };
3644 if (waitUntilResume(r)) {
3645 return;
3646 }
3647
Adam Cohen4caf2982013-08-20 18:54:31 -07003648 Launcher.addDumpLog(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -07003649
3650 // Add the new screens
3651 bindAddScreens(newScreens);
3652
3653 // We add the items without animation on non-visible pages, and with
3654 // animations on the new page (which we will try and snap to).
3655 if (!addNotAnimated.isEmpty()) {
3656 bindItems(addNotAnimated, 0,
3657 addNotAnimated.size(), false);
3658 }
3659 if (!addAnimated.isEmpty()) {
3660 bindItems(addAnimated, 0,
3661 addAnimated.size(), true);
3662 }
Winson Chungc58497e2013-09-03 17:48:37 -07003663
3664 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3665 addedApps != null && mAppsCustomizeContent != null) {
3666 mAppsCustomizeContent.addApps(addedApps);
3667 }
Winson Chungd64d1762013-08-20 14:37:16 -07003668 }
3669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003670 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003671 * Bind the items start-end from the list.
3672 *
3673 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003674 */
Winson Chung64359a52013-07-08 17:17:08 -07003675 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3676 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003677 Runnable r = new Runnable() {
3678 public void run() {
3679 bindItems(shortcuts, start, end, forceAnimateIcons);
3680 }
3681 };
3682 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003683 return;
3684 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003685
Winson Chungf0c6ae02012-03-21 16:10:31 -07003686 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003687 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3688 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003689 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003690 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003691 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003692 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003694
3695 // Short circuit if we are loading dock items for a configuration which has no dock
3696 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3697 mHotseat == null) {
3698 continue;
3699 }
3700
Joe Onorato9c1289c2009-08-17 11:03:03 -04003701 switch (item.itemType) {
3702 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3703 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003704 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003705 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003706
Winson Chung64359a52013-07-08 17:17:08 -07003707 /*
3708 * TODO: FIX collision case
3709 */
Winson Chungce376632013-07-11 16:12:41 -07003710 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3711 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3712 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3713 throw new RuntimeException("OCCUPIED");
3714 }
Winson Chung64359a52013-07-08 17:17:08 -07003715 }
3716
Adam Cohendcd297f2013-06-18 13:13:40 -07003717 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3718 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003719 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003720 // Animate all the applications up now
3721 shortcut.setAlpha(0f);
3722 shortcut.setScaleX(0f);
3723 shortcut.setScaleY(0f);
3724 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003725 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003728 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003729 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003730 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003731 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003732 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3733 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003735 default:
3736 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003738 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003739
Winson Chung997a9232013-07-24 15:33:46 -07003740 if (animateIcons) {
3741 // Animate to the correct page
3742 if (newShortcutsScreenId > -1) {
3743 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3744 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3745 if (newShortcutsScreenId != currentScreenId) {
3746 mWorkspace.snapToPage(newScreenIndex);
3747 }
3748 }
3749
Winson Chung64359a52013-07-08 17:17:08 -07003750 // We post the animation slightly delayed to prevent slowdowns when we are loading
3751 // right after we return to launcher.
3752 mWorkspace.postDelayed(new Runnable() {
3753 public void run() {
3754 anim.playTogether(bounceAnims);
3755 anim.start();
3756 }
3757 }, NEW_APPS_ANIMATION_DELAY);
3758 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003760 }
3761
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 /**
3763 * Implementation of the method from LauncherModel.Callbacks.
3764 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003765 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003766 Runnable r = new Runnable() {
3767 public void run() {
3768 bindFolders(folders);
3769 }
3770 };
3771 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003772 return;
3773 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003774 sFolders.clear();
3775 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003776 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003777
3778 /**
3779 * Add the views for a widget to the workspace.
3780 *
3781 * Implementation of the method from LauncherModel.Callbacks.
3782 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003783 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003784 Runnable r = new Runnable() {
3785 public void run() {
3786 bindAppWidget(item);
3787 }
3788 };
3789 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003790 return;
3791 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003792
Daniel Sandler843e8602010-06-07 14:59:01 -04003793 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3794 if (DEBUG_WIDGETS) {
3795 Log.d(TAG, "bindAppWidget: " + item);
3796 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003797 final Workspace workspace = mWorkspace;
3798
3799 final int appWidgetId = item.appWidgetId;
3800 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003801 if (DEBUG_WIDGETS) {
3802 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3803 }
3804
Joe Onorato9c1289c2009-08-17 11:03:03 -04003805 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3806
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003808 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003809
Adam Cohendcd297f2013-06-18 13:13:40 -07003810 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003811 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003812 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3813
Joe Onorato9c1289c2009-08-17 11:03:03 -04003814 workspace.requestLayout();
3815
Daniel Sandler843e8602010-06-07 14:59:01 -04003816 if (DEBUG_WIDGETS) {
3817 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3818 + (SystemClock.uptimeMillis()-start) + "ms");
3819 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003820 }
3821
Adam Cohen1462de32012-07-24 22:34:36 -07003822 public void onPageBoundSynchronously(int page) {
3823 mSynchronouslyBoundPages.add(page);
3824 }
3825
Joe Onorato9c1289c2009-08-17 11:03:03 -04003826 /**
3827 * Callback saying that there aren't any more items to bind.
3828 *
3829 * Implementation of the method from LauncherModel.Callbacks.
3830 */
Adam Cohene25af792013-06-06 23:08:25 -07003831 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003832 Runnable r = new Runnable() {
3833 public void run() {
3834 finishBindingItems(upgradePath);
3835 }
3836 };
3837 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003838 return;
3839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003840 if (mSavedState != null) {
3841 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003842 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003843 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003844 mSavedState = null;
3845 }
3846
Adam Cohen1462de32012-07-24 22:34:36 -07003847 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003848
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003849 // If we received the result of any pending adds while the loader was running (e.g. the
3850 // widget configuration forced an orientation change), process them now.
3851 for (int i = 0; i < sPendingAddList.size(); i++) {
3852 completeAdd(sPendingAddList.get(i));
3853 }
3854 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003855
Winson Chungc51db6a2011-10-05 11:44:49 -07003856 // Update the market app icon as necessary (the other icons will be managed in response to
3857 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003858 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003859
Winson Chungf0c6ae02012-03-21 16:10:31 -07003860 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003861 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003862 mWorkspace.getUniqueComponents(true, null);
3863 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003864 }
Adam Cohen99894d92013-06-14 11:22:59 -07003865
Adam Cohen66a01fd2013-06-11 12:48:00 -07003866 mWorkspace.post(new Runnable() {
3867 @Override
3868 public void run() {
3869 onFinishBindingItems();
3870 }
3871 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003872
3873 // Write all the logs to disk
3874 Launcher.addDumpLog(TAG, "10249126 - finishBindingItems() - dumping logs to disk", true);
3875 dumpLogsToLocalData(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003876 }
3877
Winson Chunga2413752012-04-03 14:22:34 -07003878 private boolean canRunNewAppsAnimation() {
3879 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3880 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3881 }
3882
Winson Chungc9168342013-06-26 14:54:55 -07003883 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3884 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3885 PropertyValuesHolder.ofFloat("alpha", 1f),
3886 PropertyValuesHolder.ofFloat("scaleX", 1f),
3887 PropertyValuesHolder.ofFloat("scaleY", 1f));
3888 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3889 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3890 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3891 return bounceAnim;
3892 }
3893
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003894 @Override
3895 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003896 boolean searchVisible = updateGlobalSearchIcon();
3897 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003898 if (mSearchDropTargetBar != null) {
3899 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3900 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003901 }
3902
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003903 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003904 * Add the icons for all apps.
3905 *
3906 * Implementation of the method from LauncherModel.Callbacks.
3907 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003908 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003909 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3910 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3911 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3912 getHotseat().addAllAppsFolder(mIconCache, apps,
3913 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3914 }
3915 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003916 }
Winson Chungc58497e2013-09-03 17:48:37 -07003917 } else {
3918 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3919 mAppsCustomizeContent != null) {
3920 mAppsCustomizeContent.setApps(apps);
3921 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003922 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923 }
3924
Bjorn Bringert85f418d2013-09-06 12:50:05 +01003925 @Override
3926 public boolean shouldShowApp(ResolveInfo app) {
3927 return true;
3928 }
3929
Joe Onorato9c1289c2009-08-17 11:03:03 -04003930 /**
3931 * A package was updated.
3932 *
3933 * Implementation of the method from LauncherModel.Callbacks.
3934 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003935 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003936 Runnable r = new Runnable() {
3937 public void run() {
3938 bindAppsUpdated(apps);
3939 }
3940 };
3941 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003942 return;
3943 }
3944
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003945 if (mWorkspace != null) {
3946 mWorkspace.updateShortcuts(apps);
3947 }
Winson Chungc58497e2013-09-03 17:48:37 -07003948
3949 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3950 mAppsCustomizeContent != null) {
3951 mAppsCustomizeContent.updateApps(apps);
3952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003953 }
3954
3955 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003956 * A package was uninstalled. We take both the super set of packageNames
3957 * in addition to specific applications to remove, the reason being that
3958 * this can be called when a package is updated as well. In that scenario,
3959 * we only remove specific components from the workspace, where as
3960 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961 *
3962 * Implementation of the method from LauncherModel.Callbacks.
3963 */
Winson Chung83892cc2013-05-01 16:53:33 -07003964 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003965 final ArrayList<AppInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003966 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003967 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003968 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003969 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003970 }
Winson Chungd64d1762013-08-20 14:37:16 -07003971 };
3972 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003973 return;
3974 }
3975
Winson Chung64359a52013-07-08 17:17:08 -07003976 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003977 mWorkspace.removeItemsByPackageName(packageNames);
3978 } else {
3979 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003980 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003981
Winson Chunga1820962011-10-03 16:31:06 -07003982 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003983 mDragController.onAppsRemoved(appInfos, this);
Winson Chungc58497e2013-09-03 17:48:37 -07003984
3985 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3986 mAppsCustomizeContent != null) {
3987 mAppsCustomizeContent.removeApps(appInfos);
3988 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 }
Joe Onoratobe386092009-11-17 17:32:16 -08003990
3991 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003992 * A number of packages were updated.
3993 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003994 private ArrayList<Object> mWidgetsAndShortcuts;
3995 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003996 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003997 bindPackagesUpdated(mWidgetsAndShortcuts);
3998 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003999 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004000 };
4001
4002 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4003 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4004 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004005 return;
4006 }
4007
Winson Chung64359a52013-07-08 17:17:08 -07004008 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004009 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4010 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004011 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004012 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004013 }
4014
Winson Chung400438b2011-01-16 17:53:48 -08004015 private int mapConfigurationOriActivityInfoOri(int configOri) {
4016 final Display d = getWindowManager().getDefaultDisplay();
4017 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4018 switch (d.getRotation()) {
4019 case Surface.ROTATION_0:
4020 case Surface.ROTATION_180:
4021 // We are currently in the same basic orientation as the natural orientation
4022 naturalOri = configOri;
4023 break;
4024 case Surface.ROTATION_90:
4025 case Surface.ROTATION_270:
4026 // We are currently in the other basic orientation to the natural orientation
4027 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4028 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4029 break;
4030 }
4031
4032 int[] oriMap = {
4033 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4034 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4035 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4036 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4037 };
4038 // Since the map starts at portrait, we need to offset if this device's natural orientation
4039 // is landscape.
4040 int indexOffset = 0;
4041 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4042 indexOffset = 1;
4043 }
4044 return oriMap[(d.getRotation() + indexOffset) % 4];
4045 }
Adam Cohen446e9402011-09-15 18:21:21 -07004046
Winson Chung4b919f82012-05-01 10:44:08 -07004047 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004048 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004049 getResources().getBoolean(R.bool.allow_rotation);
4050 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004051 }
Winson Chung4b919f82012-05-01 10:44:08 -07004052 public void lockScreenOrientation() {
4053 if (isRotationEnabled()) {
4054 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4055 .getConfiguration().orientation));
4056 }
4057 }
4058 public void unlockScreenOrientation(boolean immediate) {
4059 if (isRotationEnabled()) {
4060 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004061 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004062 } else {
4063 mHandler.postDelayed(new Runnable() {
4064 public void run() {
4065 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4066 }
4067 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004068 }
Winson Chung4b919f82012-05-01 10:44:08 -07004069 }
Winson Chung400438b2011-01-16 17:53:48 -08004070 }
4071
Winson Chung82f55532011-08-09 14:14:23 -07004072 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004073 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004074 if (DISABLE_CLINGS) {
4075 return false;
4076 }
4077
Brett Chabot2a9e2282011-08-23 15:03:13 -07004078 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004079 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004080
Michael Jurkae233a8b2013-03-19 13:49:20 +01004081 // Restricted secondary users (child mode) will potentially have very few apps
4082 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004083// boolean supportsLimitedUsers =
4084// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4085// Account[] accounts = AccountManager.get(this).getAccounts();
4086// if (supportsLimitedUsers && accounts.length == 0) {
4087// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4088// Bundle restrictions = um.getUserRestrictions();
4089// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4090// return false;
4091// }
4092// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004093 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004094 }
Michael Jurka22143132012-10-04 17:42:38 +02004095
Winson Chung7d7541e2011-09-16 20:14:36 -07004096 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004097 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004098 if (cling != null) {
4099 cling.init(this, positionData);
4100 cling.setVisibility(View.VISIBLE);
4101 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4102 if (animate) {
4103 cling.buildLayer();
4104 cling.setAlpha(0f);
4105 cling.animate()
4106 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004107 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004108 .setDuration(SHOW_CLING_DURATION)
4109 .setStartDelay(delay)
4110 .start();
4111 } else {
4112 cling.setAlpha(1f);
4113 }
Michael Jurka22143132012-10-04 17:42:38 +02004114 cling.setFocusableInTouchMode(true);
4115 cling.post(new Runnable() {
4116 public void run() {
4117 cling.setFocusable(true);
4118 cling.requestFocus();
4119 }
4120 });
4121 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4122 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004123 }
4124 return cling;
4125 }
Michael Jurka22143132012-10-04 17:42:38 +02004126
Winson Chung7d7541e2011-09-16 20:14:36 -07004127 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004128 // To catch cases where siblings of top-level views are made invisible, just check whether
4129 // the cling is directly set to GONE before dismissing it.
4130 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004131 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004132 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004133 anim.addListener(new AnimatorListenerAdapter() {
4134 public void onAnimationEnd(Animator animation) {
4135 cling.setVisibility(View.GONE);
4136 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004137 // We should update the shared preferences on a background thread
4138 new Thread("dismissClingThread") {
4139 public void run() {
4140 SharedPreferences.Editor editor = mSharedPrefs.edit();
4141 editor.putBoolean(flag, true);
4142 editor.commit();
4143 }
4144 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004145 };
4146 });
4147 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004148 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004149 }
4150 }
Michael Jurka22143132012-10-04 17:42:38 +02004151
Winson Chung9d9d74f2011-09-19 11:49:12 -07004152 private void removeCling(int id) {
4153 final View cling = findViewById(id);
4154 if (cling != null) {
4155 final ViewGroup parent = (ViewGroup) cling.getParent();
4156 parent.post(new Runnable() {
4157 @Override
4158 public void run() {
4159 parent.removeView(cling);
4160 }
4161 });
Michael Jurka22143132012-10-04 17:42:38 +02004162 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004163 }
4164 }
Michael Jurka974c3862012-05-22 22:00:31 -07004165
4166 private boolean skipCustomClingIfNoAccounts() {
4167 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4168 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4169 if (customCling) {
4170 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004171 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004172 Account[] accounts = am.getAccountsByType("com.google");
4173 return accounts.length == 0;
4174 }
4175 return false;
4176 }
4177
Winson Chung7d7541e2011-09-16 20:14:36 -07004178 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004179 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004180 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004181 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4182 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004183 // If we're not using the default workspace layout, replace workspace cling
4184 // with a custom workspace cling (usually specified in an overlay)
4185 // For now, only do this on tablets
4186 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004187 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004188 // Use a custom cling
4189 View cling = findViewById(R.id.workspace_cling);
4190 ViewGroup clingParent = (ViewGroup) cling.getParent();
4191 int clingIndex = clingParent.indexOfChild(cling);
4192 clingParent.removeViewAt(clingIndex);
4193 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4194 clingParent.addView(customCling, clingIndex);
4195 customCling.setId(R.id.workspace_cling);
4196 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004197 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004198 } else {
4199 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004200 }
4201 }
4202 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004203 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004204 if (isClingsEnabled() &&
4205 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004206 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004207 } else {
4208 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004209 }
4210 }
4211 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004212 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004213 if (isClingsEnabled() &&
4214 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4215 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004216 } else {
4217 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004218 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004219 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004220 }
Michael Jurka104c4562013-07-08 18:03:46 -07004221 protected SharedPreferences getSharedPrefs() {
4222 return mSharedPrefs;
4223 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004224 public boolean isFolderClingVisible() {
4225 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004226 if (cling != null) {
4227 return cling.getVisibility() == View.VISIBLE;
4228 }
4229 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004230 }
Winson Chung82f55532011-08-09 14:14:23 -07004231 public void dismissWorkspaceCling(View v) {
4232 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004233 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004234 }
4235 public void dismissAllAppsCling(View v) {
4236 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004237 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4238 }
4239 public void dismissFolderCling(View v) {
4240 Cling cling = (Cling) findViewById(R.id.folder_cling);
4241 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004242 }
4243
Winson Chung80baf5a2010-08-09 16:03:15 -07004244 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004245 * Prints out out state for debugging.
4246 */
4247 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004248 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004249 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004250 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4251 Log.d(TAG, "mRestoring=" + mRestoring);
4252 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4253 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004254 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004255 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004256
Winson Chung785d2eb2011-04-14 16:08:02 -07004257 if (mAppsCustomizeContent != null) {
4258 mAppsCustomizeContent.dumpState();
4259 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004260 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004261 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004262
4263 @Override
4264 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4265 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004266 synchronized (sDumpLogs) {
4267 writer.println(" ");
4268 writer.println("Debug logs: ");
4269 for (int i = 0; i < sDumpLogs.size(); i++) {
4270 writer.println(" " + sDumpLogs.get(i));
4271 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004272 }
4273 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004274
4275 public static void dumpDebugLogsToConsole() {
Adam Cohen4caf2982013-08-20 18:54:31 -07004276 synchronized (sDumpLogs) {
4277 Log.d(TAG, "");
4278 Log.d(TAG, "*********************");
4279 Log.d(TAG, "Launcher debug logs: ");
4280 for (int i = 0; i < sDumpLogs.size(); i++) {
4281 Log.d(TAG, " " + sDumpLogs.get(i));
4282 }
4283 Log.d(TAG, "*********************");
4284 Log.d(TAG, "");
Adam Cohen487f7dd2012-06-28 18:12:10 -07004285 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004286 }
4287
4288 public static void addDumpLog(String tag, String log, boolean debugLog) {
4289 if (debugLog) {
4290 Log.d(tag, log);
4291 }
4292 sDateStamp.setTime(System.currentTimeMillis());
4293 synchronized (sDumpLogs) {
4294 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
4295 }
4296 }
4297
4298 public void dumpLogsToLocalData(final boolean email) {
4299 new Thread("DumpLogsToLocalData") {
4300 @Override
4301 public void run() {
4302 boolean success = false;
4303 sDateStamp.setTime(sRunStart);
4304 String FILENAME = sDateStamp.getMonth() + "-"
4305 + sDateStamp.getDay() + "_"
4306 + sDateStamp.getHours() + "-"
4307 + sDateStamp.getMinutes() + "_"
4308 + sDateStamp.getSeconds() + ".txt";
4309
4310 FileOutputStream fos = null;
4311 File outFile = null;
4312 try {
4313 outFile = new File(getFilesDir(), FILENAME);
4314 outFile.createNewFile();
4315 fos = new FileOutputStream(outFile);
4316 } catch (Exception e) {
4317 e.printStackTrace();
4318 }
4319 if (fos != null) {
4320 PrintWriter writer = new PrintWriter(fos);
4321
4322 writer.println(" ");
4323 writer.println("Debug logs: ");
4324 synchronized (sDumpLogs) {
4325 for (int i = 0; i < sDumpLogs.size(); i++) {
4326 writer.println(" " + sDumpLogs.get(i));
4327 }
4328 }
4329 writer.close();
4330 }
4331 try {
4332 if (fos != null) {
4333 fos.close();
4334 success = true;
4335 }
4336 } catch (IOException e) {
4337 e.printStackTrace();
4338 }
4339
4340 if (success && email) {
4341 if (!emailSent()) {
4342 emailFile(outFile);
4343 }
4344 }
4345 }
4346 }.start();
4347 }
4348
4349 private void emailFile(File file) {
4350 File publicCopy = new File(Environment.getExternalStorageDirectory(), file.getName());
4351 try {
4352 copyFile(file, publicCopy);
4353 } catch (IOException e) {
4354 e.printStackTrace();
4355 return;
4356 }
4357
4358 Intent intent = new Intent(Intent.ACTION_SEND);
4359 intent.setType("text/plain");
4360 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"adamcohen@google.com, winsonc@google.com," +
4361 "mikejurka@google"});
4362 intent.putExtra(Intent.EXTRA_SUBJECT, "Data corruption " + file.getName());
4363 intent.putExtra(Intent.EXTRA_TEXT, "Data corruption has occurred, logs attached");
4364
4365 if (!file.exists() || !file.canRead()) {
4366 Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
4367 finish();
4368 return;
4369 }
4370
4371 Toast.makeText(this, "Data corruption has occurred, please send e-mail", Toast.LENGTH_LONG);
4372 Uri uri = Uri.fromFile(publicCopy);
4373 intent.putExtra(Intent.EXTRA_STREAM, uri);
4374 startActivity(Intent.createChooser(intent, "Please send logs, consider clearing data"));
4375
4376 setEmailSent(true);
4377 }
4378
4379 public void copyFile(File src, File dst) throws IOException {
4380 InputStream in = new FileInputStream(src);
4381 OutputStream out = new FileOutputStream(dst);
4382
4383 // Transfer bytes from in to out
4384 byte[] buf = new byte[1024];
4385 int len;
4386 while ((len = in.read(buf)) > 0) {
4387 out.write(buf, 0, len);
4388 }
4389 in.close();
4390 out.close();
Adam Cohen487f7dd2012-06-28 18:12:10 -07004391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004392}
Michael Jurka2763be32011-02-24 11:19:57 -08004393
Michael Jurkaabded662011-03-04 12:06:57 -08004394interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004395 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004396 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004397 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004398 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004399 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004400}