blob: 408f61a473de19b9a5042cf3c301af2e845c3ed8 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
Bjorn Bringert85f418d2013-09-06 12:50:05 +010048import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070054import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070072import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
80import android.view.MenuItem;
81import android.view.MotionEvent;
82import android.view.Surface;
83import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070084import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080085import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewGroup;
87import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080088import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070091import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070092import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Advanceable;
96import android.widget.FrameLayout;
97import android.widget.ImageView;
98import android.widget.TextView;
99import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700100
Daniel Sandler325dc232013-06-05 22:57:57 -0400101import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800102
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.DataInputStream;
104import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700105import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.IOException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700111import java.io.InputStream;
112import java.io.OutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700116import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700119import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121/**
122 * Default launcher application.
123 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100124public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700125 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700126 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800127 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700128 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Daniel Sandlerf061f822013-06-27 13:59:36 -0400130 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400131 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700132 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400133 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700134
Winson Chung70d72102011-08-12 11:24:35 -0700135 private static final int MENU_GROUP_WALLPAPER = 1;
136 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
137 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700138 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
139 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
141 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700142 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final int REQUEST_PICK_APPLICATION = 6;
144 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700146 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Michael Jurka8b805b12012-04-18 14:23:14 -0700148 private static final int REQUEST_BIND_APPWIDGET = 11;
149
Mathew Inwood876a8462013-06-14 14:12:41 +0100150 /**
151 * IntentStarter uses request codes starting with this. This must be greater than all activity
152 * request codes used internally.
153 */
154 protected static final int REQUEST_LAST = 100;
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
157
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800158 static final int SCREEN_COUNT = 5;
159 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Romain Guy98d01652009-06-30 16:21:04 -0700161 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800162 // To turn on these properties, type
163 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
164 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
165 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Winson Chung2672ff92012-05-04 16:22:30 -0700167 // The Intent extra that defines whether to ignore the launch animation
168 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400169 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: int
172 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700173 // Type: int
174 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
177 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
179 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700182 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: boolean
184 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
185 // Type: long
186 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700187 // Type: int
188 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
189 // Type: int
190 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
191 // Type: parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100194 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700195 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100196 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700197 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100198 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700199
Adam Cohen39a06042013-07-19 14:30:12 -0700200 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700201 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700202
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700204 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700205 private State mState = State.WORKSPACE;
206 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700209 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
210 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700211 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700212 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800215 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
218 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700219 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800221 private final BroadcastReceiver mCloseSystemDialogsReceiver
222 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800223 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 private LayoutInflater mInflater;
226
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800229 private DragLayer mDragLayer;
230 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700231 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700233 private AppWidgetManager mAppWidgetManager;
234 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Michael Jurkac9d95c52011-08-29 14:03:34 -0700236 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700237 private AppWidgetProviderInfo mPendingAddWidgetInfo;
238
Michael Jurka0280c3b2010-09-17 15:00:07 -0700239 private int[] mTmpAddItemCellCoordinates = new int[2];
240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 private FolderInfo mFolderInfo;
242
Winson Chung3d503fb2011-07-13 17:25:49 -0700243 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700244 private View mOverviewPanel;
245
Michael Jurka838a4ca2011-02-07 13:33:06 -0800246 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247
Winson Chungc51db6a2011-10-05 11:44:49 -0700248 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700249 private AppsCustomizeTabHost mAppsCustomizeTabHost;
250 private AppsCustomizePagedView mAppsCustomizeContent;
251 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100252 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700255 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
256 // scroll issues (because the workspace may not have been measured yet) and extra work.
257 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
258 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259
260 private SpannableStringBuilder mDefaultKeySsb = null;
261
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262 private boolean mWorkspaceLoading = true;
263
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800264 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 private boolean mRestoring;
266 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700267 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
Michael Jurka1e2f4652013-07-08 18:03:46 -0700269 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700270 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
271
Winson Chung4a2afa32012-07-19 14:53:05 -0700272 // Keep track of whether the user has left launcher
273 private static boolean sPausedFromUserAction = false;
274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private Bundle mSavedInstanceState;
276
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800278 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800279 private boolean mUserPresent = true;
280 private boolean mVisible = false;
281 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700282 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700284 private static LocaleConfiguration sLocaleConfiguration = null;
285
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700286 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700287
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700288 private Intent mAppMarketIntent = null;
289
Adam Cohended9f8d2010-11-03 13:25:16 -0700290 // Related to the auto-advancing of widgets
291 private final int ADVANCE_MSG = 1;
292 private final int mAdvanceInterval = 20000;
293 private final int mAdvanceStagger = 250;
294 private long mAutoAdvanceSentTime;
295 private long mAutoAdvanceTimeLeft = -1;
296 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
297 new HashMap<View, AppWidgetProviderInfo>();
298
Winson Chung400438b2011-01-16 17:53:48 -0800299 // Determines how long to wait after a rotation before restoring the screen orientation to
300 // match the sensor state.
301 private final int mRestoreScreenOrientationDelay = 500;
302
Michael Jurka4ef207b2010-11-29 17:05:45 -0800303 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700304 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
305 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
306 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800307
Craig Mautner360310b2012-10-26 15:13:08 -0700308 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700309
Adam Cohen1462de32012-07-24 22:34:36 -0700310 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
311
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700312 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700313 static Date sDateStamp = new Date();
314 static DateFormat sDateFormat =
315 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
316 static long sRunStart = System.currentTimeMillis();
317 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Adam Cohene25af792013-06-06 23:08:25 -0700323 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
324
Winson Chungf0c6ae02012-03-21 16:10:31 -0700325 // Holds the page that we need to animate to, and the icon views that we need to animate up
326 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700327 private ImageView mFolderIconImageView;
328 private Bitmap mFolderIconBitmap;
329 private Canvas mFolderIconCanvas;
330 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700331
Michael Jurkaddd62e92011-02-16 17:49:14 -0800332 private BubbleTextView mWaitingForResume;
333
Michael Jurka22143132012-10-04 17:42:38 +0200334 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
335 = new HideFromAccessibilityHelper();
336
Michael Jurkac1f5d262011-09-30 19:32:27 -0700337 private Runnable mBuildLayersRunnable = new Runnable() {
338 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700339 if (mWorkspace != null) {
340 mWorkspace.buildPageHardwareLayers();
341 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700342 }
343 };
344
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800345 private static ArrayList<PendingAddArguments> sPendingAddList
346 = new ArrayList<PendingAddArguments>();
347
Michael Jurka0a457bf2012-11-19 14:05:05 -0800348 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
349
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800350 private static class PendingAddArguments {
351 int requestCode;
352 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700353 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700354 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355 int cellX;
356 int cellY;
357 }
358
Daniel Sandlerff02d492013-08-05 02:12:05 -0400359 private Stats mStats;
360
Michael Jurka0a457bf2012-11-19 14:05:05 -0800361 private static boolean isPropertyEnabled(String propertyName) {
362 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700363 }
364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 @Override
366 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700367 if (DEBUG_STRICT_MODE) {
368 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
369 .detectDiskReads()
370 .detectDiskWrites()
371 .detectNetwork() // or .detectAll() for all detectable problems
372 .penaltyLog()
373 .build());
374 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
375 .detectLeakedSqlLiteObjects()
376 .detectLeakedClosableObjects()
377 .penaltyLog()
378 .penaltyDeath()
379 .build());
380 }
381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400383
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400384 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400385 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700386
387 // Determine the dynamic grid properties
388 Point smallestSize = new Point();
389 Point largestSize = new Point();
390 Point realSize = new Point();
391 Display display = getWindowManager().getDefaultDisplay();
392 display.getCurrentSizeRange(smallestSize, largestSize);
393 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700394 DisplayMetrics dm = new DisplayMetrics();
395 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700396 // Lazy-initialize the dynamic grid
397 DeviceProfile grid = app.initDynamicGrid(this,
398 Math.min(smallestSize.x, smallestSize.y),
399 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700400 realSize.x, realSize.y,
401 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700402
403 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400404 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700405 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800406 mModel = app.setLauncher(this);
407 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400408 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700410
Daniel Sandlerff02d492013-08-05 02:12:05 -0400411 mStats = new Stats(this);
412
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700413 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700414 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
415 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700416
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700417 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
418 // this also ensures that any synchronous binding below doesn't re-trigger another
419 // LauncherModel load.
420 mPaused = false;
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200423 android.os.Debug.startMethodTracing(
424 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 }
426
427 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 setContentView(R.layout.launcher);
429 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100430 grid.layout(this);
Winson Chung7d7541e2011-09-16 20:14:36 -0700431 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800433 registerContentObservers();
434
Joe Onorato7c312c12009-08-13 21:36:53 -0700435 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 mSavedState = savedInstanceState;
438 restoreState(mSavedState);
439
Winson Chunga12a2502010-12-20 14:41:35 -0800440 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700441 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200442 mAppsCustomizeContent.onPackagesUpdated(
443 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700444 }
Winson Chunga12a2502010-12-20 14:41:35 -0800445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 if (PROFILE_STARTUP) {
447 android.os.Debug.stopMethodTracing();
448 }
449
450 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700451 if (sPausedFromUserAction) {
452 // If the user leaves launcher, then we should just load items asynchronously when
453 // they return.
454 mModel.startLoader(true, -1);
455 } else {
456 // We only load the page synchronously if the user rotates (or triggers a
457 // configuration change) while launcher is in the foreground
458 mModel.startLoader(true, mWorkspace.getCurrentPage());
459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
462 // For handling default keys
463 mDefaultKeySsb = new SpannableStringBuilder();
464 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800465
466 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
467 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800468
Adam Cohenf9426d52012-06-04 17:26:21 -0700469 updateGlobalIcons();
470
471 // On large interfaces, we want the screen to auto-rotate based on the current orientation
472 unlockScreenOrientation(true);
473 }
474
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 protected void onUserLeaveHint() {
476 super.onUserLeaveHint();
477 sPausedFromUserAction = true;
478 }
479
Winson Chung98ca0f72013-07-29 12:58:51 -0700480 /** To be overriden by subclasses to hint to Launcher that we have custom content */
481 protected boolean hasCustomContentToLeft() {
482 return false;
483 }
484
Adam Cohenf9426d52012-06-04 17:26:21 -0700485 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700486 boolean searchVisible = false;
487 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800488 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700489 int coi = getCurrentOrientationIndexForGlobalIcons();
490 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
491 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700492 updateAppMarketIcon();
493 searchVisible = updateGlobalSearchIcon();
494 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700495 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700496 if (sGlobalSearchIcon[coi] != null) {
497 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700498 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700499 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700500 if (sVoiceSearchIcon[coi] != null) {
501 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700502 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700503 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700504 if (sAppMarketIcon[coi] != null) {
505 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800506 }
Winson Chungadf0c182012-08-23 14:59:07 -0700507 if (mSearchDropTargetBar != null) {
508 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
509 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 }
Romain Guycbb89e42009-06-08 15:52:54 -0700511
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700513 if (sLocaleConfiguration == null) {
514 new AsyncTask<Void, Void, LocaleConfiguration>() {
515 @Override
516 protected LocaleConfiguration doInBackground(Void... unused) {
517 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
518 readConfiguration(Launcher.this, localeConfiguration);
519 return localeConfiguration;
520 }
521
522 @Override
523 protected void onPostExecute(LocaleConfiguration result) {
524 sLocaleConfiguration = result;
525 checkForLocaleChange(); // recursive, but now with a locale configuration
526 }
527 }.execute();
528 return;
529 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700530
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 final Configuration configuration = getResources().getConfiguration();
532
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700533 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 final String locale = configuration.locale.toString();
535
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700536 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 final int mcc = configuration.mcc;
538
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700539 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 final int mnc = configuration.mnc;
541
Romain Guy84f296c2009-11-04 15:00:44 -0800542 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
Romain Guy84f296c2009-11-04 15:00:44 -0800544 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700545 sLocaleConfiguration.locale = locale;
546 sLocaleConfiguration.mcc = mcc;
547 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700548
Joe Onorato0589f0f2010-02-08 13:44:00 -0800549 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700550
551 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
552 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700553 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700554 public void run() {
555 writeConfiguration(Launcher.this, localeConfiguration);
556 }
557 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700558 }
559 }
560
561 private static class LocaleConfiguration {
562 public String locale;
563 public int mcc = -1;
564 public int mnc = -1;
565 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700566
Romain Guy98d01652009-06-30 16:21:04 -0700567 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
568 DataInputStream in = null;
569 try {
570 in = new DataInputStream(context.openFileInput(PREFERENCES));
571 configuration.locale = in.readUTF();
572 configuration.mcc = in.readInt();
573 configuration.mnc = in.readInt();
574 } catch (FileNotFoundException e) {
575 // Ignore
576 } catch (IOException e) {
577 // Ignore
578 } finally {
579 if (in != null) {
580 try {
581 in.close();
582 } catch (IOException e) {
583 // Ignore
584 }
585 }
586 }
587 }
588
589 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
590 DataOutputStream out = null;
591 try {
592 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
593 out.writeUTF(configuration.locale);
594 out.writeInt(configuration.mcc);
595 out.writeInt(configuration.mnc);
596 out.flush();
597 } catch (FileNotFoundException e) {
598 // Ignore
599 } catch (IOException e) {
600 //noinspection ResultOfMethodCallIgnored
601 context.getFileStreamPath(PREFERENCES).delete();
602 } finally {
603 if (out != null) {
604 try {
605 out.close();
606 } catch (IOException e) {
607 // Ignore
608 }
609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 }
611 }
612
Bjorn Bringertc459e522013-06-07 19:36:01 +0100613 public LayoutInflater getInflater() {
614 return mInflater;
615 }
616
Adam Cohen716b51e2011-06-30 12:09:54 -0700617 public DragLayer getDragLayer() {
618 return mDragLayer;
619 }
620
Winson Chung36a62fe2012-05-06 18:04:42 -0700621 boolean isDraggingEnabled() {
622 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
623 // that is subsequently removed from the workspace in startBinding().
624 return !mModel.isLoadingWorkspace();
625 }
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 static int getScreen() {
628 synchronized (sLock) {
629 return sScreen;
630 }
631 }
632
633 static void setScreen(int screen) {
634 synchronized (sLock) {
635 sScreen = screen;
636 }
637 }
638
Winson Chung557d6ed2011-07-08 15:34:52 -0700639 /**
640 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
641 * a configuration step, this allows the proper animations to run after other transitions.
642 */
643 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700644 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800645 switch (args.requestCode) {
646 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800649 break;
650 case REQUEST_PICK_SHORTCUT:
651 processShortcut(args.intent);
652 break;
653 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700654 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700655 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700656 result = true;
657 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 case REQUEST_CREATE_APPWIDGET:
659 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700660 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700661 result = true;
662 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 case REQUEST_PICK_WALLPAPER:
664 // We just wanted the activity result here so we can clear mWaitingForResult
665 break;
666 }
Michael Jurka27614d22012-04-02 06:40:22 -0700667 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
668 // if you turned the screen off and then back while in All Apps, Launcher would not
669 // return to the workspace. Clearing mAddInfo.container here fixes this issue
670 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700671 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800672 }
673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700675 protected void onActivityResult(
676 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700677 // Reset the startActivity waiting flag
678 mWaitingForResult = false;
679
Michael Jurka8b805b12012-04-18 14:23:14 -0700680 if (requestCode == REQUEST_BIND_APPWIDGET) {
681 int appWidgetId = data != null ?
682 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
683 if (resultCode == RESULT_CANCELED) {
684 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
685 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700686 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700687 }
688 return;
689 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700690 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800691 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
692 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700693
Adam Cohened66b2b2012-01-23 17:28:51 -0800694 // We have special handling for widgets
695 if (isWidgetDrop) {
696 int appWidgetId = data != null ?
697 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700698 if (appWidgetId < 0) {
699 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
700 "widget configuration activity.");
701 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
702 } else {
703 completeTwoStageWidgetDrop(resultCode, appWidgetId);
704 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800705 return;
706 }
707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 // The pattern used here is that a user PICKs a specific application,
709 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
712 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700713 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 final PendingAddArguments args = new PendingAddArguments();
715 args.requestCode = requestCode;
716 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700717 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700719 args.cellX = mPendingAddInfo.cellX;
720 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800721 if (isWorkspaceLocked()) {
722 sPendingAddList.add(args);
723 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700724 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800727 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700728 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800729 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
730 null);
731 }
732
733 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700735 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 Runnable onCompleteRunnable = null;
737 int animationType = 0;
738
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700739 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 if (resultCode == RESULT_OK) {
741 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
742 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700743 mPendingAddWidgetInfo);
744 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 onCompleteRunnable = new Runnable() {
746 @Override
747 public void run() {
748 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700749 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
751 null);
752 }
753 };
754 } else if (resultCode == RESULT_CANCELED) {
755 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
756 onCompleteRunnable = new Runnable() {
757 @Override
758 public void run() {
759 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
760 null);
761 }
762 };
763 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700764 if (mDragLayer.getAnimatedView() != null) {
765 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
766 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
767 animationType, boundWidget, true);
768 } else {
769 // The animated view may be null in the case of a rotation during widget configuration
770 onCompleteRunnable.run();
771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
773
774 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700775 protected void onStop() {
776 super.onStop();
777 FirstFrameAnimatorHelper.setIsVisible(false);
778 }
779
780 @Override
781 protected void onStart() {
782 super.onStart();
783 FirstFrameAnimatorHelper.setIsVisible(true);
784 }
785
786 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200788 long startTime = 0;
789 if (DEBUG_RESUME_TIME) {
790 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400791 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200792 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700794
Winson Chung4a2afa32012-07-19 14:53:05 -0700795 // Restore the previous launcher state
796 if (mOnResumeState == State.WORKSPACE) {
797 showWorkspace(false);
798 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chungc58497e2013-09-03 17:48:37 -0700799 showAllApps(false, AppsCustomizePagedView.ContentType.Applications);
Winson Chung4a2afa32012-07-19 14:53:05 -0700800 }
801 mOnResumeState = State.NONE;
802
Craig Mautner360310b2012-10-26 15:13:08 -0700803 // Background was set to gradient in onPause(), restore to black if in all apps.
804 setWorkspaceBackground(mState == State.WORKSPACE);
805
Winson Chungde0fb8f2012-05-08 14:37:08 -0700806 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700807 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700808
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800809 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700810 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700811 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700813 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700815 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700817 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200818 // We might have postponed some bind calls until onResume (see waitUntilResume) --
819 // execute them here
820 long startTimeCallbacks = 0;
821 if (DEBUG_RESUME_TIME) {
822 startTimeCallbacks = System.currentTimeMillis();
823 }
824
825 if (mAppsCustomizeContent != null) {
826 mAppsCustomizeContent.setBulkBind(true);
827 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700828 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
829 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200830 }
831 if (mAppsCustomizeContent != null) {
832 mAppsCustomizeContent.setBulkBind(false);
833 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700834 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200835 if (DEBUG_RESUME_TIME) {
836 Log.d(TAG, "Time spent processing callbacks in onResume: " +
837 (System.currentTimeMillis() - startTimeCallbacks));
838 }
Michael Jurka447bf842013-05-15 14:52:15 +0200839 }
Michael Jurka54554252013-08-01 12:52:23 +0200840 if (mOnResumeCallbacks.size() > 0) {
841 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
842 mOnResumeCallbacks.get(i).run();
843 }
844 mOnResumeCallbacks.clear();
845 }
Winson Chunge4e50662012-01-23 14:45:13 -0800846
847 // Reset the pressed state of icons that were locked in the press state while activities
848 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800849 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800850 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800851 mWaitingForResume.setStayPressed(false);
852 }
Winson Chunge4e50662012-01-23 14:45:13 -0800853 if (mAppsCustomizeContent != null) {
854 // Resets the previous all apps icon press state
855 mAppsCustomizeContent.resetDrawableState();
856 }
Adam Cohen06dff352012-06-01 17:17:08 -0700857 // It is possible that widgets can receive updates while launcher is not in the foreground.
858 // Consequently, the widgets will be inflated in the orientation of the foreground activity
859 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
860 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700861 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700862
863 // Again, as with the above scenario, it's possible that one or more of the global icons
864 // were updated in the wrong orientation.
865 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200866 if (DEBUG_RESUME_TIME) {
867 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
868 }
Adam Cohen4caf2982013-08-20 18:54:31 -0700869
870 // Write all the logs to disk
871 Launcher.addDumpLog(TAG, "10249126 - onResume() - dumping logs to disk", true);
872 dumpLogsToLocalData(false);
Adam Cohen06dff352012-06-01 17:17:08 -0700873 }
874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700876 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700877 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
878 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
879 // when Launcher resumes and we are still in AllApps.
880 updateWallpaperVisibility(true);
881
Winson Chung997a9232013-07-24 15:33:46 -0700882 // Ensure that items added to Launcher are queued until Launcher returns
883 InstallShortcutReceiver.enableInstallQueue();
884
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700885 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700886 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800887 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700888 mDragController.resetLastGestureUpTime();
Adam Cohen4caf2982013-08-20 18:54:31 -0700889
890 // Write all the logs to disk
891 Launcher.addDumpLog(TAG, "10249126 - onPause() - dumping logs to disk", true);
892 dumpLogsToLocalData(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700893 }
Romain Guycbb89e42009-06-08 15:52:54 -0700894
Adam Cohen66a01fd2013-06-11 12:48:00 -0700895 protected void onFinishBindingItems() {
896 }
897
Adam Cohenbffe7452013-07-22 18:21:45 -0700898 QSBScroller mQsbScroller = new QSBScroller() {
899 int scrollY = 0;
900
901 @Override
902 public void setScrollY(int scroll) {
903 scrollY = scroll;
904
905 if (mWorkspace.isOnOrMovingToCustomContent()) {
906 mSearchDropTargetBar.setTranslationY(- scrollY);
907 }
908 }
909 };
910
911 public void resetQSBScroll() {
912 mSearchDropTargetBar.animate().translationY(0).start();
913 }
914
915 public interface CustomContentCallbacks {
916 // Custom content is completely shown
917 public void onShow();
918
919 // Custom content is completely hidden
920 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700921
922 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
923 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700924 }
925
Adam Cohen30bacb22013-08-29 14:25:25 -0700926 protected void startSettings() {
927 }
928
Adam Cohenbffe7452013-07-22 18:21:45 -0700929 public interface QSBScroller {
930 public void setScrollY(int scrollY);
931 }
932
Adam Cohen66a01fd2013-06-11 12:48:00 -0700933 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700934 public QSBScroller addToCustomContentPage(View customContent) {
935 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700936 }
937
Winson Chung98ca0f72013-07-29 12:58:51 -0700938 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700939 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700940 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700941 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700942 }
943
Adam Cohenedb40762013-07-18 16:45:45 -0700944 // The custom content needs to offset its content to account for the QSB
945 public int getTopOffsetForCustomContent() {
946 return mWorkspace.getPaddingTop();
947 }
948
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700949 @Override
950 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 // Flag the loader to stop early before switching
952 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700953 if (mAppsCustomizeContent != null) {
954 mAppsCustomizeContent.surrender();
955 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800956 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700957 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700958
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800959 // We can't hide the IME if it was forced open. So don't bother
960 /*
961 @Override
962 public void onWindowFocusChanged(boolean hasFocus) {
963 super.onWindowFocusChanged(hasFocus);
964
965 if (hasFocus) {
966 final InputMethodManager inputManager = (InputMethodManager)
967 getSystemService(Context.INPUT_METHOD_SERVICE);
968 WindowManager.LayoutParams lp = getWindow().getAttributes();
969 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
970 android.os.Handler()) {
971 protected void onReceiveResult(int resultCode, Bundle resultData) {
972 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
973 }
974 });
975 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
976 }
977 }
978 */
979
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 private boolean acceptFilter() {
981 final InputMethodManager inputManager = (InputMethodManager)
982 getSystemService(Context.INPUT_METHOD_SERVICE);
983 return !inputManager.isFullscreenMode();
984 }
985
986 @Override
987 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700988 final int uniChar = event.getUnicodeChar();
989 final boolean handled = super.onKeyDown(keyCode, event);
990 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
991 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
993 keyCode, event);
994 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700995 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700996 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700997 // showSearchDialog()
998 // If there are multiple keystrokes before the search dialog takes focus,
999 // onSearchRequested() will be called for every keystroke,
1000 // but it is idempotent, so it's fine.
1001 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 }
1003 }
1004
Joe Onorato8a9625e2010-01-28 15:55:35 -08001005 // Eat the long press event so the keyboard doesn't come up.
1006 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1007 return true;
1008 }
1009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 return handled;
1011 }
1012
Karl Rosaen138a0412009-04-23 19:00:21 -07001013 private String getTypedText() {
1014 return mDefaultKeySsb.toString();
1015 }
1016
1017 private void clearTypedText() {
1018 mDefaultKeySsb.clear();
1019 mDefaultKeySsb.clearSpans();
1020 Selection.setSelection(mDefaultKeySsb, 0);
1021 }
1022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001024 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1025 * State
1026 */
1027 private static State intToState(int stateOrdinal) {
1028 State state = State.WORKSPACE;
1029 final State[] stateValues = State.values();
1030 for (int i = 0; i < stateValues.length; i++) {
1031 if (stateValues[i].ordinal() == stateOrdinal) {
1032 state = stateValues[i];
1033 break;
1034 }
1035 }
1036 return state;
1037 }
1038
1039 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 * Restores the previous state, if it exists.
1041 *
1042 * @param savedState The previous state.
1043 */
1044 private void restoreState(Bundle savedState) {
1045 if (savedState == null) {
1046 return;
1047 }
1048
Michael Jurka883f55b2010-10-21 15:47:14 -07001049 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001050 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001051 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001052 }
1053
Winson Chungf0c6ae02012-03-21 16:10:31 -07001054 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001056 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
1058
Winson Chung3d503fb2011-07-13 17:25:49 -07001059 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001060 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001061
Winson Chung3d503fb2011-07-13 17:25:49 -07001062 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1063 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001064 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001065 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1066 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001067 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1068 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1069 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001070 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 mRestoring = true;
1072 }
1073
1074 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1075 if (renameFolder) {
1076 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001077 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 mRestoring = true;
1079 }
Winson Chunga12a2502010-12-20 14:41:35 -08001080
Winson Chung785d2eb2011-04-14 16:08:02 -07001081 // Restore the AppsCustomize tab
1082 if (mAppsCustomizeTabHost != null) {
1083 String curTab = savedState.getString("apps_customize_currentTab");
1084 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001085 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001086 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001087 mAppsCustomizeContent.loadAssociatedPages(
1088 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001089 }
1090
Winson Chung5afbf7b2011-07-25 11:53:08 -07001091 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1092 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
1095
1096 /**
1097 * Finds all the views we need and configure them properly.
1098 */
1099 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001100 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001101
Craig Mautner360310b2012-10-26 15:13:08 -07001102 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001103 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1104 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001105
John Spurlock77e1f472013-09-11 10:09:51 -04001106 mLauncherView.setSystemUiVisibility(
1107 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001108 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109
Winson Chung4c98d922011-05-31 16:50:48 -07001110 // Setup the drag layer
1111 mDragLayer.setup(this, dragController);
1112
Winson Chung3d503fb2011-07-13 17:25:49 -07001113 // Setup the hotseat
1114 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1115 if (mHotseat != null) {
1116 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001117 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001118 }
1119
Adam Cohenf358a4b2013-07-23 16:47:31 -07001120 mOverviewPanel = findViewById(R.id.overview_panel);
1121 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1122 @Override
1123 public void onClick(View arg0) {
Winson Chungc58497e2013-09-03 17:48:37 -07001124 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001125 }
1126 });
1127 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1128 @Override
1129 public void onClick(View arg0) {
1130 startWallpaper();
1131 }
1132 });
Adam Cohen30bacb22013-08-29 14:25:25 -07001133 findViewById(R.id.settings_button).setOnClickListener(new OnClickListener() {
1134 @Override
1135 public void onClick(View arg0) {
1136 startSettings();
1137 }
1138 });
Adam Cohenf358a4b2013-07-23 16:47:31 -07001139
Winson Chung4c98d922011-05-31 16:50:48 -07001140 // Setup the workspace
1141 mWorkspace.setHapticFeedbackEnabled(false);
1142 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001143 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001144 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001145
Winson Chungf0ea4d32011-06-06 14:27:16 -07001146 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001147 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001148
Winson Chungf0ea4d32011-06-06 14:27:16 -07001149 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001150 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001151 mAppsCustomizeContent = (AppsCustomizePagedView)
1152 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1153 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001154
Winson Chung3d503fb2011-07-13 17:25:49 -07001155 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001156 dragController.setDragScoller(mWorkspace);
1157 dragController.setScrollView(mDragLayer);
1158 dragController.setMoveTarget(mWorkspace);
1159 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001160 if (mSearchDropTargetBar != null) {
1161 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001162 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001163
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001164 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001165 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001166 mWeightWatcher = new WeightWatcher(this);
1167 mWeightWatcher.setAlpha(0.5f);
1168 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001169 new FrameLayout.LayoutParams(
1170 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001171 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001172 Gravity.BOTTOM)
1173 );
Adam Cohen39a06042013-07-19 14:30:12 -07001174
1175 boolean show = shouldShowWeightWatcher();
1176 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 }
1179
1180 /**
1181 * Creates a view representing a shortcut.
1182 *
1183 * @param info The data structure describing the shortcut.
1184 *
1185 * @return A View inflated from R.layout.application.
1186 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001187 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001189 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 }
1191
1192 /**
1193 * Creates a view representing a shortcut inflated from the specified resource.
1194 *
1195 * @param layoutResId The id of the XML layout used to create the shortcut.
1196 * @param parent The group the shortcut belongs to.
1197 * @param info The data structure describing the shortcut.
1198 *
1199 * @return A View inflated from layoutResId.
1200 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001201 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001202 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1203 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 return favorite;
1206 }
1207
1208 /**
1209 * Add an application shortcut to the workspace.
1210 *
1211 * @param data The intent describing the application.
1212 * @param cellInfo The position on screen where to create the shortcut.
1213 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001214 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001215 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001216 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001217
Adam Cohenfbba09b2011-07-18 21:43:05 -07001218 // First we check if we already know the exact location where we want to add this item.
1219 if (cellX >= 0 && cellY >= 0) {
1220 cellXY[0] = cellX;
1221 cellXY[1] = cellY;
1222 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001223 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001224 return;
1225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001227 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001228
Romain Guy73b979d2009-06-09 12:57:21 -07001229 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001230 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001232 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001233 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001234 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001235 } else {
1236 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 }
1238 }
Romain Guycbb89e42009-06-08 15:52:54 -07001239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 /**
1241 * Add a shortcut to the workspace.
1242 *
1243 * @param data The intent describing the shortcut.
1244 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001246 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001247 int cellY) {
1248 int[] cellXY = mTmpAddItemCellCoordinates;
1249 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001250 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001251
Michael Jurkad3ef3062010-11-23 16:23:58 -08001252 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001253
Adam Cohen558baaf2011-08-15 15:22:57 -07001254 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001255 if (info == null) {
1256 return;
1257 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001258 final View view = createShortcut(info);
1259
Adam Cohenfbba09b2011-07-18 21:43:05 -07001260 // First we check if we already know the exact location where we want to add this item.
1261 if (cellX >= 0 && cellY >= 0) {
1262 cellXY[0] = cellX;
1263 cellXY[1] = cellY;
1264 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001265
1266 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001267 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001268 true, null,null)) {
1269 return;
1270 }
1271 DragObject dragObject = new DragObject();
1272 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001273 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1274 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001275 return;
1276 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001277 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001278 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001279 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001280 foundCellSpan = (result != null);
1281 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001282 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001283 }
1284
1285 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001286 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001287 return;
1288 }
1289
Adam Cohendcd297f2013-06-18 13:13:40 -07001290 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291
1292 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001293 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001294 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
1296 }
1297
Adam Cohen2f093b62012-04-30 18:59:53 -07001298 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1299 int minHeight) {
1300 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001301 // We want to account for the extra amount of padding that we are adding to the widget
1302 // to ensure that it gets the full amount of space that it has requested
1303 int requiredWidth = minWidth + padding.left + padding.right;
1304 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001305 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001306 }
1307
Adam Cohen2f093b62012-04-30 18:59:53 -07001308 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1309 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001310 }
1311
Adam Cohen2f093b62012-04-30 18:59:53 -07001312 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1313 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001314 }
1315
Adam Cohen2f093b62012-04-30 18:59:53 -07001316 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1317 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001318 }
1319
Adam Cohen2f093b62012-04-30 18:59:53 -07001320 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1321 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001322 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001323 }
1324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001326 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001328 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001329 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001331 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001332 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1333 if (appWidgetInfo == null) {
1334 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1335 }
Romain Guycbb89e42009-06-08 15:52:54 -07001336
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001337 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001338 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001339
Adam Cohen2f093b62012-04-30 18:59:53 -07001340 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1341 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001342
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001344 // We have saved the position to which the widget was dragged-- this really only matters
1345 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001346 int[] cellXY = mTmpAddItemCellCoordinates;
1347 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001348 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001349 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001350 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1351 cellXY[0] = mPendingAddInfo.cellX;
1352 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001353 spanXY[0] = mPendingAddInfo.spanX;
1354 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001355 foundCellSpan = true;
1356 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001357 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001358 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001359 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1360 spanXY[1], cellXY, finalSpan);
1361 spanXY[0] = finalSpan[0];
1362 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001363 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001364 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001365 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001366 }
1367
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001369 if (appWidgetId != -1) {
1370 // Deleting an app widget ID is a void call but writes to disk before returning
1371 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001372 new Thread("deleteAppWidgetId") {
1373 public void run() {
1374 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1375 }
1376 }.start();
1377 }
Winson Chung93eef082012-03-23 15:59:27 -07001378 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001379 return;
1380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001382 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001383 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1384 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001385 launcherInfo.spanX = spanXY[0];
1386 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001387 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1388 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001391 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392
1393 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001394 if (hostView == null) {
1395 // Perform actual inflation because we're live
1396 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1397 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1398 } else {
1399 // The AppWidgetHostView has already been inflated and instantiated
1400 launcherInfo.hostView = hostView;
1401 }
Romain Guycbb89e42009-06-08 15:52:54 -07001402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001404 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001405 launcherInfo.notifyWidgetSizeChanged(this);
1406
Adam Cohendcd297f2013-06-18 13:13:40 -07001407 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001408 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001409
1410 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001412 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
Romain Guycbb89e42009-06-08 15:52:54 -07001414
Adam Cohended9f8d2010-11-03 13:25:16 -07001415 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1416 @Override
1417 public void onReceive(Context context, Intent intent) {
1418 final String action = intent.getAction();
1419 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1420 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001421 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001422 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001423
Winson Chungc100e8e2011-08-09 16:02:43 -07001424 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001425 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001426 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1427 mAppsCustomizeTabHost.reset();
1428 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001429 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001430 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1431 mUserPresent = true;
1432 updateRunning();
1433 }
1434 }
1435 };
1436
1437 @Override
1438 public void onAttachedToWindow() {
1439 super.onAttachedToWindow();
1440
1441 // Listen for broadcasts related to user-presence
1442 final IntentFilter filter = new IntentFilter();
1443 filter.addAction(Intent.ACTION_SCREEN_OFF);
1444 filter.addAction(Intent.ACTION_USER_PRESENT);
1445 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001446 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001447 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001448 mVisible = true;
1449 }
1450
1451 @Override
1452 public void onDetachedFromWindow() {
1453 super.onDetachedFromWindow();
1454 mVisible = false;
1455
Adam Cohend113e0c2010-11-11 10:48:05 -08001456 if (mAttached) {
1457 unregisterReceiver(mReceiver);
1458 mAttached = false;
1459 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001460 updateRunning();
1461 }
1462
1463 public void onWindowVisibilityChanged(int visibility) {
1464 mVisible = visibility == View.VISIBLE;
1465 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001466 // The following code used to be in onResume, but it turns out onResume is called when
1467 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1468 // is a more appropriate event to handle
1469 if (mVisible) {
1470 mAppsCustomizeTabHost.onWindowVisible();
1471 if (!mWorkspaceLoading) {
1472 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001473 // We want to let Launcher draw itself at least once before we force it to build
1474 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001475 // apps is nice and speedy.
1476 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001477 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001478 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001479 if (mStarted) return;
1480 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001481 // We delay the layer building a bit in order to give
1482 // other message processing a time to run. In particular
1483 // this avoids a delay in hiding the IME if it was
1484 // currently shown, because doing that may involve
1485 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001486 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001487 final ViewTreeObserver.OnDrawListener listener = this;
1488 mWorkspace.post(new Runnable() {
1489 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001490 if (mWorkspace != null &&
1491 mWorkspace.getViewTreeObserver() != null) {
1492 mWorkspace.getViewTreeObserver().
1493 removeOnDrawListener(listener);
1494 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001495 }
1496 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001497 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001498 }
1499 });
1500 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001501 // When Launcher comes back to foreground, a different Activity might be responsible for
1502 // the app market intent, so refresh the icon
1503 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001504 clearTypedText();
1505 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001506 }
1507
1508 private void sendAdvanceMessage(long delay) {
1509 mHandler.removeMessages(ADVANCE_MSG);
1510 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1511 mHandler.sendMessageDelayed(msg, delay);
1512 mAutoAdvanceSentTime = System.currentTimeMillis();
1513 }
1514
1515 private void updateRunning() {
1516 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1517 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1518 mAutoAdvanceRunning = autoAdvanceRunning;
1519 if (autoAdvanceRunning) {
1520 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1521 sendAdvanceMessage(delay);
1522 } else {
1523 if (!mWidgetsToAdvance.isEmpty()) {
1524 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1525 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1526 }
1527 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001528 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001529 }
1530 }
1531 }
1532
1533 private final Handler mHandler = new Handler() {
1534 @Override
1535 public void handleMessage(Message msg) {
1536 if (msg.what == ADVANCE_MSG) {
1537 int i = 0;
1538 for (View key: mWidgetsToAdvance.keySet()) {
1539 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1540 final int delay = mAdvanceStagger * i;
1541 if (v instanceof Advanceable) {
1542 postDelayed(new Runnable() {
1543 public void run() {
1544 ((Advanceable) v).advance();
1545 }
1546 }, delay);
1547 }
1548 i++;
1549 }
1550 sendAdvanceMessage(mAdvanceInterval);
1551 }
1552 }
1553 };
1554
1555 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001556 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001557 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1558 if (v instanceof Advanceable) {
1559 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001560 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001561 updateRunning();
1562 }
1563 }
1564
1565 void removeWidgetToAutoAdvance(View hostView) {
1566 if (mWidgetsToAdvance.containsKey(hostView)) {
1567 mWidgetsToAdvance.remove(hostView);
1568 updateRunning();
1569 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001570 }
1571
Joe Onorato9c1289c2009-08-17 11:03:03 -04001572 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001573 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001574 launcherInfo.hostView = null;
1575 }
1576
Winson Chung93eef082012-03-23 15:59:27 -07001577 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1578 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1579 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001580 }
1581
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001582 public LauncherAppWidgetHost getAppWidgetHost() {
1583 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
Romain Guycbb89e42009-06-08 15:52:54 -07001585
Winson Chunga9abd0e2010-10-27 17:18:37 -07001586 public LauncherModel getModel() {
1587 return mModel;
1588 }
1589
Bjorn Bringertc459e522013-06-07 19:36:01 +01001590 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001591 getWindow().closeAllPanels();
1592
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001593 // Whatever we were doing is hereby canceled.
1594 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001595 }
1596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 @Override
1598 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001599 long startTime = 0;
1600 if (DEBUG_RESUME_TIME) {
1601 startTime = System.currentTimeMillis();
1602 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 super.onNewIntent(intent);
1604
1605 // Close the menu
1606 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001607 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001608 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001609
Jamie Genniscb222e82012-10-23 15:44:26 -07001610 final boolean alreadyOnHome =
1611 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001612 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001613
Jamie Genniscb222e82012-10-23 15:44:26 -07001614 Runnable processIntent = new Runnable() {
1615 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001616 if (mWorkspace == null) {
1617 // Can be cases where mWorkspace is null, this prevents a NPE
1618 return;
1619 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001620 Folder openFolder = mWorkspace.getOpenFolder();
1621 // In all these cases, only animate if we're already on home
1622 mWorkspace.exitWidgetResizeMode();
1623 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1624 openFolder == null) {
1625 mWorkspace.moveToDefaultScreen(true);
1626 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001627
Jamie Genniscb222e82012-10-23 15:44:26 -07001628 closeFolder();
1629 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001630
Jamie Genniscb222e82012-10-23 15:44:26 -07001631 // If we are already on home, then just animate back to the workspace,
1632 // otherwise, just wait until onResume to set the state back to Workspace
1633 if (alreadyOnHome) {
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001634 showWorkspace();
Jamie Genniscb222e82012-10-23 15:44:26 -07001635 } else {
1636 mOnResumeState = State.WORKSPACE;
1637 }
1638
1639 final View v = getWindow().peekDecorView();
1640 if (v != null && v.getWindowToken() != null) {
1641 InputMethodManager imm = (InputMethodManager)getSystemService(
1642 INPUT_METHOD_SERVICE);
1643 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1644 }
1645
1646 // Reset AllApps to its initial state
1647 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1648 mAppsCustomizeTabHost.reset();
1649 }
1650 }
1651 };
1652
1653 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1654 // Delay processing of the intent to allow the status bar animation to finish
1655 // first in order to avoid janky animations.
1656 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001657 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001658 // Process the intent immediately.
1659 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001660 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 }
Michael Jurka447bf842013-05-15 14:52:15 +02001663 if (DEBUG_RESUME_TIME) {
1664 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 }
1667
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001668 protected void showWorkspace() {
1669 showWorkspace(true);
1670 if (mWorkspace.isInOverviewMode()) {
1671 mWorkspace.exitOverviewMode();
1672 }
1673 }
1674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001676 public void onRestoreInstanceState(Bundle state) {
1677 super.onRestoreInstanceState(state);
1678 for (int page: mSynchronouslyBoundPages) {
1679 mWorkspace.restoreInstanceStateForChild(page);
1680 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 }
1682
1683 @Override
1684 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001685 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001686 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687
Michael Jurka883f55b2010-10-21 15:47:14 -07001688 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001689 // We close any open folder since it will not be re-opened, and we need to make sure
1690 // this state is reflected.
1691 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692
Adam Cohendcd297f2013-06-18 13:13:40 -07001693 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001694 mWaitingForResult) {
1695 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001696 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001697 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1698 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001699 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1700 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1701 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
1703
1704 if (mFolderInfo != null && mWaitingForResult) {
1705 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1706 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1707 }
Michael Jurka946ad472010-07-09 18:05:18 -07001708
Winson Chung785d2eb2011-04-14 16:08:02 -07001709 // Save the current AppsCustomize tab
1710 if (mAppsCustomizeTabHost != null) {
1711 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1712 if (currentTabTag != null) {
1713 outState.putString("apps_customize_currentTab", currentTabTag);
1714 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001715 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1716 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 }
1719
1720 @Override
1721 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001723
Winson Chunge7a03942011-08-05 15:05:12 -07001724 // Remove all pending runnables
1725 mHandler.removeMessages(ADVANCE_MSG);
1726 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001727 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001728
Winson Chungcd2b0142011-06-08 16:02:26 -07001729 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001730 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001731 mModel.stopLoader();
1732 app.setLauncher(null);
1733
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001735 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001737 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001739 mAppWidgetHost = null;
1740
1741 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742
1743 TextKeyListener.getInstance().release();
1744
Winson Chung81b52252012-08-27 15:34:29 -07001745 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1746 // to prevent leaking Launcher activities on orientation change.
1747 if (mModel != null) {
1748 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1749 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001750
1751 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001752 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001753
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001754 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001755 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1756 mWorkspace.removeAllViews();
1757 mWorkspace = null;
1758 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001759
Michael Jurka2ecf9952012-06-18 12:52:28 -07001760 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 }
1762
Adam Cohena9cf38f2011-05-02 15:36:58 -07001763 public DragController getDragController() {
1764 return mDragController;
1765 }
1766
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 @Override
1768 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001769 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 super.startActivityForResult(intent, requestCode);
1771 }
1772
Winson Chung70d72102011-08-12 11:24:35 -07001773 /**
1774 * Indicates that we want global search for this activity by setting the globalSearch
1775 * argument for {@link #startSearch} to true.
1776 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001778 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001780
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001781 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001782
Karl Rosaen138a0412009-04-23 19:00:21 -07001783 if (initialQuery == null) {
1784 // Use any text typed in the launcher as the initial query
1785 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001786 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 if (appSearchData == null) {
1788 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001789 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
Winson Chungadf0c182012-08-23 14:59:07 -07001791 Rect sourceBounds = new Rect();
1792 if (mSearchDropTargetBar != null) {
1793 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1794 }
Romain Guycbb89e42009-06-08 15:52:54 -07001795
Bjorn Bringertc459e522013-06-07 19:36:01 +01001796 startSearch(initialQuery, selectInitialQuery,
1797 appSearchData, sourceBounds);
1798 }
1799
1800 public void startSearch(String initialQuery,
1801 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001802 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001803 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001804 }
1805
1806 /**
1807 * Starts the global search activity. This code is a copied from SearchManager
1808 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001809 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001810 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001811 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001812 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1813 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1814 if (globalSearchActivity == null) {
1815 Log.w(TAG, "No global search activity found.");
1816 return;
1817 }
1818 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1819 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1820 intent.setComponent(globalSearchActivity);
1821 // Make sure that we have a Bundle to put source in
1822 if (appSearchData == null) {
1823 appSearchData = new Bundle();
1824 } else {
1825 appSearchData = new Bundle(appSearchData);
1826 }
1827 // Set source to package name of app that starts global search, if not set already.
1828 if (!appSearchData.containsKey("source")) {
1829 appSearchData.putString("source", getPackageName());
1830 }
1831 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1832 if (!TextUtils.isEmpty(initialQuery)) {
1833 intent.putExtra(SearchManager.QUERY, initialQuery);
1834 }
1835 if (selectInitialQuery) {
1836 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1837 }
1838 intent.setSourceBounds(sourceBounds);
1839 try {
1840 startActivity(intent);
1841 } catch (ActivityNotFoundException ex) {
1842 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1843 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
1845
Winson Chung70d72102011-08-12 11:24:35 -07001846 @Override
1847 public boolean onCreateOptionsMenu(Menu menu) {
1848 if (isWorkspaceLocked()) {
1849 return false;
1850 }
1851
1852 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001853
1854 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1855 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1856 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001857 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1858 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1859 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001860 String helpUrl = getString(R.string.help_url);
1861 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001862 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1863 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1864
Winson Chung70d72102011-08-12 11:24:35 -07001865 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1866 .setIcon(android.R.drawable.ic_menu_gallery)
1867 .setAlphabeticShortcut('W');
1868 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1869 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001870 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001871 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001872 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1873 .setIcon(android.R.drawable.ic_menu_preferences)
1874 .setIntent(settings)
1875 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001876 if (!helpUrl.isEmpty()) {
1877 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1878 .setIcon(android.R.drawable.ic_menu_help)
1879 .setIntent(help)
1880 .setAlphabeticShortcut('H');
1881 }
Winson Chung70d72102011-08-12 11:24:35 -07001882 return true;
1883 }
1884
1885 @Override
1886 public boolean onPrepareOptionsMenu(Menu menu) {
1887 super.onPrepareOptionsMenu(menu);
1888
1889 if (mAppsCustomizeTabHost.isTransitioning()) {
1890 return false;
1891 }
1892 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1893 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1894
1895 return true;
1896 }
1897
1898 @Override
1899 public boolean onOptionsItemSelected(MenuItem item) {
1900 switch (item.getItemId()) {
1901 case MENU_WALLPAPER_SETTINGS:
1902 startWallpaper();
1903 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001904 }
1905
1906 return super.onOptionsItemSelected(item);
1907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001909 @Override
1910 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001911 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001912 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001913 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001914 }
1915
Joe Onorato9c1289c2009-08-17 11:03:03 -04001916 public boolean isWorkspaceLocked() {
1917 return mWorkspaceLoading || mWaitingForResult;
1918 }
1919
Michael Jurka0280c3b2010-09-17 15:00:07 -07001920 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001922 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001923 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1924 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001925 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001926 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001927 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001928
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001929 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1930 AppWidgetProviderInfo appWidgetInfo) {
1931 if (appWidgetInfo.configure != null) {
1932 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001933
Bjorn Bringert7984c942009-12-09 15:38:25 +00001934 // Launch over to configure widget, if needed
1935 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001936 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001937 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001938 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001940 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001941 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001942 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001943 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001944 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946 }
Romain Guycbb89e42009-06-08 15:52:54 -07001947
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001948 protected void moveToCustomContentScreen(boolean animate) {
1949 mWorkspace.moveToCustomContentScreen(animate);
1950 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001951 /**
1952 * Process a shortcut drop.
1953 *
1954 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001955 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001956 * @param cell The cell it should be added to, optional
1957 * @param position The location on the screen where it was dropped, optional
1958 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001959 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001960 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001961 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001962 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001964 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001965
1966 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001967 mPendingAddInfo.cellX = cell[0];
1968 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001969 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001970
1971 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1972 createShortcutIntent.setComponent(componentName);
1973 processShortcut(createShortcutIntent);
1974 }
1975
Adam Cohenfbba09b2011-07-18 21:43:05 -07001976 /**
1977 * Process a widget drop.
1978 *
1979 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001981 * @param cell The cell it should be added to, optional
1982 * @param position The location on the screen where it was dropped, optional
1983 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001984 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001985 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001986 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001987 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001989 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001990 mPendingAddInfo.minSpanX = info.minSpanX;
1991 mPendingAddInfo.minSpanY = info.minSpanY;
1992
Adam Cohenfbba09b2011-07-18 21:43:05 -07001993 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001994 mPendingAddInfo.cellX = cell[0];
1995 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001996 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001997 if (span != null) {
1998 mPendingAddInfo.spanX = span[0];
1999 mPendingAddInfo.spanY = span[1];
2000 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002001
Adam Cohened66b2b2012-01-23 17:28:51 -08002002 AppWidgetHostView hostView = info.boundWidget;
2003 int appWidgetId;
2004 if (hostView != null) {
2005 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002006 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002007 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002008 // In this case, we either need to start an activity to get permission to bind
2009 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002010 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002011 Bundle options = info.bindOptions;
2012
2013 boolean success = false;
2014 if (options != null) {
2015 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2016 info.componentName, options);
2017 } else {
2018 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2019 info.componentName);
2020 }
2021 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002022 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002023 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002024 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002025 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2026 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2027 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002028 // TODO: we need to make sure that this accounts for the options bundle.
2029 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002030 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2031 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002032 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002033 }
2034
Joe Onoratodeb98af2010-02-19 14:59:39 -08002035 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002036 // Handle case where user selected "Applications"
2037 String applicationName = getResources().getString(R.string.group_applications);
2038 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002039
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002040 if (applicationName != null && applicationName.equals(shortcutName)) {
2041 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2042 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002043
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002044 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2045 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002046 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002047 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002048 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002049 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 }
2052
Winson Chung24ab2f12010-09-16 14:10:47 -07002053 void processWallpaper(Intent intent) {
2054 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2055 }
2056
Adam Cohendcd297f2013-06-18 13:13:40 -07002057 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002058 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002059 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 folderInfo.title = getText(R.string.folder_name);
2061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002063 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002064 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002065 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066
2067 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002068 FolderIcon newFolder =
2069 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002070 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002071 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002072 // Force measure the new folder icon
2073 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2074 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002075 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 }
Romain Guycbb89e42009-06-08 15:52:54 -07002077
Joe Onorato9c1289c2009-08-17 11:03:03 -04002078 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002079 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002080 }
2081
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002082 protected void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002083 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002085 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002086 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002089 protected ComponentName getWallpaperPickerComponent() {
2090 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2091 }
2092
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002093 /**
2094 * Registers various content observers. The current implementation registers
2095 * only a favorites observer to keep track of the favorites applications.
2096 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002097 private void registerContentObservers() {
2098 ContentResolver resolver = getContentResolver();
2099 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2100 true, mWidgetObserver);
2101 }
2102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 @Override
2104 public boolean dispatchKeyEvent(KeyEvent event) {
2105 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2106 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002108 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002109 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002110 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002111 dumpState();
2112 return true;
2113 }
2114 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002115 }
2116 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2117 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002118 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 return true;
2120 }
2121 }
2122
2123 return super.dispatchKeyEvent(event);
2124 }
2125
Joe Onorato88ec0992009-11-19 13:16:06 -08002126 @Override
2127 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002128 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002129 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002130 } else if (mWorkspace.isInOverviewMode()) {
2131 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002132 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002133 Folder openFolder = mWorkspace.getOpenFolder();
2134 if (openFolder.isEditingName()) {
2135 openFolder.dismissEditingName();
2136 } else {
2137 closeFolder();
2138 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002139 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002140 mWorkspace.exitWidgetResizeMode();
2141
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002142 // Back button is a no-op here, but give at least some feedback for the button press
2143 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002144 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002145 }
2146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002148 * Re-listen when widgets are reset.
2149 */
2150 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002151 if (mAppWidgetHost != null) {
2152 mAppWidgetHost.startListening();
2153 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002154 }
2155
2156 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 * Launches the intent referred by the clicked shortcut.
2158 *
2159 * @param v The view representing the clicked shortcut.
2160 */
2161 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002162 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2163 // view has detached (it's possible for this to happen if the view is removed mid touch).
2164 if (v.getWindowToken() == null) {
2165 return;
2166 }
2167
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002168 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002169 return;
2170 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002171
Adam Cohenf358a4b2013-07-23 16:47:31 -07002172 if (v instanceof PageIndicator) {
2173 if (!mWorkspace.isInOverviewMode()) {
2174 mWorkspace.enterOverviewMode();
2175 }
2176 return;
2177 }
2178
2179 if (v instanceof CellLayout) {
2180 if (mWorkspace.isInOverviewMode()) {
2181 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2182 }
2183 }
2184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002186 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002188 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2189 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002190
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002191 // Check for special shortcuts
2192 if (intent.getComponent() != null) {
2193 final String shortcutClass = intent.getComponent().getClassName();
2194
2195 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chungc58497e2013-09-03 17:48:37 -07002196 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002197 return;
2198 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2199 MemoryDumpActivity.startDump(this);
2200 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002201 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2202 toggleShowWeightWatcher();
2203 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002204 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002205 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002206
2207 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002208 int[] pos = new int[2];
2209 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002210 intent.setSourceBounds(new Rect(pos[0], pos[1],
2211 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002212
2213 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002214
Daniel Sandlerff02d492013-08-05 02:12:05 -04002215 mStats.recordLaunch(intent, shortcut);
2216
Michael Jurkaddd62e92011-02-16 17:49:14 -08002217 if (success && v instanceof BubbleTextView) {
2218 mWaitingForResume = (BubbleTextView) v;
2219 mWaitingForResume.setStayPressed(true);
2220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002222 if (v instanceof FolderIcon) {
2223 FolderIcon fi = (FolderIcon) v;
2224 handleFolderClick(fi);
2225 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002226 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002227 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002228 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002229 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002230 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
2233 }
2234
Michael Jurka0e260592010-06-30 17:07:39 -07002235 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002236 return false;
2237 }
2238
Michael Jurkaaf442092010-06-10 17:01:57 -07002239 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002240 * Event handler for the search button
2241 *
2242 * @param v The view that was clicked.
2243 */
2244 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002245 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2246
Amith Yamasania135ba82011-08-09 17:42:01 -07002247 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002248 }
2249
2250 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002251 * Event handler for the voice button
2252 *
2253 * @param v The view that was clicked.
2254 */
2255 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002256 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002257
Bjorn Bringertc459e522013-06-07 19:36:01 +01002258 startVoice();
2259 }
2260
2261 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002262 try {
2263 final SearchManager searchManager =
2264 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2265 ComponentName activityName = searchManager.getGlobalSearchActivity();
2266 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002267 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002268 if (activityName != null) {
2269 intent.setPackage(activityName.getPackageName());
2270 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002271 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002272 } catch (ActivityNotFoundException e) {
2273 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002274 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002275 startActivitySafely(null, intent, "onClickVoiceButton");
2276 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002277 }
2278
2279 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002280 * Event handler for the "grid" button that appears on the home screen, which
2281 * enters all apps mode.
2282 *
2283 * @param v The view that was clicked.
2284 */
2285 public void onClickAllAppsButton(View v) {
Winson Chungc58497e2013-09-03 17:48:37 -07002286 showAllApps(true, AppsCustomizePagedView.ContentType.Applications);
Michael Jurka5130e402011-10-13 04:55:35 -07002287 }
2288
2289 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002290 // Provide the same haptic feedback that the system offers for virtual keys.
2291 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002292 }
2293
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002294 public void onClickAppMarketButton(View v) {
2295 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002296 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002297 } else {
2298 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002299 }
2300 }
2301
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002302 void startApplicationDetailsActivity(ComponentName componentName) {
2303 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002304 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2305 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002306 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002307 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002308 }
2309
Michael Jurka1e2f4652013-07-08 18:03:46 -07002310 // returns true if the activity was started
2311 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002312 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002313 // System applications cannot be installed. For now, show a toast explaining that.
2314 // We may give them the option of disabling apps this way.
2315 int messageId = R.string.uninstall_system_app_text;
2316 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002317 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002318 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002319 String packageName = componentName.getPackageName();
2320 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002321 Intent intent = new Intent(
2322 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002323 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2324 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002325 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002326 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002327 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002328 }
2329
Michael Jurka86a720e2012-05-09 11:23:23 -07002330 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002331 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002334 // Only launch using the new animation if the shortcut has not opted out (this is a
2335 // private contract between launcher and may be ignored in the future).
2336 boolean useLaunchAnimation = (v != null) &&
2337 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2338 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002339 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2340 v.getMeasuredWidth(), v.getMeasuredHeight());
2341
2342 startActivity(intent, opts.toBundle());
2343 } else {
2344 startActivity(intent);
2345 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002346 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 } catch (SecurityException e) {
2348 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002349 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002351 "or use the exported attribute for this activity. "
2352 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002354 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002356
Michael Jurka86a720e2012-05-09 11:23:23 -07002357 boolean startActivitySafely(View v, Intent intent, Object tag) {
2358 boolean success = false;
2359 try {
2360 success = startActivity(v, intent, tag);
2361 } catch (ActivityNotFoundException e) {
2362 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2363 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2364 }
2365 return success;
2366 }
2367
Adam Cohena9cf38f2011-05-02 15:36:58 -07002368 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002369 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002370 Folder openFolder = mWorkspace.getFolderForTag(info);
2371
2372 // If the folder info reports that the associated folder is open, then verify that
2373 // it is actually opened. There have been a few instances where this gets out of sync.
2374 if (info.opened && openFolder == null) {
2375 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002376 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002377 info.opened = false;
2378 }
2379
Adam Cohenfb91f302012-06-11 15:45:18 -07002380 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 // Close any open folder
2382 closeFolder();
2383 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002384 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 } else {
2386 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387 int folderScreen;
2388 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002389 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 // .. and close it
2391 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002392 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 // Close any folder open on the current screen
2394 closeFolder();
2395 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002396 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 }
2398 }
2399 }
2400 }
2401
Adam Cohen268c4752012-06-06 17:47:33 -07002402 /**
2403 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2404 * in the DragLayer in the exact absolute location of the original FolderIcon.
2405 */
2406 private void copyFolderIconToImage(FolderIcon fi) {
2407 final int width = fi.getMeasuredWidth();
2408 final int height = fi.getMeasuredHeight();
2409
2410 // Lazy load ImageView, Bitmap and Canvas
2411 if (mFolderIconImageView == null) {
2412 mFolderIconImageView = new ImageView(this);
2413 }
2414 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2415 mFolderIconBitmap.getHeight() != height) {
2416 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2417 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2418 }
2419
2420 DragLayer.LayoutParams lp;
2421 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2422 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2423 } else {
2424 lp = new DragLayer.LayoutParams(width, height);
2425 }
2426
Adam Cohen307fe232012-08-16 17:55:58 -07002427 // The layout from which the folder is being opened may be scaled, adjust the starting
2428 // view size by this scale factor.
2429 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002430 lp.customPosition = true;
2431 lp.x = mRectForFolderAnimation.left;
2432 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002433 lp.width = (int) (scale * width);
2434 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002435
2436 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2437 fi.draw(mFolderIconCanvas);
2438 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002439 if (fi.getFolder() != null) {
2440 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2441 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002442 }
Adam Cohen268c4752012-06-06 17:47:33 -07002443 // Just in case this image view is still in the drag layer from a previous animation,
2444 // we remove it and re-add it.
2445 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2446 mDragLayer.removeView(mFolderIconImageView);
2447 }
2448 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002449 if (fi.getFolder() != null) {
2450 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002451 }
Adam Cohen268c4752012-06-06 17:47:33 -07002452 }
2453
Adam Cohen2801caf2011-05-13 20:57:39 -07002454 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002455 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002456 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2457 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2458 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2459
Adam Cohenc51934b2011-07-26 21:07:43 -07002460 FolderInfo info = (FolderInfo) fi.getTag();
2461 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2462 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002463 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2464 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002465 }
2466
Adam Cohen268c4752012-06-06 17:47:33 -07002467 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2468 copyFolderIconToImage(fi);
2469 fi.setVisibility(View.INVISIBLE);
2470
Michael Jurka2ecf9952012-06-18 12:52:28 -07002471 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002472 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002473 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2474 oa.start();
2475 }
2476
Adam Cohen268c4752012-06-06 17:47:33 -07002477 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002478 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002479 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2480 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2481 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2482
Adam Cohen268c4752012-06-06 17:47:33 -07002483 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002484
Adam Cohen268c4752012-06-06 17:47:33 -07002485 // We remove and re-draw the FolderIcon in-case it has changed
2486 mDragLayer.removeView(mFolderIconImageView);
2487 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002488 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002489 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002490 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002491 oa.addListener(new AnimatorListenerAdapter() {
2492 @Override
2493 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002494 if (cl != null) {
2495 cl.clearFolderLeaveBehind();
2496 // Remove the ImageView copy of the FolderIcon and make the original visible.
2497 mDragLayer.removeView(mFolderIconImageView);
2498 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002499 }
2500 }
2501 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002502 oa.start();
2503 }
2504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002506 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 * is animated relative to the specified View. If the View is null, no animation
2508 * is played.
2509 *
2510 * @param folderInfo The FolderInfo describing the folder to open.
2511 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002512 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002513 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002514 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515
Adam Cohena9cf38f2011-05-02 15:36:58 -07002516 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517
Adam Cohen4554ee12011-08-03 16:13:21 -07002518 // Just verify that the folder hasn't already been added to the DragLayer.
2519 // There was a one-off crash where the folder had a parent already.
2520 if (folder.getParent() == null) {
2521 mDragLayer.addView(folder);
2522 mDragController.addDropTarget((DropTarget) folder);
2523 } else {
2524 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2525 folder.getParent() + ").");
2526 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002527 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002528 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002529
2530 // Notify the accessibility manager that this folder "window" has appeared and occluded
2531 // the workspace items
2532 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2533 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002534 }
2535
2536 public void closeFolder() {
2537 Folder folder = mWorkspace.getOpenFolder();
2538 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002539 if (folder.isEditingName()) {
2540 folder.dismissEditingName();
2541 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002542 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002543
2544 // Dismiss the folder cling
2545 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002546 }
2547 }
2548
2549 void closeFolder(Folder folder) {
2550 folder.getInfo().opened = false;
2551
2552 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2553 if (parent != null) {
2554 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2555 shrinkAndFadeInFolderIcon(fi);
2556 }
2557 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002558
2559 // Notify the accessibility manager that this folder "window" has disappeard and no
2560 // longer occludeds the workspace items
2561 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002562 }
2563
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002565 if (!isDraggingEnabled()) return false;
2566 if (isWorkspaceLocked()) return false;
2567 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568
2569 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002570 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571 }
2572
Michael Jurka0280c3b2010-09-17 15:00:07 -07002573 resetAddInfo();
2574 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002576 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 return true;
2578 }
2579
Winson Chung3d503fb2011-07-13 17:25:49 -07002580 // The hotseat touch handling does not go through Workspace, and we always allow long press
2581 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002582 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002583 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2584 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002585 if (itemUnderLongClick == null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07002586 if (mWorkspace.hasNonCustomEmptyScreens()) {
2587 // User long pressed on empty space
2588 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2589 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2590 // Disabling reordering until we sort out some issues.
2591 if (mWorkspace.isInOverviewMode()) {
2592 mWorkspace.startReordering(v);
2593 } else {
2594 mWorkspace.enterOverviewMode();
2595 }
Adam Cohendedbd962013-07-11 14:21:49 -07002596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002598 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002600 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 }
2602 }
2603 }
2604 return true;
2605 }
2606
Winson Chung3d503fb2011-07-13 17:25:49 -07002607 boolean isHotseatLayout(View layout) {
2608 return mHotseat != null && layout != null &&
2609 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2610 }
2611 Hotseat getHotseat() {
2612 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002613 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002614 View getOverviewPanel() {
2615 return mOverviewPanel;
2616 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002617 SearchDropTargetBar getSearchBar() {
2618 return mSearchDropTargetBar;
2619 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002620
Winson Chung3d503fb2011-07-13 17:25:49 -07002621 /**
2622 * Returns the CellLayout of the specified container at the specified screen.
2623 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002624 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002625 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2626 if (mHotseat != null) {
2627 return mHotseat.getLayout();
2628 } else {
2629 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002630 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002631 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002632 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002633 }
2634 }
2635
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002636 Workspace getWorkspace() {
2637 return mWorkspace;
2638 }
2639
Daniel Sandler843e8602010-06-07 14:59:01 -04002640 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002641 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002642 }
2643
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002644 /**
2645 * Helper method for the cameraZoomIn/cameraZoomOut animations
2646 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002647 * @param scaleFactor The scale factor used for the zoom
2648 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002649 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002650 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002651 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002652 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002653
Winson Chung18f41f82012-05-09 13:28:10 -07002654 void disableWallpaperIfInAllApps() {
2655 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002656 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002657 if (mAppsCustomizeTabHost != null &&
2658 !mAppsCustomizeTabHost.isTransitioning()) {
2659 updateWallpaperVisibility(false);
2660 }
2661 }
2662 }
2663
Craig Mautner360310b2012-10-26 15:13:08 -07002664 private void setWorkspaceBackground(boolean workspace) {
2665 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002666 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002667 }
2668
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002669 void updateWallpaperVisibility(boolean visible) {
2670 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2671 int curflags = getWindow().getAttributes().flags
2672 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2673 if (wpflags != curflags) {
2674 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2675 }
Craig Mautner360310b2012-10-26 15:13:08 -07002676 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002677 }
2678
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002679 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2680 if (v instanceof LauncherTransitionable) {
2681 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2682 }
2683 }
2684
Michael Jurkabed61d22012-02-14 22:51:29 -08002685 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2686 if (v instanceof LauncherTransitionable) {
2687 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2688 }
Winson Chung70442722012-02-10 15:43:22 -08002689
2690 // Update the workspace transition step as well
2691 dispatchOnLauncherTransitionStep(v, 0f);
2692 }
2693
2694 private void dispatchOnLauncherTransitionStep(View v, float t) {
2695 if (v instanceof LauncherTransitionable) {
2696 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2697 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002698 }
2699
2700 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2701 if (v instanceof LauncherTransitionable) {
2702 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2703 }
Winson Chung70442722012-02-10 15:43:22 -08002704
2705 // Update the workspace transition step as well
2706 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002707 }
2708
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002709 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002710 * Things to test when changing the following seven functions.
2711 * - Home from workspace
2712 * - from center screen
2713 * - from other screens
2714 * - Home from all apps
2715 * - from center screen
2716 * - from other screens
2717 * - Back from all apps
2718 * - from center screen
2719 * - from other screens
2720 * - Launch app from workspace and quit
2721 * - with back
2722 * - with home
2723 * - Launch app from all apps and quit
2724 * - with back
2725 * - with home
2726 * - Go to a screen that's not the default, then all
2727 * apps, and launch and app, and go back
2728 * - with back
2729 * -with home
2730 * - On workspace, long press power and go back
2731 * - with back
2732 * - with home
2733 * - On all apps, long press power and go back
2734 * - with back
2735 * - with home
2736 * - On workspace, power off
2737 * - On all apps, power off
2738 * - Launch an app and turn off the screen while in that app
2739 * - Go back with home key
2740 * - Go back with back key TODO: make this not go to workspace
2741 * - From all apps
2742 * - From workspace
2743 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2744 * - From all apps
2745 * - From the center workspace
2746 * - From another workspace
2747 */
2748
2749 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002750 * Zoom the camera out from the workspace to reveal 'toView'.
2751 * Assumes that the view to show is anchored at either the very top or very bottom
2752 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002753 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002754 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002755 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2756 showAppsCustomizeHelper(animated, springLoaded, contentType);
2757 }
2758 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2759 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002760 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002761 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002762 mStateAnimation.cancel();
2763 mStateAnimation = null;
2764 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002766
Winson Chungf0ea4d32011-06-06 14:27:16 -07002767 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2768 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2769 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002770 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002771 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002772 final int startDelay =
2773 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002774
Michael Jurkab3e22d92011-10-31 15:58:33 -07002775 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002776
Michael Jurkad74c9842011-07-10 12:44:21 -07002777 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002778 Animator workspaceAnim =
2779 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002780 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2781 // Set the content type for the all apps space
2782 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2783 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002784
2785 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002786 toView.setScaleX(scale);
2787 toView.setScaleY(scale);
2788 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2789 scaleAnim.
2790 scaleX(1f).scaleY(1f).
2791 setDuration(duration).
2792 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002793
Winson Chungf0ea4d32011-06-06 14:27:16 -07002794 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002795 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002796 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002797 .ofFloat(toView, "alpha", 0f, 1f)
2798 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002799 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002800 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2801 @Override
2802 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002803 if (animation == null) {
2804 throw new RuntimeException("animation is null");
2805 }
Winson Chung70442722012-02-10 15:43:22 -08002806 float t = (Float) animation.getAnimatedValue();
2807 dispatchOnLauncherTransitionStep(fromView, t);
2808 dispatchOnLauncherTransitionStep(toView, t);
2809 }
2810 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002811
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002812 // toView should appear right at the end of the workspace shrink
2813 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002814 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002815 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002816 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002817
2818 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002819 boolean animationCancelled = false;
2820
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002821 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002822 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002823 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002824 // Prepare the position
2825 toView.setTranslationX(0.0f);
2826 toView.setTranslationY(0.0f);
2827 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002828 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002830 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002831 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002832 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2833 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002834
Adam Cohenf4ddea32011-09-12 13:46:42 -07002835 if (!animationCancelled) {
2836 updateWallpaperVisibility(false);
2837 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002838
2839 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002840 if (mSearchDropTargetBar != null) {
2841 mSearchDropTargetBar.hideSearchBar(false);
2842 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002843 }
2844
Adam Cohencff6af82011-09-13 14:51:53 -07002845 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002846 public void onAnimationCancel(Animator animation) {
2847 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002848 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002849 });
2850
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002851 if (workspaceAnim != null) {
2852 mStateAnimation.play(workspaceAnim);
2853 }
Michael Jurka1899a362011-11-03 13:50:45 -07002854
2855 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002856
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002857 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2858 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002859
2860 // If any of the objects being animated haven't been measured/laid out
2861 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002862 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002863 (mWorkspace.getMeasuredWidth() == 0) ||
2864 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002865 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002866 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002867
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002868 final AnimatorSet stateAnimation = mStateAnimation;
2869 final Runnable startAnimRunnable = new Runnable() {
2870 public void run() {
2871 // Check that mStateAnimation hasn't changed while
2872 // we waited for a layout/draw pass
2873 if (mStateAnimation != stateAnimation)
2874 return;
2875 setPivotsForZoom(toView, scale);
2876 dispatchOnLauncherTransitionStart(fromView, animated, false);
2877 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002878 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002879 }
2880 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002881 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002882 final ViewTreeObserver observer = toView.getViewTreeObserver();
2883 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2884 public void onGlobalLayout() {
2885 startAnimRunnable.run();
2886 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2887 }
2888 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002889 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002890 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002891 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002892 } else {
2893 toView.setTranslationX(0.0f);
2894 toView.setTranslationY(0.0f);
2895 toView.setScaleX(1.0f);
2896 toView.setScaleY(1.0f);
2897 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002898 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002899
Daniel Sandlere4f98912013-06-25 15:13:26 -04002900 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002901 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002902 if (mSearchDropTargetBar != null) {
2903 mSearchDropTargetBar.hideSearchBar(false);
2904 }
Michael Jurkaabded662011-03-04 12:06:57 -08002905 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002906 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002907 dispatchOnLauncherTransitionStart(fromView, animated, false);
2908 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002909 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002910 dispatchOnLauncherTransitionStart(toView, animated, false);
2911 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002912 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002913 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002914 }
2915
2916 /**
2917 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002918 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002919 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002920 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002921 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2922 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002923
Michael Jurkab3e22d92011-10-31 15:58:33 -07002924 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002925 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002926 mStateAnimation.cancel();
2927 mStateAnimation = null;
2928 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002929 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002930
Winson Chungf0ea4d32011-06-06 14:27:16 -07002931 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002932 final int fadeOutDuration =
2933 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002934 final float scaleFactor = (float)
2935 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2936 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002937 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002938 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002939 if (toState == State.WORKSPACE) {
2940 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2941 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002942 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002943 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2944 workspaceAnim = mWorkspace.getChangeStateAnimation(
2945 Workspace.State.SPRING_LOADED, animated);
2946 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002947
Michael Jurkab3e22d92011-10-31 15:58:33 -07002948 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002949 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002950 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002951 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002952 final LauncherViewPropertyAnimator scaleAnim =
2953 new LauncherViewPropertyAnimator(fromView);
2954 scaleAnim.
2955 scaleX(scaleFactor).scaleY(scaleFactor).
2956 setDuration(duration).
2957 setInterpolator(new Workspace.ZoomInInterpolator());
2958
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002959 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002960 .ofFloat(fromView, "alpha", 1f, 0f)
2961 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002962 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002963 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2964 @Override
2965 public void onAnimationUpdate(ValueAnimator animation) {
2966 float t = 1f - (Float) animation.getAnimatedValue();
2967 dispatchOnLauncherTransitionStep(fromView, t);
2968 dispatchOnLauncherTransitionStep(toView, t);
2969 }
2970 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002971
Michael Jurka2ecf9952012-06-18 12:52:28 -07002972 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002973
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002974 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2975 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002976 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002977
2978 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002979 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002980 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002981 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002982 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002983 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2984 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002985 if (onCompleteRunnable != null) {
2986 onCompleteRunnable.run();
2987 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002988 mAppsCustomizeContent.updateCurrentPageScroll();
2989 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002990 }
2991 });
2992
Winson Chungf0ea4d32011-06-06 14:27:16 -07002993 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002994 if (workspaceAnim != null) {
2995 mStateAnimation.play(workspaceAnim);
2996 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002997 dispatchOnLauncherTransitionStart(fromView, animated, true);
2998 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002999 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003000 } else {
3001 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003002 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003003 dispatchOnLauncherTransitionStart(fromView, animated, true);
3004 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003005 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003006 dispatchOnLauncherTransitionStart(toView, animated, true);
3007 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003008 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003009 }
3010
Michael Jurkae326f182011-11-21 14:05:46 -08003011 @Override
3012 public void onTrimMemory(int level) {
3013 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003014 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003015 mAppsCustomizeTabHost.onTrimMemory();
3016 }
3017 }
3018
Winson Chung18f41f82012-05-09 13:28:10 -07003019 @Override
3020 public void onWindowFocusChanged(boolean hasFocus) {
3021 if (!hasFocus) {
3022 // When another window occludes launcher (like the notification shade, or recents),
3023 // ensure that we enable the wallpaper flag so that transitions are done correctly.
3024 updateWallpaperVisibility(true);
3025 } else {
3026 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07003027 mWorkspace.postDelayed(new Runnable() {
3028 @Override
3029 public void run() {
3030 disableWallpaperIfInAllApps();
3031 }
3032 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07003033 }
3034 }
3035
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003036 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003037 showWorkspace(animated, null);
3038 }
3039
3040 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003041 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003042 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003043 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003044 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003045
Winson Chungc7d2b602012-05-16 17:02:20 -07003046 // Show the search bar (only animate if we were showing the drop target bar in spring
3047 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003048 if (mSearchDropTargetBar != null) {
3049 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3050 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003051
Michael Jurkab3e22d92011-10-31 15:58:33 -07003052 // Set focus to the AppsCustomize button
3053 if (mAllAppsButton != null) {
3054 mAllAppsButton.requestFocus();
3055 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003056 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003057
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003058 // Change the state *after* we've called all the transition code
3059 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003060
Winson Chung5fb63472011-02-02 17:03:37 -08003061 // Resume the auto-advance of widgets
3062 mUserPresent = true;
3063 updateRunning();
3064
alanv1d4fde62012-10-17 13:15:47 -07003065 // Send an accessibility event to announce the context change
3066 getWindow().getDecorView()
3067 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003068
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003069 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003070 }
3071
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003072 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003073 }
3074
Winson Chungc58497e2013-09-03 17:48:37 -07003075 void showAllApps(boolean animated,
3076 AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003077 if (mState != State.WORKSPACE) return;
3078
Winson Chungc58497e2013-09-03 17:48:37 -07003079 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003080 mAppsCustomizeTabHost.requestFocus();
3081
Michael Jurkab3e22d92011-10-31 15:58:33 -07003082 // Change the state *after* we've called all the transition code
3083 mState = State.APPS_CUSTOMIZE;
3084
3085 // Pause the auto-advance of widgets until we are out of AllApps
3086 mUserPresent = false;
3087 updateRunning();
3088 closeFolder();
3089
3090 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003091 getWindow().getDecorView()
3092 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003093 }
3094
Adam Cohen7777d962011-08-18 18:58:38 -07003095 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003096 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003097 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003098 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003099 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003100 }
Adam Cohen7777d962011-08-18 18:58:38 -07003101
Adam Cohened66b2b2012-01-23 17:28:51 -08003102 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3103 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003104 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3105
Winson Chunge7a03942011-08-05 15:05:12 -07003106 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003107 @Override
3108 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003109 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003110 // Before we show workspace, hide all apps again because
3111 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3112 // clean up our state transition functions
3113 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003114 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003115 } else {
3116 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003117 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003118 }
3119 }, (extendedDelay ?
3120 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3121 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3122 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003123
Michael Jurkad3ef3062010-11-23 16:23:58 -08003124 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003125 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003126 final boolean animated = true;
3127 final boolean springLoaded = true;
3128 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003129 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003130 }
3131 // Otherwise, we are not in spring loaded mode, so don't do anything.
3132 }
3133
Michael Jurkab3e22d92011-10-31 15:58:33 -07003134 void lockAllApps() {
3135 // TODO
3136 }
3137
3138 void unlockAllApps() {
3139 // TODO
3140 }
3141
Winson Chungf0ea4d32011-06-06 14:27:16 -07003142 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003143 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003144 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003145 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003146 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003147 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003148 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003149 int duration = 0;
3150 if (mSearchDropTargetBar != null) {
3151 duration = mSearchDropTargetBar.getTransitionInDuration();
3152 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003153 mHotseat.animate().alpha(1f).setDuration(duration);
3154 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003155 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003156 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003157 }
3158 }
3159 }
3160
3161 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003162 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003163 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003164 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003165 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003166 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003167 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003168 int duration = 0;
3169 if (mSearchDropTargetBar != null) {
3170 duration = mSearchDropTargetBar.getTransitionOutDuration();
3171 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003172 mHotseat.animate().alpha(0f).setDuration(duration);
3173 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003174 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003175 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003176 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003177 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003178 }
3179
Patrick Dubroy5f445422011-02-18 14:35:21 -08003180 /**
3181 * Add an item from all apps or customize onto the given workspace screen.
3182 * If layout is null, add to the current screen.
3183 */
3184 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003185 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003186 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003187 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003188 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003189
Winson Chungdff8ebb2011-09-08 17:25:31 -07003190 /** Maps the current orientation to an index for referencing orientation correct global icons */
3191 private int getCurrentOrientationIndexForGlobalIcons() {
3192 // default - 0, landscape - 1
3193 switch (getResources().getConfiguration().orientation) {
3194 case Configuration.ORIENTATION_LANDSCAPE:
3195 return 1;
3196 default:
3197 return 0;
3198 }
3199 }
3200
Michael Jurkaae65ee32012-04-30 11:45:54 -07003201 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003202 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003203 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003204 // Look for the toolbar icon specified in the activity meta-data
3205 Bundle metaData = packageManager.getActivityInfo(
3206 activityName, PackageManager.GET_META_DATA).metaData;
3207 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003208 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003209 if (iconResId != 0) {
3210 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003211 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003212 }
3213 }
3214 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003215 // This can happen if the activity defines an invalid drawable
3216 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3217 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003218 } catch (Resources.NotFoundException nfe) {
3219 // This can happen if the activity defines an invalid drawable
3220 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3221 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003222 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003223 return null;
3224 }
3225
3226 // if successful in getting icon, return it; otherwise, set button to use default drawable
3227 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003228 int buttonId, ComponentName activityName, int fallbackDrawableId,
3229 String toolbarResourceName) {
3230 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003231 Resources r = getResources();
3232 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3233 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003234
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003235 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003236 // If we were unable to find the icon via the meta-data, use a generic one
3237 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003238 toolbarIcon = r.getDrawable(fallbackDrawableId);
3239 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003240 if (button != null) {
3241 button.setCompoundDrawables(toolbarIcon, null, null, null);
3242 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003243 return null;
3244 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003245 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003246 if (button != null) {
3247 button.setCompoundDrawables(toolbarIcon, null, null, null);
3248 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003249 return toolbarIcon.getConstantState();
3250 }
3251 }
3252
3253 // if successful in getting icon, return it; otherwise, set button to use default drawable
3254 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003255 int buttonId, ComponentName activityName, int fallbackDrawableId,
3256 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003257 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003258 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003259
Michael Jurka19e0fc52011-07-22 18:00:21 -07003260 if (button != null) {
3261 // If we were unable to find the icon via the meta-data, use a
3262 // generic one
3263 if (toolbarIcon == null) {
3264 button.setImageResource(fallbackDrawableId);
3265 } else {
3266 button.setImageDrawable(toolbarIcon);
3267 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003268 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003269
3270 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3271
Michael Jurka0423dcf2010-10-05 14:56:18 -07003272 }
3273
Winson Chung1b884092012-06-08 17:13:02 -07003274 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003275 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003276 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003277 }
3278
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003279 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003280 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003281 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003282 }
3283
Winson Chungbb185bd2011-11-21 12:31:42 -08003284 private void invalidatePressedFocusedStates(View container, View button) {
3285 if (container instanceof HolographicLinearLayout) {
3286 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3287 layout.invalidatePressedFocusedStates();
3288 } else if (button instanceof HolographicImageView) {
3289 HolographicImageView view = (HolographicImageView) button;
3290 view.invalidatePressedFocusedStates();
3291 }
3292 }
3293
Cristina Stancu476493b2013-08-07 17:20:14 +01003294 public View getQsbBar() {
3295 if (mQsbBar == null) {
3296 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3297 }
3298 return mQsbBar;
3299 }
3300
3301 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003302 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003303 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003304 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003305 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003306
Winson Chung1cad91e2011-05-25 17:41:01 -07003307 final SearchManager searchManager =
3308 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3309 ComponentName activityName = searchManager.getGlobalSearchActivity();
3310 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003311 int coi = getCurrentOrientationIndexForGlobalIcons();
3312 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003313 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3314 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3315 if (sGlobalSearchIcon[coi] == null) {
3316 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3317 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3318 TOOLBAR_ICON_METADATA_NAME);
3319 }
3320
Winson Chungc51db6a2011-10-05 11:44:49 -07003321 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3322 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003323 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003324 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003325 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003326 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003327 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3328 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3329 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003330 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003331 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003332 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003333 }
3334 }
3335
Cristina Stancu476493b2013-08-07 17:20:14 +01003336 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003337 final View searchButtonContainer = findViewById(R.id.search_button_container);
3338 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003339 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003340 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003341 }
3342
Cristina Stancu476493b2013-08-07 17:20:14 +01003343 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003344 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003345 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003346
Winson Chungc51db6a2011-10-05 11:44:49 -07003347 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003348 final SearchManager searchManager =
3349 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3350 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3351
3352 ComponentName activityName = null;
3353 if (globalSearchActivity != null) {
3354 // Check if the global search activity handles voice search
3355 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3356 intent.setPackage(globalSearchActivity.getPackageName());
3357 activityName = intent.resolveActivity(getPackageManager());
3358 }
3359
3360 if (activityName == null) {
3361 // Fallback: check if an activity other than the global search activity
3362 // resolves this
3363 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3364 activityName = intent.resolveActivity(getPackageManager());
3365 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003366 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003367 int coi = getCurrentOrientationIndexForGlobalIcons();
3368 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003369 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3370 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3371 if (sVoiceSearchIcon[coi] == null) {
3372 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3373 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3374 TOOLBAR_ICON_METADATA_NAME);
3375 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003376 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003377 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003378 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003379 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003380 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003381 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003382 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003383 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003384 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003385 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003386 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003387 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003388
Cristina Stancu476493b2013-08-07 17:20:14 +01003389 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003390 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3391 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003392 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003393 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003394 }
3395
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003396 public void setVoiceButtonProxyVisible(boolean visible) {
3397 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3398 if (voiceButtonProxy != null) {
3399 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3400 }
3401 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003402 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003403 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003404 */
3405 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003406 final View marketButton = findViewById(R.id.market_button);
3407 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3408 // Find the app market activity by resolving an intent.
3409 // (If multiple app markets are installed, it will return the ResolverActivity.)
3410 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003411 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003412 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003413 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003414 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003415 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3416 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003417 marketButton.setVisibility(View.VISIBLE);
3418 } else {
3419 // We should hide and disable the view so that we don't try and restore the visibility
3420 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3421 marketButton.setVisibility(View.GONE);
3422 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003423 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003424 }
3425
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003426 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003427 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3428 Resources r = getResources();
3429 Drawable marketIconDrawable = d.newDrawable(r);
3430 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3431 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3432 marketIconDrawable.setBounds(0, 0, w, h);
3433
3434 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003435 }
3436
Michael Jurkad7c28052012-04-27 15:43:36 -07003437 @Override
3438 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003439 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003440 final List<CharSequence> text = event.getText();
3441 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003442 // Populate event with a fake title based on the current state.
3443 if (mState == State.APPS_CUSTOMIZE) {
3444 text.add(getString(R.string.all_apps_button_label));
3445 } else {
3446 text.add(getString(R.string.all_apps_home_button_label));
3447 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003448 return result;
3449 }
3450
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003451 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003452 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003453 */
3454 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3455 @Override
3456 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003457 closeSystemDialogs();
3458 }
3459 }
3460
3461 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003462 * Receives notifications whenever the appwidgets are reset.
3463 */
3464 private class AppWidgetResetObserver extends ContentObserver {
3465 public AppWidgetResetObserver() {
3466 super(new Handler());
3467 }
3468
3469 @Override
3470 public void onChange(boolean selfChange) {
3471 onAppWidgetReset();
3472 }
3473 }
3474
3475 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003476 * If the activity is currently paused, signal that we need to run the passed Runnable
3477 * in onResume.
3478 *
3479 * This needs to be called from incoming places where resources might have been loaded
3480 * while we are paused. That is becaues the Configuration might be wrong
3481 * when we're not running, and if it comes back to what it was when we
3482 * were paused, we are not restarted.
3483 *
3484 * Implementation of the method from LauncherModel.Callbacks.
3485 *
3486 * @return true if we are currently paused. The caller might be able to
3487 * skip some work in that case since we will come back again.
3488 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003489 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003490 if (mPaused) {
3491 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003492 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003493 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003494 }
3495 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003496 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003497 return true;
3498 } else {
3499 return false;
3500 }
3501 }
3502
Michael Jurkac402cd92013-05-20 15:49:32 +02003503 private boolean waitUntilResume(Runnable run) {
3504 return waitUntilResume(run, false);
3505 }
3506
Michael Jurka1e2f4652013-07-08 18:03:46 -07003507 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003508 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003509 }
3510
Michael Jurka7607c2f2013-04-03 14:33:19 -07003511 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003512 * If the activity is currently paused, signal that we need to re-run the loader
3513 * in onResume.
3514 *
3515 * This needs to be called from incoming places where resources might have been loaded
3516 * while we are paused. That is becaues the Configuration might be wrong
3517 * when we're not running, and if it comes back to what it was when we
3518 * were paused, we are not restarted.
3519 *
3520 * Implementation of the method from LauncherModel.Callbacks.
3521 *
3522 * @return true if we are currently paused. The caller might be able to
3523 * skip some work in that case since we will come back again.
3524 */
3525 public boolean setLoadOnResume() {
3526 if (mPaused) {
3527 Log.i(TAG, "setLoadOnResume");
3528 mOnResumeNeedsLoad = true;
3529 return true;
3530 } else {
3531 return false;
3532 }
3533 }
3534
3535 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003537 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003539 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003540 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003541 } else {
3542 return SCREEN_COUNT / 2;
3543 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003544 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003545
Joe Onorato9c1289c2009-08-17 11:03:03 -04003546 /**
3547 * Refreshes the shortcuts shown on the workspace.
3548 *
3549 * Implementation of the method from LauncherModel.Callbacks.
3550 */
3551 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003552 // If we're starting binding all over again, clear any bind calls we'd postponed in
3553 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3554 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003555 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003556
Winson Chungd64d1762013-08-20 14:37:16 -07003557 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003558 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003559 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003560
Michael Jurka05bf6442011-11-30 20:28:53 -08003561 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003562 if (mHotseat != null) {
3563 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003564 }
3565 }
3566
Adam Cohendcd297f2013-06-18 13:13:40 -07003567 @Override
3568 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003569 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003570
Adam Cohen5084cba2013-09-03 12:01:16 -07003571 // If there are no screens, we need to have an empty screen
3572 if (orderedScreenIds.size() == 0) {
3573 mWorkspace.addExtraEmptyScreen();
3574 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003575
3576 // Create the custom content page (this call updates mDefaultScreen which calls
3577 // setCurrentPage() so ensure that all pages are added before calling this)
3578 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3579 mWorkspace.createCustomContentPage();
3580 }
Winson Chung64359a52013-07-08 17:17:08 -07003581 }
3582
3583 @Override
3584 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003585 int count = orderedScreenIds.size();
3586 for (int i = 0; i < count; i++) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003587 Launcher.addDumpLog(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")", true);
Adam Cohen89bddfa2013-08-20 11:57:13 -07003588 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003589 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003590 }
3591
Adam Cohen39a06042013-07-19 14:30:12 -07003592 private boolean shouldShowWeightWatcher() {
3593 String spKey = LauncherAppState.getSharedPreferencesKey();
3594 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003595 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003596
3597 return show;
3598 }
3599
Adam Cohen4caf2982013-08-20 18:54:31 -07003600 private boolean emailSent() {
3601 String spKey = LauncherAppState.getSharedPreferencesKey();
3602 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3603 boolean show = sp.getBoolean(CORRUPTION_EMAIL_SENT_KEY, false);
3604 return show;
3605 }
3606
3607 private void setEmailSent(boolean sent) {
3608 String spKey = LauncherAppState.getSharedPreferencesKey();
3609 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3610
3611 SharedPreferences.Editor editor = sp.edit();
3612 editor.putBoolean(CORRUPTION_EMAIL_SENT_KEY, sent);
3613 editor.commit();
3614 }
3615
Adam Cohen39a06042013-07-19 14:30:12 -07003616 private void toggleShowWeightWatcher() {
3617 String spKey = LauncherAppState.getSharedPreferencesKey();
3618 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3619 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3620
3621 show = !show;
3622
3623 SharedPreferences.Editor editor = sp.edit();
3624 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3625 editor.commit();
3626
3627 if (mWeightWatcher != null) {
3628 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3629 }
3630 }
3631
Winson Chungd64d1762013-08-20 14:37:16 -07003632 public void bindAppsAdded(final ArrayList<Long> newScreens,
3633 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003634 final ArrayList<ItemInfo> addAnimated,
3635 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003636 Runnable r = new Runnable() {
3637 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003638 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003639 }
3640 };
3641 if (waitUntilResume(r)) {
3642 return;
3643 }
3644
Adam Cohen4caf2982013-08-20 18:54:31 -07003645 Launcher.addDumpLog(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -07003646
3647 // Add the new screens
3648 bindAddScreens(newScreens);
3649
3650 // We add the items without animation on non-visible pages, and with
3651 // animations on the new page (which we will try and snap to).
3652 if (!addNotAnimated.isEmpty()) {
3653 bindItems(addNotAnimated, 0,
3654 addNotAnimated.size(), false);
3655 }
3656 if (!addAnimated.isEmpty()) {
3657 bindItems(addAnimated, 0,
3658 addAnimated.size(), true);
3659 }
Winson Chungc58497e2013-09-03 17:48:37 -07003660
3661 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3662 addedApps != null && mAppsCustomizeContent != null) {
3663 mAppsCustomizeContent.addApps(addedApps);
3664 }
Winson Chungd64d1762013-08-20 14:37:16 -07003665 }
3666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003667 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003668 * Bind the items start-end from the list.
3669 *
3670 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003671 */
Winson Chung64359a52013-07-08 17:17:08 -07003672 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3673 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003674 Runnable r = new Runnable() {
3675 public void run() {
3676 bindItems(shortcuts, start, end, forceAnimateIcons);
3677 }
3678 };
3679 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003680 return;
3681 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003682
Winson Chungf0c6ae02012-03-21 16:10:31 -07003683 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003684 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3685 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003686 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003687 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003688 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003689 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003690 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003691
3692 // Short circuit if we are loading dock items for a configuration which has no dock
3693 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3694 mHotseat == null) {
3695 continue;
3696 }
3697
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698 switch (item.itemType) {
3699 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3700 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003701 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003702 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003703
Winson Chung64359a52013-07-08 17:17:08 -07003704 /*
3705 * TODO: FIX collision case
3706 */
Winson Chungce376632013-07-11 16:12:41 -07003707 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3708 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3709 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3710 throw new RuntimeException("OCCUPIED");
3711 }
Winson Chung64359a52013-07-08 17:17:08 -07003712 }
3713
Adam Cohendcd297f2013-06-18 13:13:40 -07003714 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3715 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003716 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003717 // Animate all the applications up now
3718 shortcut.setAlpha(0f);
3719 shortcut.setScaleX(0f);
3720 shortcut.setScaleY(0f);
3721 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003722 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003723 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003725 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003726 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003727 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003728 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003729 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3730 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003732 default:
3733 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003735 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003736
Winson Chung997a9232013-07-24 15:33:46 -07003737 if (animateIcons) {
3738 // Animate to the correct page
3739 if (newShortcutsScreenId > -1) {
3740 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3741 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3742 if (newShortcutsScreenId != currentScreenId) {
3743 mWorkspace.snapToPage(newScreenIndex);
3744 }
3745 }
3746
Winson Chung64359a52013-07-08 17:17:08 -07003747 // We post the animation slightly delayed to prevent slowdowns when we are loading
3748 // right after we return to launcher.
3749 mWorkspace.postDelayed(new Runnable() {
3750 public void run() {
3751 anim.playTogether(bounceAnims);
3752 anim.start();
3753 }
3754 }, NEW_APPS_ANIMATION_DELAY);
3755 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003756 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003757 }
3758
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 /**
3760 * Implementation of the method from LauncherModel.Callbacks.
3761 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003762 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003763 Runnable r = new Runnable() {
3764 public void run() {
3765 bindFolders(folders);
3766 }
3767 };
3768 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003769 return;
3770 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003771 sFolders.clear();
3772 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003773 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003774
3775 /**
3776 * Add the views for a widget to the workspace.
3777 *
3778 * Implementation of the method from LauncherModel.Callbacks.
3779 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003780 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003781 Runnable r = new Runnable() {
3782 public void run() {
3783 bindAppWidget(item);
3784 }
3785 };
3786 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003787 return;
3788 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003789
Daniel Sandler843e8602010-06-07 14:59:01 -04003790 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3791 if (DEBUG_WIDGETS) {
3792 Log.d(TAG, "bindAppWidget: " + item);
3793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003794 final Workspace workspace = mWorkspace;
3795
3796 final int appWidgetId = item.appWidgetId;
3797 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003798 if (DEBUG_WIDGETS) {
3799 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3800 }
3801
Joe Onorato9c1289c2009-08-17 11:03:03 -04003802 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3803
Joe Onorato9c1289c2009-08-17 11:03:03 -04003804 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003805 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003806
Adam Cohendcd297f2013-06-18 13:13:40 -07003807 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003809 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3810
Joe Onorato9c1289c2009-08-17 11:03:03 -04003811 workspace.requestLayout();
3812
Daniel Sandler843e8602010-06-07 14:59:01 -04003813 if (DEBUG_WIDGETS) {
3814 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3815 + (SystemClock.uptimeMillis()-start) + "ms");
3816 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 }
3818
Adam Cohen1462de32012-07-24 22:34:36 -07003819 public void onPageBoundSynchronously(int page) {
3820 mSynchronouslyBoundPages.add(page);
3821 }
3822
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 /**
3824 * Callback saying that there aren't any more items to bind.
3825 *
3826 * Implementation of the method from LauncherModel.Callbacks.
3827 */
Adam Cohene25af792013-06-06 23:08:25 -07003828 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003829 Runnable r = new Runnable() {
3830 public void run() {
3831 finishBindingItems(upgradePath);
3832 }
3833 };
3834 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003835 return;
3836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 if (mSavedState != null) {
3838 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003839 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003840 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003841 mSavedState = null;
3842 }
3843
Adam Cohen1462de32012-07-24 22:34:36 -07003844 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003845
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003846 // If we received the result of any pending adds while the loader was running (e.g. the
3847 // widget configuration forced an orientation change), process them now.
3848 for (int i = 0; i < sPendingAddList.size(); i++) {
3849 completeAdd(sPendingAddList.get(i));
3850 }
3851 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852
Winson Chungc51db6a2011-10-05 11:44:49 -07003853 // Update the market app icon as necessary (the other icons will be managed in response to
3854 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003855 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003856
Winson Chungf0c6ae02012-03-21 16:10:31 -07003857 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003858 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003859 mWorkspace.getUniqueComponents(true, null);
3860 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003861 }
Adam Cohen99894d92013-06-14 11:22:59 -07003862
Adam Cohen66a01fd2013-06-11 12:48:00 -07003863 mWorkspace.post(new Runnable() {
3864 @Override
3865 public void run() {
3866 onFinishBindingItems();
3867 }
3868 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003869
3870 // Write all the logs to disk
3871 Launcher.addDumpLog(TAG, "10249126 - finishBindingItems() - dumping logs to disk", true);
3872 dumpLogsToLocalData(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003873 }
3874
Winson Chunga2413752012-04-03 14:22:34 -07003875 private boolean canRunNewAppsAnimation() {
3876 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3877 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3878 }
3879
Winson Chungc9168342013-06-26 14:54:55 -07003880 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3881 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3882 PropertyValuesHolder.ofFloat("alpha", 1f),
3883 PropertyValuesHolder.ofFloat("scaleX", 1f),
3884 PropertyValuesHolder.ofFloat("scaleY", 1f));
3885 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3886 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3887 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3888 return bounceAnim;
3889 }
3890
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003891 @Override
3892 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003893 boolean searchVisible = updateGlobalSearchIcon();
3894 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003895 if (mSearchDropTargetBar != null) {
3896 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3897 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003898 }
3899
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003900 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 * Add the icons for all apps.
3902 *
3903 * Implementation of the method from LauncherModel.Callbacks.
3904 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003905 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003906 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3907 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3908 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3909 getHotseat().addAllAppsFolder(mIconCache, apps,
3910 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3911 }
3912 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003913 }
Winson Chungc58497e2013-09-03 17:48:37 -07003914 } else {
3915 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3916 mAppsCustomizeContent != null) {
3917 mAppsCustomizeContent.setApps(apps);
3918 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003919 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003920 }
3921
Bjorn Bringert85f418d2013-09-06 12:50:05 +01003922 @Override
3923 public boolean shouldShowApp(ResolveInfo app) {
3924 return true;
3925 }
3926
Joe Onorato9c1289c2009-08-17 11:03:03 -04003927 /**
3928 * A package was updated.
3929 *
3930 * Implementation of the method from LauncherModel.Callbacks.
3931 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003932 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003933 Runnable r = new Runnable() {
3934 public void run() {
3935 bindAppsUpdated(apps);
3936 }
3937 };
3938 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003939 return;
3940 }
3941
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003942 if (mWorkspace != null) {
3943 mWorkspace.updateShortcuts(apps);
3944 }
Winson Chungc58497e2013-09-03 17:48:37 -07003945
3946 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3947 mAppsCustomizeContent != null) {
3948 mAppsCustomizeContent.updateApps(apps);
3949 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003950 }
3951
3952 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003953 * A package was uninstalled. We take both the super set of packageNames
3954 * in addition to specific applications to remove, the reason being that
3955 * this can be called when a package is updated as well. In that scenario,
3956 * we only remove specific components from the workspace, where as
3957 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003958 *
3959 * Implementation of the method from LauncherModel.Callbacks.
3960 */
Winson Chung83892cc2013-05-01 16:53:33 -07003961 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003962 final ArrayList<AppInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003963 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003964 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003965 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003966 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003967 }
Winson Chungd64d1762013-08-20 14:37:16 -07003968 };
3969 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003970 return;
3971 }
3972
Winson Chung64359a52013-07-08 17:17:08 -07003973 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003974 mWorkspace.removeItemsByPackageName(packageNames);
3975 } else {
3976 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003977 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003978
Winson Chunga1820962011-10-03 16:31:06 -07003979 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003980 mDragController.onAppsRemoved(appInfos, this);
Winson Chungc58497e2013-09-03 17:48:37 -07003981
3982 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3983 mAppsCustomizeContent != null) {
3984 mAppsCustomizeContent.removeApps(appInfos);
3985 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003986 }
Joe Onoratobe386092009-11-17 17:32:16 -08003987
3988 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003989 * A number of packages were updated.
3990 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003991 private ArrayList<Object> mWidgetsAndShortcuts;
3992 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003993 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003994 bindPackagesUpdated(mWidgetsAndShortcuts);
3995 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003996 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003997 };
3998
3999 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4000 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4001 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004002 return;
4003 }
4004
Winson Chung64359a52013-07-08 17:17:08 -07004005 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004006 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4007 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004008 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004009 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004010 }
4011
Winson Chung400438b2011-01-16 17:53:48 -08004012 private int mapConfigurationOriActivityInfoOri(int configOri) {
4013 final Display d = getWindowManager().getDefaultDisplay();
4014 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4015 switch (d.getRotation()) {
4016 case Surface.ROTATION_0:
4017 case Surface.ROTATION_180:
4018 // We are currently in the same basic orientation as the natural orientation
4019 naturalOri = configOri;
4020 break;
4021 case Surface.ROTATION_90:
4022 case Surface.ROTATION_270:
4023 // We are currently in the other basic orientation to the natural orientation
4024 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4025 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4026 break;
4027 }
4028
4029 int[] oriMap = {
4030 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4031 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4032 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4033 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4034 };
4035 // Since the map starts at portrait, we need to offset if this device's natural orientation
4036 // is landscape.
4037 int indexOffset = 0;
4038 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4039 indexOffset = 1;
4040 }
4041 return oriMap[(d.getRotation() + indexOffset) % 4];
4042 }
Adam Cohen446e9402011-09-15 18:21:21 -07004043
Winson Chung4b919f82012-05-01 10:44:08 -07004044 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004045 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004046 getResources().getBoolean(R.bool.allow_rotation);
4047 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004048 }
Winson Chung4b919f82012-05-01 10:44:08 -07004049 public void lockScreenOrientation() {
4050 if (isRotationEnabled()) {
4051 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4052 .getConfiguration().orientation));
4053 }
4054 }
4055 public void unlockScreenOrientation(boolean immediate) {
4056 if (isRotationEnabled()) {
4057 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004058 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004059 } else {
4060 mHandler.postDelayed(new Runnable() {
4061 public void run() {
4062 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4063 }
4064 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004065 }
Winson Chung4b919f82012-05-01 10:44:08 -07004066 }
Winson Chung400438b2011-01-16 17:53:48 -08004067 }
4068
Winson Chung82f55532011-08-09 14:14:23 -07004069 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004070 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004071 if (DISABLE_CLINGS) {
4072 return false;
4073 }
4074
Brett Chabot2a9e2282011-08-23 15:03:13 -07004075 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004076 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004077
Michael Jurkae233a8b2013-03-19 13:49:20 +01004078 // Restricted secondary users (child mode) will potentially have very few apps
4079 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004080// boolean supportsLimitedUsers =
4081// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4082// Account[] accounts = AccountManager.get(this).getAccounts();
4083// if (supportsLimitedUsers && accounts.length == 0) {
4084// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4085// Bundle restrictions = um.getUserRestrictions();
4086// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4087// return false;
4088// }
4089// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004090 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004091 }
Michael Jurka22143132012-10-04 17:42:38 +02004092
Winson Chung7d7541e2011-09-16 20:14:36 -07004093 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004094 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004095 if (cling != null) {
4096 cling.init(this, positionData);
4097 cling.setVisibility(View.VISIBLE);
4098 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4099 if (animate) {
4100 cling.buildLayer();
4101 cling.setAlpha(0f);
4102 cling.animate()
4103 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004104 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004105 .setDuration(SHOW_CLING_DURATION)
4106 .setStartDelay(delay)
4107 .start();
4108 } else {
4109 cling.setAlpha(1f);
4110 }
Michael Jurka22143132012-10-04 17:42:38 +02004111 cling.setFocusableInTouchMode(true);
4112 cling.post(new Runnable() {
4113 public void run() {
4114 cling.setFocusable(true);
4115 cling.requestFocus();
4116 }
4117 });
4118 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4119 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004120 }
4121 return cling;
4122 }
Michael Jurka22143132012-10-04 17:42:38 +02004123
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004125 // To catch cases where siblings of top-level views are made invisible, just check whether
4126 // the cling is directly set to GONE before dismissing it.
4127 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004128 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004129 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004130 anim.addListener(new AnimatorListenerAdapter() {
4131 public void onAnimationEnd(Animator animation) {
4132 cling.setVisibility(View.GONE);
4133 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004134 // We should update the shared preferences on a background thread
4135 new Thread("dismissClingThread") {
4136 public void run() {
4137 SharedPreferences.Editor editor = mSharedPrefs.edit();
4138 editor.putBoolean(flag, true);
4139 editor.commit();
4140 }
4141 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004142 };
4143 });
4144 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004145 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004146 }
4147 }
Michael Jurka22143132012-10-04 17:42:38 +02004148
Winson Chung9d9d74f2011-09-19 11:49:12 -07004149 private void removeCling(int id) {
4150 final View cling = findViewById(id);
4151 if (cling != null) {
4152 final ViewGroup parent = (ViewGroup) cling.getParent();
4153 parent.post(new Runnable() {
4154 @Override
4155 public void run() {
4156 parent.removeView(cling);
4157 }
4158 });
Michael Jurka22143132012-10-04 17:42:38 +02004159 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004160 }
4161 }
Michael Jurka974c3862012-05-22 22:00:31 -07004162
4163 private boolean skipCustomClingIfNoAccounts() {
4164 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4165 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4166 if (customCling) {
4167 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004168 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004169 Account[] accounts = am.getAccountsByType("com.google");
4170 return accounts.length == 0;
4171 }
4172 return false;
4173 }
4174
Winson Chung7d7541e2011-09-16 20:14:36 -07004175 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004176 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004177 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004178 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4179 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004180 // If we're not using the default workspace layout, replace workspace cling
4181 // with a custom workspace cling (usually specified in an overlay)
4182 // For now, only do this on tablets
4183 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004184 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004185 // Use a custom cling
4186 View cling = findViewById(R.id.workspace_cling);
4187 ViewGroup clingParent = (ViewGroup) cling.getParent();
4188 int clingIndex = clingParent.indexOfChild(cling);
4189 clingParent.removeViewAt(clingIndex);
4190 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4191 clingParent.addView(customCling, clingIndex);
4192 customCling.setId(R.id.workspace_cling);
4193 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004194 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004195 } else {
4196 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004197 }
4198 }
4199 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004200 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004201 if (isClingsEnabled() &&
4202 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004203 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004204 } else {
4205 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004206 }
4207 }
4208 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004209 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004210 if (isClingsEnabled() &&
4211 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4212 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004213 } else {
4214 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004215 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004216 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004217 }
Michael Jurka104c4562013-07-08 18:03:46 -07004218 protected SharedPreferences getSharedPrefs() {
4219 return mSharedPrefs;
4220 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004221 public boolean isFolderClingVisible() {
4222 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004223 if (cling != null) {
4224 return cling.getVisibility() == View.VISIBLE;
4225 }
4226 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004227 }
Winson Chung82f55532011-08-09 14:14:23 -07004228 public void dismissWorkspaceCling(View v) {
4229 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004230 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004231 }
4232 public void dismissAllAppsCling(View v) {
4233 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004234 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4235 }
4236 public void dismissFolderCling(View v) {
4237 Cling cling = (Cling) findViewById(R.id.folder_cling);
4238 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004239 }
4240
Winson Chung80baf5a2010-08-09 16:03:15 -07004241 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004242 * Prints out out state for debugging.
4243 */
4244 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004245 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004246 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004247 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4248 Log.d(TAG, "mRestoring=" + mRestoring);
4249 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4250 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004251 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004252 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004253
Winson Chung785d2eb2011-04-14 16:08:02 -07004254 if (mAppsCustomizeContent != null) {
4255 mAppsCustomizeContent.dumpState();
4256 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004257 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004258 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004259
4260 @Override
4261 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4262 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004263 synchronized (sDumpLogs) {
4264 writer.println(" ");
4265 writer.println("Debug logs: ");
4266 for (int i = 0; i < sDumpLogs.size(); i++) {
4267 writer.println(" " + sDumpLogs.get(i));
4268 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004269 }
4270 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004271
4272 public static void dumpDebugLogsToConsole() {
Adam Cohen4caf2982013-08-20 18:54:31 -07004273 synchronized (sDumpLogs) {
4274 Log.d(TAG, "");
4275 Log.d(TAG, "*********************");
4276 Log.d(TAG, "Launcher debug logs: ");
4277 for (int i = 0; i < sDumpLogs.size(); i++) {
4278 Log.d(TAG, " " + sDumpLogs.get(i));
4279 }
4280 Log.d(TAG, "*********************");
4281 Log.d(TAG, "");
Adam Cohen487f7dd2012-06-28 18:12:10 -07004282 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004283 }
4284
4285 public static void addDumpLog(String tag, String log, boolean debugLog) {
4286 if (debugLog) {
4287 Log.d(tag, log);
4288 }
4289 sDateStamp.setTime(System.currentTimeMillis());
4290 synchronized (sDumpLogs) {
4291 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
4292 }
4293 }
4294
4295 public void dumpLogsToLocalData(final boolean email) {
4296 new Thread("DumpLogsToLocalData") {
4297 @Override
4298 public void run() {
4299 boolean success = false;
4300 sDateStamp.setTime(sRunStart);
4301 String FILENAME = sDateStamp.getMonth() + "-"
4302 + sDateStamp.getDay() + "_"
4303 + sDateStamp.getHours() + "-"
4304 + sDateStamp.getMinutes() + "_"
4305 + sDateStamp.getSeconds() + ".txt";
4306
4307 FileOutputStream fos = null;
4308 File outFile = null;
4309 try {
4310 outFile = new File(getFilesDir(), FILENAME);
4311 outFile.createNewFile();
4312 fos = new FileOutputStream(outFile);
4313 } catch (Exception e) {
4314 e.printStackTrace();
4315 }
4316 if (fos != null) {
4317 PrintWriter writer = new PrintWriter(fos);
4318
4319 writer.println(" ");
4320 writer.println("Debug logs: ");
4321 synchronized (sDumpLogs) {
4322 for (int i = 0; i < sDumpLogs.size(); i++) {
4323 writer.println(" " + sDumpLogs.get(i));
4324 }
4325 }
4326 writer.close();
4327 }
4328 try {
4329 if (fos != null) {
4330 fos.close();
4331 success = true;
4332 }
4333 } catch (IOException e) {
4334 e.printStackTrace();
4335 }
4336
4337 if (success && email) {
4338 if (!emailSent()) {
4339 emailFile(outFile);
4340 }
4341 }
4342 }
4343 }.start();
4344 }
4345
4346 private void emailFile(File file) {
4347 File publicCopy = new File(Environment.getExternalStorageDirectory(), file.getName());
4348 try {
4349 copyFile(file, publicCopy);
4350 } catch (IOException e) {
4351 e.printStackTrace();
4352 return;
4353 }
4354
4355 Intent intent = new Intent(Intent.ACTION_SEND);
4356 intent.setType("text/plain");
4357 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"adamcohen@google.com, winsonc@google.com," +
4358 "mikejurka@google"});
4359 intent.putExtra(Intent.EXTRA_SUBJECT, "Data corruption " + file.getName());
4360 intent.putExtra(Intent.EXTRA_TEXT, "Data corruption has occurred, logs attached");
4361
4362 if (!file.exists() || !file.canRead()) {
4363 Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
4364 finish();
4365 return;
4366 }
4367
4368 Toast.makeText(this, "Data corruption has occurred, please send e-mail", Toast.LENGTH_LONG);
4369 Uri uri = Uri.fromFile(publicCopy);
4370 intent.putExtra(Intent.EXTRA_STREAM, uri);
4371 startActivity(Intent.createChooser(intent, "Please send logs, consider clearing data"));
4372
4373 setEmailSent(true);
4374 }
4375
4376 public void copyFile(File src, File dst) throws IOException {
4377 InputStream in = new FileInputStream(src);
4378 OutputStream out = new FileOutputStream(dst);
4379
4380 // Transfer bytes from in to out
4381 byte[] buf = new byte[1024];
4382 int len;
4383 while ((len = in.read(buf)) > 0) {
4384 out.write(buf, 0, len);
4385 }
4386 in.close();
4387 out.close();
Adam Cohen487f7dd2012-06-28 18:12:10 -07004388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004389}
Michael Jurka2763be32011-02-24 11:19:57 -08004390
Michael Jurkaabded662011-03-04 12:06:57 -08004391interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004392 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004393 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004394 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004395 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004396 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004397}