blob: d1959dfec5bb35cb45307b1b11702800caf21f42 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
53import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070057import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070062import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070068import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070071import android.util.Pair;
Daniel Sandler924b9932013-06-12 00:38:25 -040072import android.view.*;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080073import android.view.View.OnLongClickListener;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080074import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080075import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070076import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070077import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080078import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.inputmethod.InputMethodManager;
Daniel Sandler924b9932013-06-12 00:38:25 -040080import android.widget.*;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070081
Daniel Sandler325dc232013-06-05 22:57:57 -040082import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080083
Adam Cohenc0dcf592011-06-01 15:30:43 -070084import java.io.DataInputStream;
85import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070086import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070087import java.io.FileNotFoundException;
88import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070089import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -070090import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -070091import java.util.Collection;
92import java.util.Collections;
93import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070094import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -070095import java.util.HashSet;
Winson Chungc9168342013-06-26 14:54:55 -070096import java.util.Iterator;
Michael Jurkad7c28052012-04-27 15:43:36 -070097import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -070098import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100/**
101 * Default launcher application.
102 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100103public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700104 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700105 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700107 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Daniel Sandlerf061f822013-06-27 13:59:36 -0400109 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400110 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700111 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400112 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700113
Winson Chung70d72102011-08-12 11:24:35 -0700114 private static final int MENU_GROUP_WALLPAPER = 1;
115 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
116 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700117 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
118 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
120 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700121 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 private static final int REQUEST_PICK_APPLICATION = 6;
123 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700125 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Michael Jurka8b805b12012-04-18 14:23:14 -0700127 private static final int REQUEST_BIND_APPWIDGET = 11;
128
Mathew Inwood876a8462013-06-14 14:12:41 +0100129 /**
130 * IntentStarter uses request codes starting with this. This must be greater than all activity
131 * request codes used internally.
132 */
133 protected static final int REQUEST_LAST = 100;
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
136
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800137 static final int SCREEN_COUNT = 5;
138 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Romain Guy98d01652009-06-30 16:21:04 -0700140 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800141 // To turn on these properties, type
142 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
143 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
144 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Winson Chung2672ff92012-05-04 16:22:30 -0700146 // The Intent extra that defines whether to ignore the launch animation
147 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400148 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 // Type: int
151 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700152 // Type: int
153 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700155 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
156 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
158 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: boolean
163 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
164 // Type: long
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700166 // Type: int
167 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
170 // Type: parcelable
171 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100173 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700174 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100175 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700176 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100177 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700178
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700179 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700180 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700181 private State mState = State.WORKSPACE;
182 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800183 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700186 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
187 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700188 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700189 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800192 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Winson Chunga2413752012-04-03 14:22:34 -0700194 // How long to wait before the new-shortcut animation automatically pans the workspace
195 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700196 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700197
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800198 private final BroadcastReceiver mCloseSystemDialogsReceiver
199 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800200 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private LayoutInflater mInflater;
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800205 private View mQsbDivider;
206 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700207 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800208 private DragLayer mDragLayer;
209 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700210
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700211 private AppWidgetManager mAppWidgetManager;
212 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700213
Michael Jurkac9d95c52011-08-29 14:03:34 -0700214 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700215 private AppWidgetProviderInfo mPendingAddWidgetInfo;
216
Michael Jurka0280c3b2010-09-17 15:00:07 -0700217 private int[] mTmpAddItemCellCoordinates = new int[2];
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private FolderInfo mFolderInfo;
220
Winson Chung3d503fb2011-07-13 17:25:49 -0700221 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800222 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700223
Winson Chungc51db6a2011-10-05 11:44:49 -0700224 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700225 private AppsCustomizeTabHost mAppsCustomizeTabHost;
226 private AppsCustomizePagedView mAppsCustomizeContent;
227 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700230 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
231 // scroll issues (because the workspace may not have been measured yet) and extra work.
232 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
233 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
235 private SpannableStringBuilder mDefaultKeySsb = null;
236
Joe Onorato9c1289c2009-08-17 11:03:03 -0400237 private boolean mWorkspaceLoading = true;
238
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800239 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 private boolean mRestoring;
241 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700242 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243
Michael Jurka7607c2f2013-04-03 14:33:19 -0700244 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
245
Winson Chung4a2afa32012-07-19 14:53:05 -0700246 // Keep track of whether the user has left launcher
247 private static boolean sPausedFromUserAction = false;
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private Bundle mSavedInstanceState;
250
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800252 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800253 private boolean mUserPresent = true;
254 private boolean mVisible = false;
255 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700256 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700258 private static LocaleConfiguration sLocaleConfiguration = null;
259
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700260 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700261
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700262 private Intent mAppMarketIntent = null;
263
Adam Cohended9f8d2010-11-03 13:25:16 -0700264 // Related to the auto-advancing of widgets
265 private final int ADVANCE_MSG = 1;
266 private final int mAdvanceInterval = 20000;
267 private final int mAdvanceStagger = 250;
268 private long mAutoAdvanceSentTime;
269 private long mAutoAdvanceTimeLeft = -1;
270 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
271 new HashMap<View, AppWidgetProviderInfo>();
272
Winson Chung400438b2011-01-16 17:53:48 -0800273 // Determines how long to wait after a rotation before restoring the screen orientation to
274 // match the sensor state.
275 private final int mRestoreScreenOrientationDelay = 500;
276
Michael Jurka4ef207b2010-11-29 17:05:45 -0800277 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700278 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
279 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
280 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800281
Craig Mautner360310b2012-10-26 15:13:08 -0700282 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700283
Adam Cohen1462de32012-07-24 22:34:36 -0700284 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
285
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700286 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
287
Winson Chung46353de2012-02-16 14:05:10 -0800288 // We only want to get the SharedPreferences once since it does an FS stat each time we get
289 // it from the context.
290 private SharedPreferences mSharedPrefs;
291
Adam Cohene25af792013-06-06 23:08:25 -0700292 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
293
Winson Chungf0c6ae02012-03-21 16:10:31 -0700294 // Holds the page that we need to animate to, and the icon views that we need to animate up
295 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700296 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700297 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700298 private ImageView mFolderIconImageView;
299 private Bitmap mFolderIconBitmap;
300 private Canvas mFolderIconCanvas;
301 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700302
Michael Jurkaddd62e92011-02-16 17:49:14 -0800303 private BubbleTextView mWaitingForResume;
304
Michael Jurka22143132012-10-04 17:42:38 +0200305 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
306 = new HideFromAccessibilityHelper();
307
Michael Jurkac1f5d262011-09-30 19:32:27 -0700308 private Runnable mBuildLayersRunnable = new Runnable() {
309 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700310 if (mWorkspace != null) {
311 mWorkspace.buildPageHardwareLayers();
312 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700313 }
314 };
315
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800316 private static ArrayList<PendingAddArguments> sPendingAddList
317 = new ArrayList<PendingAddArguments>();
318
Michael Jurka0a457bf2012-11-19 14:05:05 -0800319 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
320
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800321 private static class PendingAddArguments {
322 int requestCode;
323 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700324 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700325 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800326 int cellX;
327 int cellY;
328 }
329
Michael Jurka0a457bf2012-11-19 14:05:05 -0800330 private static boolean isPropertyEnabled(String propertyName) {
331 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700332 }
333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 @Override
335 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700336 if (DEBUG_STRICT_MODE) {
337 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
338 .detectDiskReads()
339 .detectDiskWrites()
340 .detectNetwork() // or .detectAll() for all detectable problems
341 .penaltyLog()
342 .build());
343 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
344 .detectLeakedSqlLiteObjects()
345 .detectLeakedClosableObjects()
346 .penaltyLog()
347 .penaltyDeath()
348 .build());
349 }
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400352
353 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
354 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400355 LauncherAppState app = LauncherAppState.getInstance();
356 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700357 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800358 mModel = app.setLauncher(this);
359 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400360 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700362
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700363 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700364 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
365 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700366
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700367 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
368 // this also ensures that any synchronous binding below doesn't re-trigger another
369 // LauncherModel load.
370 mPaused = false;
371
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200373 android.os.Debug.startMethodTracing(
374 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 }
376
377 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 setContentView(R.layout.launcher);
379 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700380 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800382 registerContentObservers();
383
Joe Onorato7c312c12009-08-13 21:36:53 -0700384 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 mSavedState = savedInstanceState;
387 restoreState(mSavedState);
388
Winson Chunga12a2502010-12-20 14:41:35 -0800389 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700390 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200391 mAppsCustomizeContent.onPackagesUpdated(
392 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700393 }
Winson Chunga12a2502010-12-20 14:41:35 -0800394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 if (PROFILE_STARTUP) {
396 android.os.Debug.stopMethodTracing();
397 }
398
399 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700400 if (sPausedFromUserAction) {
401 // If the user leaves launcher, then we should just load items asynchronously when
402 // they return.
403 mModel.startLoader(true, -1);
404 } else {
405 // We only load the page synchronously if the user rotates (or triggers a
406 // configuration change) while launcher is in the foreground
407 mModel.startLoader(true, mWorkspace.getCurrentPage());
408 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410
411 // For handling default keys
412 mDefaultKeySsb = new SpannableStringBuilder();
413 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800414
415 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
416 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800417
Adam Cohenf9426d52012-06-04 17:26:21 -0700418 updateGlobalIcons();
419
420 // On large interfaces, we want the screen to auto-rotate based on the current orientation
421 unlockScreenOrientation(true);
422 }
423
Winson Chung4a2afa32012-07-19 14:53:05 -0700424 protected void onUserLeaveHint() {
425 super.onUserLeaveHint();
426 sPausedFromUserAction = true;
427 }
428
Adam Cohenf9426d52012-06-04 17:26:21 -0700429 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700430 boolean searchVisible = false;
431 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800432 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700433 int coi = getCurrentOrientationIndexForGlobalIcons();
434 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
435 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700436 updateAppMarketIcon();
437 searchVisible = updateGlobalSearchIcon();
438 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700439 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700440 if (sGlobalSearchIcon[coi] != null) {
441 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700442 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700443 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700444 if (sVoiceSearchIcon[coi] != null) {
445 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700446 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700447 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700448 if (sAppMarketIcon[coi] != null) {
449 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800450 }
Winson Chungadf0c182012-08-23 14:59:07 -0700451 if (mSearchDropTargetBar != null) {
452 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
453 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
Romain Guycbb89e42009-06-08 15:52:54 -0700455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700457 if (sLocaleConfiguration == null) {
458 new AsyncTask<Void, Void, LocaleConfiguration>() {
459 @Override
460 protected LocaleConfiguration doInBackground(Void... unused) {
461 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
462 readConfiguration(Launcher.this, localeConfiguration);
463 return localeConfiguration;
464 }
465
466 @Override
467 protected void onPostExecute(LocaleConfiguration result) {
468 sLocaleConfiguration = result;
469 checkForLocaleChange(); // recursive, but now with a locale configuration
470 }
471 }.execute();
472 return;
473 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700474
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 final Configuration configuration = getResources().getConfiguration();
476
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700477 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 final String locale = configuration.locale.toString();
479
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700480 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 final int mcc = configuration.mcc;
482
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700483 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 final int mnc = configuration.mnc;
485
Romain Guy84f296c2009-11-04 15:00:44 -0800486 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487
Romain Guy84f296c2009-11-04 15:00:44 -0800488 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700489 sLocaleConfiguration.locale = locale;
490 sLocaleConfiguration.mcc = mcc;
491 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700492
Joe Onorato0589f0f2010-02-08 13:44:00 -0800493 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700494
495 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
496 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700497 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700498 public void run() {
499 writeConfiguration(Launcher.this, localeConfiguration);
500 }
501 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700502 }
503 }
504
505 private static class LocaleConfiguration {
506 public String locale;
507 public int mcc = -1;
508 public int mnc = -1;
509 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700510
Romain Guy98d01652009-06-30 16:21:04 -0700511 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
512 DataInputStream in = null;
513 try {
514 in = new DataInputStream(context.openFileInput(PREFERENCES));
515 configuration.locale = in.readUTF();
516 configuration.mcc = in.readInt();
517 configuration.mnc = in.readInt();
518 } catch (FileNotFoundException e) {
519 // Ignore
520 } catch (IOException e) {
521 // Ignore
522 } finally {
523 if (in != null) {
524 try {
525 in.close();
526 } catch (IOException e) {
527 // Ignore
528 }
529 }
530 }
531 }
532
533 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
534 DataOutputStream out = null;
535 try {
536 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
537 out.writeUTF(configuration.locale);
538 out.writeInt(configuration.mcc);
539 out.writeInt(configuration.mnc);
540 out.flush();
541 } catch (FileNotFoundException e) {
542 // Ignore
543 } catch (IOException e) {
544 //noinspection ResultOfMethodCallIgnored
545 context.getFileStreamPath(PREFERENCES).delete();
546 } finally {
547 if (out != null) {
548 try {
549 out.close();
550 } catch (IOException e) {
551 // Ignore
552 }
553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 }
555 }
556
Bjorn Bringertc459e522013-06-07 19:36:01 +0100557 public LayoutInflater getInflater() {
558 return mInflater;
559 }
560
Adam Cohen716b51e2011-06-30 12:09:54 -0700561 public DragLayer getDragLayer() {
562 return mDragLayer;
563 }
564
Winson Chung36a62fe2012-05-06 18:04:42 -0700565 boolean isDraggingEnabled() {
566 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
567 // that is subsequently removed from the workspace in startBinding().
568 return !mModel.isLoadingWorkspace();
569 }
570
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 static int getScreen() {
572 synchronized (sLock) {
573 return sScreen;
574 }
575 }
576
577 static void setScreen(int screen) {
578 synchronized (sLock) {
579 sScreen = screen;
580 }
581 }
582
Winson Chung557d6ed2011-07-08 15:34:52 -0700583 /**
584 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
585 * a configuration step, this allows the proper animations to run after other transitions.
586 */
587 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700588 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800589 switch (args.requestCode) {
590 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700591 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700592 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800593 break;
594 case REQUEST_PICK_SHORTCUT:
595 processShortcut(args.intent);
596 break;
597 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700598 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700599 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700600 result = true;
601 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800602 case REQUEST_CREATE_APPWIDGET:
603 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700604 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700605 result = true;
606 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800607 case REQUEST_PICK_WALLPAPER:
608 // We just wanted the activity result here so we can clear mWaitingForResult
609 break;
610 }
Michael Jurka27614d22012-04-02 06:40:22 -0700611 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
612 // if you turned the screen off and then back while in All Apps, Launcher would not
613 // return to the workspace. Clearing mAddInfo.container here fixes this issue
614 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700615 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800616 }
617
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700619 protected void onActivityResult(
620 final int requestCode, final int resultCode, final Intent data) {
621 if (requestCode == REQUEST_BIND_APPWIDGET) {
622 int appWidgetId = data != null ?
623 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
624 if (resultCode == RESULT_CANCELED) {
625 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
626 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700627 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700628 }
629 return;
630 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700631 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800632 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
633 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700634 mWaitingForResult = false;
635
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 // We have special handling for widgets
637 if (isWidgetDrop) {
638 int appWidgetId = data != null ?
639 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700640 if (appWidgetId < 0) {
641 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
642 "widget configuration activity.");
643 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
644 } else {
645 completeTwoStageWidgetDrop(resultCode, appWidgetId);
646 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800647 return;
648 }
649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 // The pattern used here is that a user PICKs a specific application,
651 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
654 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700655 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800656 final PendingAddArguments args = new PendingAddArguments();
657 args.requestCode = requestCode;
658 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700659 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700660 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700661 args.cellX = mPendingAddInfo.cellX;
662 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 if (isWorkspaceLocked()) {
664 sPendingAddList.add(args);
665 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700666 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800669 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700670 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
672 null);
673 }
674
675 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700676 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700677 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800678 Runnable onCompleteRunnable = null;
679 int animationType = 0;
680
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700681 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 if (resultCode == RESULT_OK) {
683 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
684 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700685 mPendingAddWidgetInfo);
686 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 onCompleteRunnable = new Runnable() {
688 @Override
689 public void run() {
690 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
693 null);
694 }
695 };
696 } else if (resultCode == RESULT_CANCELED) {
697 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
698 onCompleteRunnable = new Runnable() {
699 @Override
700 public void run() {
701 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
702 null);
703 }
704 };
705 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700706 if (mDragLayer.getAnimatedView() != null) {
707 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
708 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
709 animationType, boundWidget, true);
710 } else {
711 // The animated view may be null in the case of a rotation during widget configuration
712 onCompleteRunnable.run();
713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 }
715
716 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700717 protected void onStop() {
718 super.onStop();
719 FirstFrameAnimatorHelper.setIsVisible(false);
720 }
721
722 @Override
723 protected void onStart() {
724 super.onStart();
725 FirstFrameAnimatorHelper.setIsVisible(true);
726 }
727
728 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200730 long startTime = 0;
731 if (DEBUG_RESUME_TIME) {
732 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400733 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200734 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700736
Winson Chung4a2afa32012-07-19 14:53:05 -0700737 // Restore the previous launcher state
738 if (mOnResumeState == State.WORKSPACE) {
739 showWorkspace(false);
740 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
741 showAllApps(false);
742 }
743 mOnResumeState = State.NONE;
744
Craig Mautner360310b2012-10-26 15:13:08 -0700745 // Background was set to gradient in onPause(), restore to black if in all apps.
746 setWorkspaceBackground(mState == State.WORKSPACE);
747
Winson Chungde0fb8f2012-05-08 14:37:08 -0700748 // Process any items that were added while Launcher was away
749 InstallShortcutReceiver.flushInstallQueue(this);
750
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800751 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700752 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700753 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700755 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400756 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700757 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200759 if (mOnResumeCallbacks.size() > 0) {
760 // We might have postponed some bind calls until onResume (see waitUntilResume) --
761 // execute them here
762 long startTimeCallbacks = 0;
763 if (DEBUG_RESUME_TIME) {
764 startTimeCallbacks = System.currentTimeMillis();
765 }
766
767 if (mAppsCustomizeContent != null) {
768 mAppsCustomizeContent.setBulkBind(true);
769 }
770 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
771 mOnResumeCallbacks.get(i).run();
772 }
773 if (mAppsCustomizeContent != null) {
774 mAppsCustomizeContent.setBulkBind(false);
775 }
776 mOnResumeCallbacks.clear();
777 if (DEBUG_RESUME_TIME) {
778 Log.d(TAG, "Time spent processing callbacks in onResume: " +
779 (System.currentTimeMillis() - startTimeCallbacks));
780 }
Michael Jurka447bf842013-05-15 14:52:15 +0200781 }
Winson Chunge4e50662012-01-23 14:45:13 -0800782
783 // Reset the pressed state of icons that were locked in the press state while activities
784 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800785 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800786 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800787 mWaitingForResume.setStayPressed(false);
788 }
Winson Chunge4e50662012-01-23 14:45:13 -0800789 if (mAppsCustomizeContent != null) {
790 // Resets the previous all apps icon press state
791 mAppsCustomizeContent.resetDrawableState();
792 }
Adam Cohen06dff352012-06-01 17:17:08 -0700793 // It is possible that widgets can receive updates while launcher is not in the foreground.
794 // Consequently, the widgets will be inflated in the orientation of the foreground activity
795 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
796 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700797 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700798
799 // Again, as with the above scenario, it's possible that one or more of the global icons
800 // were updated in the wrong orientation.
801 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200802 if (DEBUG_RESUME_TIME) {
803 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
804 }
Adam Cohen06dff352012-06-01 17:17:08 -0700805 }
806
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700808 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700809 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
810 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
811 // when Launcher resumes and we are still in AllApps.
812 updateWallpaperVisibility(true);
813
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700814 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700815 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800816 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700817 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700818 }
Romain Guycbb89e42009-06-08 15:52:54 -0700819
Adam Cohen66a01fd2013-06-11 12:48:00 -0700820 protected void onFinishBindingItems() {
821 }
822
823 // Add a fullscreen unpadded view to the workspace to the left all other screens.
824 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700825 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700826 }
827
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700828 @Override
829 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400830 // Flag the loader to stop early before switching
831 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700832 if (mAppsCustomizeContent != null) {
833 mAppsCustomizeContent.surrender();
834 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800835 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700836 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700837
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800838 // We can't hide the IME if it was forced open. So don't bother
839 /*
840 @Override
841 public void onWindowFocusChanged(boolean hasFocus) {
842 super.onWindowFocusChanged(hasFocus);
843
844 if (hasFocus) {
845 final InputMethodManager inputManager = (InputMethodManager)
846 getSystemService(Context.INPUT_METHOD_SERVICE);
847 WindowManager.LayoutParams lp = getWindow().getAttributes();
848 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
849 android.os.Handler()) {
850 protected void onReceiveResult(int resultCode, Bundle resultData) {
851 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
852 }
853 });
854 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
855 }
856 }
857 */
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 private boolean acceptFilter() {
860 final InputMethodManager inputManager = (InputMethodManager)
861 getSystemService(Context.INPUT_METHOD_SERVICE);
862 return !inputManager.isFullscreenMode();
863 }
864
865 @Override
866 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700867 final int uniChar = event.getUnicodeChar();
868 final boolean handled = super.onKeyDown(keyCode, event);
869 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
870 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
872 keyCode, event);
873 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700874 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700875 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700876 // showSearchDialog()
877 // If there are multiple keystrokes before the search dialog takes focus,
878 // onSearchRequested() will be called for every keystroke,
879 // but it is idempotent, so it's fine.
880 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 }
882 }
883
Joe Onorato8a9625e2010-01-28 15:55:35 -0800884 // Eat the long press event so the keyboard doesn't come up.
885 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
886 return true;
887 }
888
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 return handled;
890 }
891
Karl Rosaen138a0412009-04-23 19:00:21 -0700892 private String getTypedText() {
893 return mDefaultKeySsb.toString();
894 }
895
896 private void clearTypedText() {
897 mDefaultKeySsb.clear();
898 mDefaultKeySsb.clearSpans();
899 Selection.setSelection(mDefaultKeySsb, 0);
900 }
901
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700903 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
904 * State
905 */
906 private static State intToState(int stateOrdinal) {
907 State state = State.WORKSPACE;
908 final State[] stateValues = State.values();
909 for (int i = 0; i < stateValues.length; i++) {
910 if (stateValues[i].ordinal() == stateOrdinal) {
911 state = stateValues[i];
912 break;
913 }
914 }
915 return state;
916 }
917
918 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 * Restores the previous state, if it exists.
920 *
921 * @param savedState The previous state.
922 */
923 private void restoreState(Bundle savedState) {
924 if (savedState == null) {
925 return;
926 }
927
Michael Jurka883f55b2010-10-21 15:47:14 -0700928 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700929 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700930 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800931 }
932
Winson Chungf0c6ae02012-03-21 16:10:31 -0700933 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700935 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700940
Winson Chung3d503fb2011-07-13 17:25:49 -0700941 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
942 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700944 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
945 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700946 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
947 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
948 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700949 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 mRestoring = true;
951 }
952
953 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
954 if (renameFolder) {
955 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700956 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 mRestoring = true;
958 }
Winson Chunga12a2502010-12-20 14:41:35 -0800959
Winson Chung785d2eb2011-04-14 16:08:02 -0700960 // Restore the AppsCustomize tab
961 if (mAppsCustomizeTabHost != null) {
962 String curTab = savedState.getString("apps_customize_currentTab");
963 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700964 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700965 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700966 mAppsCustomizeContent.loadAssociatedPages(
967 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700968 }
969
Winson Chung5afbf7b2011-07-25 11:53:08 -0700970 int currentIndex = savedState.getInt("apps_customize_currentIndex");
971 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 }
974
975 /**
976 * Finds all the views we need and configure them properly.
977 */
978 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700979 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400980
Craig Mautner360310b2012-10-26 15:13:08 -0700981 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700982 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
983 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700984 mQsbDivider = findViewById(R.id.qsb_divider);
985 mDockDivider = findViewById(R.id.dock_divider);
986
987 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
988 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989
Winson Chung4c98d922011-05-31 16:50:48 -0700990 // Setup the drag layer
991 mDragLayer.setup(this, dragController);
992
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 // Setup the hotseat
994 mHotseat = (Hotseat) findViewById(R.id.hotseat);
995 if (mHotseat != null) {
996 mHotseat.setup(this);
997 }
998
Winson Chung4c98d922011-05-31 16:50:48 -0700999 // Setup the workspace
1000 mWorkspace.setHapticFeedbackEnabled(false);
1001 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001002 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001003 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001004
Winson Chungf0ea4d32011-06-06 14:27:16 -07001005 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001006 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001007
Winson Chungf0ea4d32011-06-06 14:27:16 -07001008 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001009 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001010 mAppsCustomizeContent = (AppsCustomizePagedView)
1011 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1012 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001013
Winson Chung3d503fb2011-07-13 17:25:49 -07001014 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001015 dragController.setDragScoller(mWorkspace);
1016 dragController.setScrollView(mDragLayer);
1017 dragController.setMoveTarget(mWorkspace);
1018 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001019 if (mSearchDropTargetBar != null) {
1020 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001021 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001022
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001023 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001024 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001025 final View ww = new WeightWatcher(this);
1026 ww.setAlpha(0.5f);
1027 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001028 new FrameLayout.LayoutParams(
1029 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001030 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001031 Gravity.BOTTOM)
1032 );
1033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 }
1035
1036 /**
1037 * Creates a view representing a shortcut.
1038 *
1039 * @param info The data structure describing the shortcut.
1040 *
1041 * @return A View inflated from R.layout.application.
1042 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001043 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001045 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 }
1047
1048 /**
1049 * Creates a view representing a shortcut inflated from the specified resource.
1050 *
1051 * @param layoutResId The id of the XML layout used to create the shortcut.
1052 * @param parent The group the shortcut belongs to.
1053 * @param info The data structure describing the shortcut.
1054 *
1055 * @return A View inflated from layoutResId.
1056 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001057 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001058 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1059 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 return favorite;
1062 }
1063
1064 /**
1065 * Add an application shortcut to the workspace.
1066 *
1067 * @param data The intent describing the application.
1068 * @param cellInfo The position on screen where to create the shortcut.
1069 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001070 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001071 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001072 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073
Adam Cohenfbba09b2011-07-18 21:43:05 -07001074 // First we check if we already know the exact location where we want to add this item.
1075 if (cellX >= 0 && cellY >= 0) {
1076 cellXY[0] = cellX;
1077 cellXY[1] = cellY;
1078 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001079 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001080 return;
1081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001083 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001084
Romain Guy73b979d2009-06-09 12:57:21 -07001085 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001086 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001088 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001089 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001090 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001091 } else {
1092 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
1094 }
Romain Guycbb89e42009-06-08 15:52:54 -07001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 /**
1097 * Add a shortcut to the workspace.
1098 *
1099 * @param data The intent describing the shortcut.
1100 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001102 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001103 int cellY) {
1104 int[] cellXY = mTmpAddItemCellCoordinates;
1105 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001106 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001107
Michael Jurkad3ef3062010-11-23 16:23:58 -08001108 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001109
Adam Cohen558baaf2011-08-15 15:22:57 -07001110 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001111 if (info == null) {
1112 return;
1113 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001114 final View view = createShortcut(info);
1115
Adam Cohenfbba09b2011-07-18 21:43:05 -07001116 // First we check if we already know the exact location where we want to add this item.
1117 if (cellX >= 0 && cellY >= 0) {
1118 cellXY[0] = cellX;
1119 cellXY[1] = cellY;
1120 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001121
1122 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001123 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001124 true, null,null)) {
1125 return;
1126 }
1127 DragObject dragObject = new DragObject();
1128 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001129 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1130 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001131 return;
1132 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001133 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001134 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001135 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001136 foundCellSpan = (result != null);
1137 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001138 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001139 }
1140
1141 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001142 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001143 return;
1144 }
1145
Adam Cohendcd297f2013-06-18 13:13:40 -07001146 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147
1148 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001149 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001150 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
1152 }
1153
Adam Cohen2f093b62012-04-30 18:59:53 -07001154 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1155 int minHeight) {
1156 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001157 // We want to account for the extra amount of padding that we are adding to the widget
1158 // to ensure that it gets the full amount of space that it has requested
1159 int requiredWidth = minWidth + padding.left + padding.right;
1160 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001161 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001162 }
1163
Adam Cohen2f093b62012-04-30 18:59:53 -07001164 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1165 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001166 }
1167
Adam Cohen2f093b62012-04-30 18:59:53 -07001168 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1169 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001170 }
1171
Adam Cohen2f093b62012-04-30 18:59:53 -07001172 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1173 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001174 }
1175
Adam Cohen2f093b62012-04-30 18:59:53 -07001176 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1177 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001178 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001179 }
1180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001182 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001184 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001185 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001187 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001188 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1189 if (appWidgetInfo == null) {
1190 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1191 }
Romain Guycbb89e42009-06-08 15:52:54 -07001192
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001194 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001195
Adam Cohen2f093b62012-04-30 18:59:53 -07001196 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1197 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001200 // We have saved the position to which the widget was dragged-- this really only matters
1201 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001202 int[] cellXY = mTmpAddItemCellCoordinates;
1203 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001204 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001205 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001206 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1207 cellXY[0] = mPendingAddInfo.cellX;
1208 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001209 spanXY[0] = mPendingAddInfo.spanX;
1210 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001211 foundCellSpan = true;
1212 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001213 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001214 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001215 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1216 spanXY[1], cellXY, finalSpan);
1217 spanXY[0] = finalSpan[0];
1218 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001219 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001221 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001222 }
1223
Michael Jurka0280c3b2010-09-17 15:00:07 -07001224 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001225 if (appWidgetId != -1) {
1226 // Deleting an app widget ID is a void call but writes to disk before returning
1227 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001228 new Thread("deleteAppWidgetId") {
1229 public void run() {
1230 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1231 }
1232 }.start();
1233 }
Winson Chung93eef082012-03-23 15:59:27 -07001234 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001235 return;
1236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001238 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001239 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1240 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001241 launcherInfo.spanX = spanXY[0];
1242 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001243 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1244 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001247 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248
1249 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001250 if (hostView == null) {
1251 // Perform actual inflation because we're live
1252 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1253 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1254 } else {
1255 // The AppWidgetHostView has already been inflated and instantiated
1256 launcherInfo.hostView = hostView;
1257 }
Romain Guycbb89e42009-06-08 15:52:54 -07001258
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001260 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001261 launcherInfo.notifyWidgetSizeChanged(this);
1262
Adam Cohendcd297f2013-06-18 13:13:40 -07001263 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001264 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001265
1266 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001268 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
Romain Guycbb89e42009-06-08 15:52:54 -07001270
Adam Cohended9f8d2010-11-03 13:25:16 -07001271 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1272 @Override
1273 public void onReceive(Context context, Intent intent) {
1274 final String action = intent.getAction();
1275 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1276 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001277 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001278 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001279
Winson Chungc100e8e2011-08-09 16:02:43 -07001280 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001281 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001282 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1283 mAppsCustomizeTabHost.reset();
1284 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001285 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001286 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1287 mUserPresent = true;
1288 updateRunning();
1289 }
1290 }
1291 };
1292
1293 @Override
1294 public void onAttachedToWindow() {
1295 super.onAttachedToWindow();
1296
1297 // Listen for broadcasts related to user-presence
1298 final IntentFilter filter = new IntentFilter();
1299 filter.addAction(Intent.ACTION_SCREEN_OFF);
1300 filter.addAction(Intent.ACTION_USER_PRESENT);
1301 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001302 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001303 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001304 mVisible = true;
1305 }
1306
1307 @Override
1308 public void onDetachedFromWindow() {
1309 super.onDetachedFromWindow();
1310 mVisible = false;
1311
Adam Cohend113e0c2010-11-11 10:48:05 -08001312 if (mAttached) {
1313 unregisterReceiver(mReceiver);
1314 mAttached = false;
1315 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001316 updateRunning();
1317 }
1318
1319 public void onWindowVisibilityChanged(int visibility) {
1320 mVisible = visibility == View.VISIBLE;
1321 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001322 // The following code used to be in onResume, but it turns out onResume is called when
1323 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1324 // is a more appropriate event to handle
1325 if (mVisible) {
1326 mAppsCustomizeTabHost.onWindowVisible();
1327 if (!mWorkspaceLoading) {
1328 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001329 // We want to let Launcher draw itself at least once before we force it to build
1330 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001331 // apps is nice and speedy.
1332 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001333 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001334 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001335 if (mStarted) return;
1336 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001337 // We delay the layer building a bit in order to give
1338 // other message processing a time to run. In particular
1339 // this avoids a delay in hiding the IME if it was
1340 // currently shown, because doing that may involve
1341 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001342 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001343 final ViewTreeObserver.OnDrawListener listener = this;
1344 mWorkspace.post(new Runnable() {
1345 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001346 if (mWorkspace != null &&
1347 mWorkspace.getViewTreeObserver() != null) {
1348 mWorkspace.getViewTreeObserver().
1349 removeOnDrawListener(listener);
1350 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001351 }
1352 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001353 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001354 }
1355 });
1356 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001357 // When Launcher comes back to foreground, a different Activity might be responsible for
1358 // the app market intent, so refresh the icon
1359 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001360 clearTypedText();
1361 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001362 }
1363
1364 private void sendAdvanceMessage(long delay) {
1365 mHandler.removeMessages(ADVANCE_MSG);
1366 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1367 mHandler.sendMessageDelayed(msg, delay);
1368 mAutoAdvanceSentTime = System.currentTimeMillis();
1369 }
1370
1371 private void updateRunning() {
1372 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1373 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1374 mAutoAdvanceRunning = autoAdvanceRunning;
1375 if (autoAdvanceRunning) {
1376 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1377 sendAdvanceMessage(delay);
1378 } else {
1379 if (!mWidgetsToAdvance.isEmpty()) {
1380 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1381 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1382 }
1383 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001384 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001385 }
1386 }
1387 }
1388
1389 private final Handler mHandler = new Handler() {
1390 @Override
1391 public void handleMessage(Message msg) {
1392 if (msg.what == ADVANCE_MSG) {
1393 int i = 0;
1394 for (View key: mWidgetsToAdvance.keySet()) {
1395 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1396 final int delay = mAdvanceStagger * i;
1397 if (v instanceof Advanceable) {
1398 postDelayed(new Runnable() {
1399 public void run() {
1400 ((Advanceable) v).advance();
1401 }
1402 }, delay);
1403 }
1404 i++;
1405 }
1406 sendAdvanceMessage(mAdvanceInterval);
1407 }
1408 }
1409 };
1410
1411 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001412 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001413 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1414 if (v instanceof Advanceable) {
1415 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001416 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001417 updateRunning();
1418 }
1419 }
1420
1421 void removeWidgetToAutoAdvance(View hostView) {
1422 if (mWidgetsToAdvance.containsKey(hostView)) {
1423 mWidgetsToAdvance.remove(hostView);
1424 updateRunning();
1425 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001426 }
1427
Joe Onorato9c1289c2009-08-17 11:03:03 -04001428 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001429 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001430 launcherInfo.hostView = null;
1431 }
1432
Winson Chung93eef082012-03-23 15:59:27 -07001433 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1434 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1435 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001436 }
1437
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001438 public LauncherAppWidgetHost getAppWidgetHost() {
1439 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 }
Romain Guycbb89e42009-06-08 15:52:54 -07001441
Winson Chunga9abd0e2010-10-27 17:18:37 -07001442 public LauncherModel getModel() {
1443 return mModel;
1444 }
1445
Bjorn Bringertc459e522013-06-07 19:36:01 +01001446 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001447 getWindow().closeAllPanels();
1448
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001449 // Whatever we were doing is hereby canceled.
1450 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001451 }
1452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 @Override
1454 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001455 long startTime = 0;
1456 if (DEBUG_RESUME_TIME) {
1457 startTime = System.currentTimeMillis();
1458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 super.onNewIntent(intent);
1460
1461 // Close the menu
1462 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001463 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001464 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001465
Jamie Genniscb222e82012-10-23 15:44:26 -07001466 final boolean alreadyOnHome =
1467 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001468 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001469
Jamie Genniscb222e82012-10-23 15:44:26 -07001470 Runnable processIntent = new Runnable() {
1471 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001472 if (mWorkspace == null) {
1473 // Can be cases where mWorkspace is null, this prevents a NPE
1474 return;
1475 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001476 Folder openFolder = mWorkspace.getOpenFolder();
1477 // In all these cases, only animate if we're already on home
1478 mWorkspace.exitWidgetResizeMode();
1479 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1480 openFolder == null) {
1481 mWorkspace.moveToDefaultScreen(true);
1482 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001483
Jamie Genniscb222e82012-10-23 15:44:26 -07001484 closeFolder();
1485 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001486
Jamie Genniscb222e82012-10-23 15:44:26 -07001487 // If we are already on home, then just animate back to the workspace,
1488 // otherwise, just wait until onResume to set the state back to Workspace
1489 if (alreadyOnHome) {
1490 showWorkspace(true);
1491 } else {
1492 mOnResumeState = State.WORKSPACE;
1493 }
1494
1495 final View v = getWindow().peekDecorView();
1496 if (v != null && v.getWindowToken() != null) {
1497 InputMethodManager imm = (InputMethodManager)getSystemService(
1498 INPUT_METHOD_SERVICE);
1499 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1500 }
1501
1502 // Reset AllApps to its initial state
1503 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1504 mAppsCustomizeTabHost.reset();
1505 }
1506 }
1507 };
1508
1509 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1510 // Delay processing of the intent to allow the status bar animation to finish
1511 // first in order to avoid janky animations.
1512 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001513 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001514 // Process the intent immediately.
1515 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001516 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 }
Michael Jurka447bf842013-05-15 14:52:15 +02001519 if (DEBUG_RESUME_TIME) {
1520 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1521 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 }
1523
1524 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001525 public void onRestoreInstanceState(Bundle state) {
1526 super.onRestoreInstanceState(state);
1527 for (int page: mSynchronouslyBoundPages) {
1528 mWorkspace.restoreInstanceStateForChild(page);
1529 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
1531
1532 @Override
1533 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001534 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001535 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536
Michael Jurka883f55b2010-10-21 15:47:14 -07001537 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001538 // We close any open folder since it will not be re-opened, and we need to make sure
1539 // this state is reflected.
1540 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541
Adam Cohendcd297f2013-06-18 13:13:40 -07001542 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001543 mWaitingForResult) {
1544 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001546 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1547 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001548 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1549 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1550 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 }
1552
1553 if (mFolderInfo != null && mWaitingForResult) {
1554 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1555 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1556 }
Michael Jurka946ad472010-07-09 18:05:18 -07001557
Winson Chung785d2eb2011-04-14 16:08:02 -07001558 // Save the current AppsCustomize tab
1559 if (mAppsCustomizeTabHost != null) {
1560 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1561 if (currentTabTag != null) {
1562 outState.putString("apps_customize_currentTab", currentTabTag);
1563 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001564 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1565 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
1568
1569 @Override
1570 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001572
Winson Chunge7a03942011-08-05 15:05:12 -07001573 // Remove all pending runnables
1574 mHandler.removeMessages(ADVANCE_MSG);
1575 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001576 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001577
Winson Chungcd2b0142011-06-08 16:02:26 -07001578 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001579 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001580 mModel.stopLoader();
1581 app.setLauncher(null);
1582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001584 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001586 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001588 mAppWidgetHost = null;
1589
1590 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591
1592 TextKeyListener.getInstance().release();
1593
Winson Chung81b52252012-08-27 15:34:29 -07001594 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1595 // to prevent leaking Launcher activities on orientation change.
1596 if (mModel != null) {
1597 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1598 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001599
1600 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001601 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001602
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001603 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001604 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1605 mWorkspace.removeAllViews();
1606 mWorkspace = null;
1607 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001608
Michael Jurka2ecf9952012-06-18 12:52:28 -07001609 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 }
1611
Adam Cohena9cf38f2011-05-02 15:36:58 -07001612 public DragController getDragController() {
1613 return mDragController;
1614 }
1615
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 @Override
1617 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001618 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 super.startActivityForResult(intent, requestCode);
1620 }
1621
Winson Chung70d72102011-08-12 11:24:35 -07001622 /**
1623 * Indicates that we want global search for this activity by setting the globalSearch
1624 * argument for {@link #startSearch} to true.
1625 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001627 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001629
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001630 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001631
Karl Rosaen138a0412009-04-23 19:00:21 -07001632 if (initialQuery == null) {
1633 // Use any text typed in the launcher as the initial query
1634 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001635 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 if (appSearchData == null) {
1637 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001638 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 }
Winson Chungadf0c182012-08-23 14:59:07 -07001640 Rect sourceBounds = new Rect();
1641 if (mSearchDropTargetBar != null) {
1642 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1643 }
Romain Guycbb89e42009-06-08 15:52:54 -07001644
Bjorn Bringertc459e522013-06-07 19:36:01 +01001645 startSearch(initialQuery, selectInitialQuery,
1646 appSearchData, sourceBounds);
1647 }
1648
1649 public void startSearch(String initialQuery,
1650 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001651 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001652 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001653 }
1654
1655 /**
1656 * Starts the global search activity. This code is a copied from SearchManager
1657 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001658 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001659 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001660 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001661 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1662 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1663 if (globalSearchActivity == null) {
1664 Log.w(TAG, "No global search activity found.");
1665 return;
1666 }
1667 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1668 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1669 intent.setComponent(globalSearchActivity);
1670 // Make sure that we have a Bundle to put source in
1671 if (appSearchData == null) {
1672 appSearchData = new Bundle();
1673 } else {
1674 appSearchData = new Bundle(appSearchData);
1675 }
1676 // Set source to package name of app that starts global search, if not set already.
1677 if (!appSearchData.containsKey("source")) {
1678 appSearchData.putString("source", getPackageName());
1679 }
1680 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1681 if (!TextUtils.isEmpty(initialQuery)) {
1682 intent.putExtra(SearchManager.QUERY, initialQuery);
1683 }
1684 if (selectInitialQuery) {
1685 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1686 }
1687 intent.setSourceBounds(sourceBounds);
1688 try {
1689 startActivity(intent);
1690 } catch (ActivityNotFoundException ex) {
1691 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 }
1694
Winson Chung70d72102011-08-12 11:24:35 -07001695 @Override
1696 public boolean onCreateOptionsMenu(Menu menu) {
1697 if (isWorkspaceLocked()) {
1698 return false;
1699 }
1700
1701 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001702
1703 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1704 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1705 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001706 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1707 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1708 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001709 String helpUrl = getString(R.string.help_url);
1710 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001711 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1712 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1713
Winson Chung70d72102011-08-12 11:24:35 -07001714 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1715 .setIcon(android.R.drawable.ic_menu_gallery)
1716 .setAlphabeticShortcut('W');
1717 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1718 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001719 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001720 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001721 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1722 .setIcon(android.R.drawable.ic_menu_preferences)
1723 .setIntent(settings)
1724 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001725 if (!helpUrl.isEmpty()) {
1726 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1727 .setIcon(android.R.drawable.ic_menu_help)
1728 .setIntent(help)
1729 .setAlphabeticShortcut('H');
1730 }
Winson Chung70d72102011-08-12 11:24:35 -07001731 return true;
1732 }
1733
1734 @Override
1735 public boolean onPrepareOptionsMenu(Menu menu) {
1736 super.onPrepareOptionsMenu(menu);
1737
1738 if (mAppsCustomizeTabHost.isTransitioning()) {
1739 return false;
1740 }
1741 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1742 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1743
1744 return true;
1745 }
1746
1747 @Override
1748 public boolean onOptionsItemSelected(MenuItem item) {
1749 switch (item.getItemId()) {
1750 case MENU_WALLPAPER_SETTINGS:
1751 startWallpaper();
1752 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001753 }
1754
1755 return super.onOptionsItemSelected(item);
1756 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001758 @Override
1759 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001760 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001761 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001762 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001763 }
1764
Joe Onorato9c1289c2009-08-17 11:03:03 -04001765 public boolean isWorkspaceLocked() {
1766 return mWorkspaceLoading || mWaitingForResult;
1767 }
1768
Michael Jurka0280c3b2010-09-17 15:00:07 -07001769 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001770 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001771 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001772 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1773 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001774 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001775 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001777
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001778 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1779 AppWidgetProviderInfo appWidgetInfo) {
1780 if (appWidgetInfo.configure != null) {
1781 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001782
Bjorn Bringert7984c942009-12-09 15:38:25 +00001783 // Launch over to configure widget, if needed
1784 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001785 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001786 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001787 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001789 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001790 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001791 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001792 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001793 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795 }
Romain Guycbb89e42009-06-08 15:52:54 -07001796
Adam Cohenfbba09b2011-07-18 21:43:05 -07001797 /**
1798 * Process a shortcut drop.
1799 *
1800 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001801 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001802 * @param cell The cell it should be added to, optional
1803 * @param position The location on the screen where it was dropped, optional
1804 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001805 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001806 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001807 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001808 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001809 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001810 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001811
1812 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001813 mPendingAddInfo.cellX = cell[0];
1814 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001815 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001816
1817 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1818 createShortcutIntent.setComponent(componentName);
1819 processShortcut(createShortcutIntent);
1820 }
1821
Adam Cohenfbba09b2011-07-18 21:43:05 -07001822 /**
1823 * Process a widget drop.
1824 *
1825 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001826 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001827 * @param cell The cell it should be added to, optional
1828 * @param position The location on the screen where it was dropped, optional
1829 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001830 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001831 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001832 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001833 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001834 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001835 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001836 mPendingAddInfo.minSpanX = info.minSpanX;
1837 mPendingAddInfo.minSpanY = info.minSpanY;
1838
Adam Cohenfbba09b2011-07-18 21:43:05 -07001839 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001840 mPendingAddInfo.cellX = cell[0];
1841 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001842 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001843 if (span != null) {
1844 mPendingAddInfo.spanX = span[0];
1845 mPendingAddInfo.spanY = span[1];
1846 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001847
Adam Cohened66b2b2012-01-23 17:28:51 -08001848 AppWidgetHostView hostView = info.boundWidget;
1849 int appWidgetId;
1850 if (hostView != null) {
1851 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001852 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001853 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001854 // In this case, we either need to start an activity to get permission to bind
1855 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001856 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001857 Bundle options = info.bindOptions;
1858
1859 boolean success = false;
1860 if (options != null) {
1861 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1862 info.componentName, options);
1863 } else {
1864 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1865 info.componentName);
1866 }
1867 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001868 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001869 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001870 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001871 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1872 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1873 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001874 // TODO: we need to make sure that this accounts for the options bundle.
1875 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001876 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1877 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001878 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001879 }
1880
Joe Onoratodeb98af2010-02-19 14:59:39 -08001881 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001882 // Handle case where user selected "Applications"
1883 String applicationName = getResources().getString(R.string.group_applications);
1884 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001885
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001886 if (applicationName != null && applicationName.equals(shortcutName)) {
1887 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1888 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001889
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001890 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1891 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001892 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001893 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001894 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001895 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 }
1898
Winson Chung24ab2f12010-09-16 14:10:47 -07001899 void processWallpaper(Intent intent) {
1900 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1901 }
1902
Adam Cohendcd297f2013-06-18 13:13:40 -07001903 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001904 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001905 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 folderInfo.title = getText(R.string.folder_name);
1907
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001909 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001910 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001911 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912
1913 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001914 FolderIcon newFolder =
1915 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001916 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001917 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001918 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
Romain Guycbb89e42009-06-08 15:52:54 -07001920
Joe Onorato9c1289c2009-08-17 11:03:03 -04001921 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001922 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001923 }
1924
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001926 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001928 Intent chooser = Intent.createChooser(pickWallpaper,
1929 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001930 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1931 // Removed in Eclair MR1
1932// WallpaperManager wm = (WallpaperManager)
1933// getSystemService(Context.WALLPAPER_SERVICE);
1934// WallpaperInfo wi = wm.getWallpaperInfo();
1935// if (wi != null && wi.getSettingsActivity() != null) {
1936// LabeledIntent li = new LabeledIntent(getPackageName(),
1937// R.string.configure_wallpaper, 0);
1938// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1939// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1940// }
Mike Clerona0618e42009-10-22 13:55:21 -07001941 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
1943
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001944 /**
1945 * Registers various content observers. The current implementation registers
1946 * only a favorites observer to keep track of the favorites applications.
1947 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001948 private void registerContentObservers() {
1949 ContentResolver resolver = getContentResolver();
1950 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1951 true, mWidgetObserver);
1952 }
1953
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 @Override
1955 public boolean dispatchKeyEvent(KeyEvent event) {
1956 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1957 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001959 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001960 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001961 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001962 dumpState();
1963 return true;
1964 }
1965 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001966 }
1967 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1968 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001969 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 return true;
1971 }
1972 }
1973
1974 return super.dispatchKeyEvent(event);
1975 }
1976
Joe Onorato88ec0992009-11-19 13:16:06 -08001977 @Override
1978 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001979 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001980 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001981 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001982 Folder openFolder = mWorkspace.getOpenFolder();
1983 if (openFolder.isEditingName()) {
1984 openFolder.dismissEditingName();
1985 } else {
1986 closeFolder();
1987 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001988 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001989 mWorkspace.exitWidgetResizeMode();
1990
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001991 // Back button is a no-op here, but give at least some feedback for the button press
1992 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001993 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001994 }
1995
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001997 * Re-listen when widgets are reset.
1998 */
1999 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002000 if (mAppWidgetHost != null) {
2001 mAppWidgetHost.startListening();
2002 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002003 }
2004
2005 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 * Launches the intent referred by the clicked shortcut.
2007 *
2008 * @param v The view representing the clicked shortcut.
2009 */
2010 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002011 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2012 // view has detached (it's possible for this to happen if the view is removed mid touch).
2013 if (v.getWindowToken() == null) {
2014 return;
2015 }
2016
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002017 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002018 return;
2019 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002022 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002024 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002025
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002026 // Check for special shortcuts
2027 if (intent.getComponent() != null) {
2028 final String shortcutClass = intent.getComponent().getClassName();
2029
2030 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2031 showAllApps(true);
2032 return;
2033 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2034 MemoryDumpActivity.startDump(this);
2035 return;
2036 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002037 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002038
2039 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002040 int[] pos = new int[2];
2041 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002042 intent.setSourceBounds(new Rect(pos[0], pos[1],
2043 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002044
2045 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002046
2047 if (success && v instanceof BubbleTextView) {
2048 mWaitingForResume = (BubbleTextView) v;
2049 mWaitingForResume.setStayPressed(true);
2050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002052 if (v instanceof FolderIcon) {
2053 FolderIcon fi = (FolderIcon) v;
2054 handleFolderClick(fi);
2055 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002056 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002057 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002058 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002059 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002060 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002061 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063 }
2064
Michael Jurka0e260592010-06-30 17:07:39 -07002065 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002066 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002067 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002068 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2069 showWorkspace(true);
2070 }
Michael Jurka0e260592010-06-30 17:07:39 -07002071 return false;
2072 }
2073
Michael Jurkaaf442092010-06-10 17:01:57 -07002074 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002075 * Event handler for the search button
2076 *
2077 * @param v The view that was clicked.
2078 */
2079 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002080 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2081
Amith Yamasania135ba82011-08-09 17:42:01 -07002082 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002083 }
2084
2085 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002086 * Event handler for the voice button
2087 *
2088 * @param v The view that was clicked.
2089 */
2090 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002091 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002092
Bjorn Bringertc459e522013-06-07 19:36:01 +01002093 startVoice();
2094 }
2095
2096 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002097 try {
2098 final SearchManager searchManager =
2099 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2100 ComponentName activityName = searchManager.getGlobalSearchActivity();
2101 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002102 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002103 if (activityName != null) {
2104 intent.setPackage(activityName.getPackageName());
2105 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002106 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002107 } catch (ActivityNotFoundException e) {
2108 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002109 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002110 startActivitySafely(null, intent, "onClickVoiceButton");
2111 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002112 }
2113
2114 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002115 * Event handler for the "grid" button that appears on the home screen, which
2116 * enters all apps mode.
2117 *
2118 * @param v The view that was clicked.
2119 */
2120 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002121 showAllApps(true);
2122 }
2123
2124 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002125 // Provide the same haptic feedback that the system offers for virtual keys.
2126 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002127 }
2128
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002129 public void onClickAppMarketButton(View v) {
2130 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002131 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002132 } else {
2133 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002134 }
2135 }
2136
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002137 void startApplicationDetailsActivity(ComponentName componentName) {
2138 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002139 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2140 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002141 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002142 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002143 }
2144
Patrick Dubroy5539af72010-09-07 15:22:01 -07002145 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2146 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2147 // System applications cannot be installed. For now, show a toast explaining that.
2148 // We may give them the option of disabling apps this way.
2149 int messageId = R.string.uninstall_system_app_text;
2150 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2151 } else {
2152 String packageName = appInfo.componentName.getPackageName();
2153 String className = appInfo.componentName.getClassName();
2154 Intent intent = new Intent(
2155 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002156 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2157 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002158 startActivity(intent);
2159 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002160 }
2161
Michael Jurka86a720e2012-05-09 11:23:23 -07002162 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002166 // Only launch using the new animation if the shortcut has not opted out (this is a
2167 // private contract between launcher and may be ignored in the future).
2168 boolean useLaunchAnimation = (v != null) &&
2169 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2170 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002171 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2172 v.getMeasuredWidth(), v.getMeasuredHeight());
2173
2174 startActivity(intent, opts.toBundle());
2175 } else {
2176 startActivity(intent);
2177 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002178 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 } catch (SecurityException e) {
2180 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002181 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002183 "or use the exported attribute for this activity. "
2184 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002186 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002188
Michael Jurka86a720e2012-05-09 11:23:23 -07002189 boolean startActivitySafely(View v, Intent intent, Object tag) {
2190 boolean success = false;
2191 try {
2192 success = startActivity(v, intent, tag);
2193 } catch (ActivityNotFoundException e) {
2194 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2195 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2196 }
2197 return success;
2198 }
2199
Romain Guy8e633c52010-03-04 12:51:36 -08002200 void startActivityForResultSafely(Intent intent, int requestCode) {
2201 try {
2202 startActivityForResult(intent, requestCode);
2203 } catch (ActivityNotFoundException e) {
2204 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2205 } catch (SecurityException e) {
2206 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2207 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2208 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2209 "or use the exported attribute for this activity.", e);
2210 }
2211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212
Adam Cohena9cf38f2011-05-02 15:36:58 -07002213 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002214 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002215 Folder openFolder = mWorkspace.getFolderForTag(info);
2216
2217 // If the folder info reports that the associated folder is open, then verify that
2218 // it is actually opened. There have been a few instances where this gets out of sync.
2219 if (info.opened && openFolder == null) {
2220 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002221 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002222 info.opened = false;
2223 }
2224
Adam Cohenfb91f302012-06-11 15:45:18 -07002225 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 // Close any open folder
2227 closeFolder();
2228 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002229 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 } else {
2231 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 int folderScreen;
2233 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002234 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 // .. and close it
2236 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002237 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 // Close any folder open on the current screen
2239 closeFolder();
2240 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002241 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 }
2243 }
2244 }
2245 }
2246
Adam Cohen268c4752012-06-06 17:47:33 -07002247 /**
2248 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2249 * in the DragLayer in the exact absolute location of the original FolderIcon.
2250 */
2251 private void copyFolderIconToImage(FolderIcon fi) {
2252 final int width = fi.getMeasuredWidth();
2253 final int height = fi.getMeasuredHeight();
2254
2255 // Lazy load ImageView, Bitmap and Canvas
2256 if (mFolderIconImageView == null) {
2257 mFolderIconImageView = new ImageView(this);
2258 }
2259 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2260 mFolderIconBitmap.getHeight() != height) {
2261 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2262 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2263 }
2264
2265 DragLayer.LayoutParams lp;
2266 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2267 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2268 } else {
2269 lp = new DragLayer.LayoutParams(width, height);
2270 }
2271
Adam Cohen307fe232012-08-16 17:55:58 -07002272 // The layout from which the folder is being opened may be scaled, adjust the starting
2273 // view size by this scale factor.
2274 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002275 lp.customPosition = true;
2276 lp.x = mRectForFolderAnimation.left;
2277 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002278 lp.width = (int) (scale * width);
2279 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002280
2281 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2282 fi.draw(mFolderIconCanvas);
2283 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002284 if (fi.getFolder() != null) {
2285 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2286 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002287 }
Adam Cohen268c4752012-06-06 17:47:33 -07002288 // Just in case this image view is still in the drag layer from a previous animation,
2289 // we remove it and re-add it.
2290 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2291 mDragLayer.removeView(mFolderIconImageView);
2292 }
2293 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002294 if (fi.getFolder() != null) {
2295 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002296 }
Adam Cohen268c4752012-06-06 17:47:33 -07002297 }
2298
Adam Cohen2801caf2011-05-13 20:57:39 -07002299 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002300 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002301 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2302 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2303 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2304
Adam Cohenc51934b2011-07-26 21:07:43 -07002305 FolderInfo info = (FolderInfo) fi.getTag();
2306 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2307 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002308 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2309 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002310 }
2311
Adam Cohen268c4752012-06-06 17:47:33 -07002312 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2313 copyFolderIconToImage(fi);
2314 fi.setVisibility(View.INVISIBLE);
2315
Michael Jurka2ecf9952012-06-18 12:52:28 -07002316 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002317 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002318 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2319 oa.start();
2320 }
2321
Adam Cohen268c4752012-06-06 17:47:33 -07002322 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002323 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002324 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2325 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2326 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2327
Adam Cohen268c4752012-06-06 17:47:33 -07002328 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002329
Adam Cohen268c4752012-06-06 17:47:33 -07002330 // We remove and re-draw the FolderIcon in-case it has changed
2331 mDragLayer.removeView(mFolderIconImageView);
2332 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002333 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002334 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002335 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002336 oa.addListener(new AnimatorListenerAdapter() {
2337 @Override
2338 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002339 if (cl != null) {
2340 cl.clearFolderLeaveBehind();
2341 // Remove the ImageView copy of the FolderIcon and make the original visible.
2342 mDragLayer.removeView(mFolderIconImageView);
2343 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002344 }
2345 }
2346 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002347 oa.start();
2348 }
2349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002351 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 * is animated relative to the specified View. If the View is null, no animation
2353 * is played.
2354 *
2355 * @param folderInfo The FolderInfo describing the folder to open.
2356 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002357 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002358 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002359 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002360
Adam Cohena9cf38f2011-05-02 15:36:58 -07002361 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362
Adam Cohen4554ee12011-08-03 16:13:21 -07002363 // Just verify that the folder hasn't already been added to the DragLayer.
2364 // There was a one-off crash where the folder had a parent already.
2365 if (folder.getParent() == null) {
2366 mDragLayer.addView(folder);
2367 mDragController.addDropTarget((DropTarget) folder);
2368 } else {
2369 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2370 folder.getParent() + ").");
2371 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002372 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002373 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002374
2375 // Notify the accessibility manager that this folder "window" has appeared and occluded
2376 // the workspace items
2377 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2378 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002379 }
2380
2381 public void closeFolder() {
2382 Folder folder = mWorkspace.getOpenFolder();
2383 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002384 if (folder.isEditingName()) {
2385 folder.dismissEditingName();
2386 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002387 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002388
2389 // Dismiss the folder cling
2390 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002391 }
2392 }
2393
2394 void closeFolder(Folder folder) {
2395 folder.getInfo().opened = false;
2396
2397 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2398 if (parent != null) {
2399 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2400 shrinkAndFadeInFolderIcon(fi);
2401 }
2402 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002403
2404 // Notify the accessibility manager that this folder "window" has disappeard and no
2405 // longer occludeds the workspace items
2406 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 }
2408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002410 if (!isDraggingEnabled()) return false;
2411 if (isWorkspaceLocked()) return false;
2412 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413
2414 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002415 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 }
2417
Michael Jurka0280c3b2010-09-17 15:00:07 -07002418 resetAddInfo();
2419 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002421 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 return true;
2423 }
2424
Winson Chung3d503fb2011-07-13 17:25:49 -07002425 // The hotseat touch handling does not go through Workspace, and we always allow long press
2426 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002427 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002428 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2429 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002430 if (itemUnderLongClick == null) {
2431 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002432 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2433 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002434 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002435 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2436 mWorkspace.startReordering();
2437 } else {
2438 startWallpaper();
2439 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002441 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002443 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 }
2445 }
2446 }
2447 return true;
2448 }
2449
Winson Chung3d503fb2011-07-13 17:25:49 -07002450 boolean isHotseatLayout(View layout) {
2451 return mHotseat != null && layout != null &&
2452 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2453 }
2454 Hotseat getHotseat() {
2455 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002456 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002457 SearchDropTargetBar getSearchBar() {
2458 return mSearchDropTargetBar;
2459 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002460
Winson Chung3d503fb2011-07-13 17:25:49 -07002461 /**
2462 * Returns the CellLayout of the specified container at the specified screen.
2463 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002464 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002465 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2466 if (mHotseat != null) {
2467 return mHotseat.getLayout();
2468 } else {
2469 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002470 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002471 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002472 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002473 }
2474 }
2475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 Workspace getWorkspace() {
2477 return mWorkspace;
2478 }
2479
Daniel Sandler843e8602010-06-07 14:59:01 -04002480 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002481 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002482 }
2483
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002484 /**
2485 * Helper method for the cameraZoomIn/cameraZoomOut animations
2486 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002487 * @param scaleFactor The scale factor used for the zoom
2488 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002489 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002490 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002491 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002492 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002493
Winson Chung18f41f82012-05-09 13:28:10 -07002494 void disableWallpaperIfInAllApps() {
2495 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002496 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002497 if (mAppsCustomizeTabHost != null &&
2498 !mAppsCustomizeTabHost.isTransitioning()) {
2499 updateWallpaperVisibility(false);
2500 }
2501 }
2502 }
2503
Craig Mautner360310b2012-10-26 15:13:08 -07002504 private void setWorkspaceBackground(boolean workspace) {
2505 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002506 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002507 }
2508
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002509 void updateWallpaperVisibility(boolean visible) {
2510 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2511 int curflags = getWindow().getAttributes().flags
2512 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2513 if (wpflags != curflags) {
2514 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2515 }
Craig Mautner360310b2012-10-26 15:13:08 -07002516 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002517 }
2518
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002519 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2520 if (v instanceof LauncherTransitionable) {
2521 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2522 }
2523 }
2524
Michael Jurkabed61d22012-02-14 22:51:29 -08002525 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2526 if (v instanceof LauncherTransitionable) {
2527 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2528 }
Winson Chung70442722012-02-10 15:43:22 -08002529
2530 // Update the workspace transition step as well
2531 dispatchOnLauncherTransitionStep(v, 0f);
2532 }
2533
2534 private void dispatchOnLauncherTransitionStep(View v, float t) {
2535 if (v instanceof LauncherTransitionable) {
2536 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2537 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002538 }
2539
2540 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2541 if (v instanceof LauncherTransitionable) {
2542 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2543 }
Winson Chung70442722012-02-10 15:43:22 -08002544
2545 // Update the workspace transition step as well
2546 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002547 }
2548
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002549 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002550 * Things to test when changing the following seven functions.
2551 * - Home from workspace
2552 * - from center screen
2553 * - from other screens
2554 * - Home from all apps
2555 * - from center screen
2556 * - from other screens
2557 * - Back from all apps
2558 * - from center screen
2559 * - from other screens
2560 * - Launch app from workspace and quit
2561 * - with back
2562 * - with home
2563 * - Launch app from all apps and quit
2564 * - with back
2565 * - with home
2566 * - Go to a screen that's not the default, then all
2567 * apps, and launch and app, and go back
2568 * - with back
2569 * -with home
2570 * - On workspace, long press power and go back
2571 * - with back
2572 * - with home
2573 * - On all apps, long press power and go back
2574 * - with back
2575 * - with home
2576 * - On workspace, power off
2577 * - On all apps, power off
2578 * - Launch an app and turn off the screen while in that app
2579 * - Go back with home key
2580 * - Go back with back key TODO: make this not go to workspace
2581 * - From all apps
2582 * - From workspace
2583 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2584 * - From all apps
2585 * - From the center workspace
2586 * - From another workspace
2587 */
2588
2589 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002590 * Zoom the camera out from the workspace to reveal 'toView'.
2591 * Assumes that the view to show is anchored at either the very top or very bottom
2592 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002593 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002594 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002595 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002596 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002597 mStateAnimation.cancel();
2598 mStateAnimation = null;
2599 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002600 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002601
Winson Chungf0ea4d32011-06-06 14:27:16 -07002602 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2603 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2604 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002605 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002606 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002607 final int startDelay =
2608 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002609
Michael Jurkab3e22d92011-10-31 15:58:33 -07002610 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002611
Michael Jurkad74c9842011-07-10 12:44:21 -07002612 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002613 Animator workspaceAnim =
2614 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002615
2616 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002617 toView.setScaleX(scale);
2618 toView.setScaleY(scale);
2619 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2620 scaleAnim.
2621 scaleX(1f).scaleY(1f).
2622 setDuration(duration).
2623 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002624
Winson Chungf0ea4d32011-06-06 14:27:16 -07002625 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002626 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002627 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002628 .ofFloat(toView, "alpha", 0f, 1f)
2629 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002630 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002631 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2632 @Override
2633 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002634 if (animation == null) {
2635 throw new RuntimeException("animation is null");
2636 }
Winson Chung70442722012-02-10 15:43:22 -08002637 float t = (Float) animation.getAnimatedValue();
2638 dispatchOnLauncherTransitionStep(fromView, t);
2639 dispatchOnLauncherTransitionStep(toView, t);
2640 }
2641 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002642
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002643 // toView should appear right at the end of the workspace shrink
2644 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002645 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002646 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002647 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002648
2649 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002650 boolean animationCancelled = false;
2651
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002652 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002653 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002654 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002655 // Prepare the position
2656 toView.setTranslationX(0.0f);
2657 toView.setTranslationY(0.0f);
2658 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002659 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002660 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002661 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002662 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002663 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2664 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002665
Daniel Sandlere4f98912013-06-25 15:13:26 -04002666 if (mWorkspace != null
2667 && !springLoaded
2668 && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002669 // Hide the workspace scrollbar
2670 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002671 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002672 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002673 if (!animationCancelled) {
2674 updateWallpaperVisibility(false);
2675 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002676
2677 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002678 if (mSearchDropTargetBar != null) {
2679 mSearchDropTargetBar.hideSearchBar(false);
2680 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002681 }
2682
Adam Cohencff6af82011-09-13 14:51:53 -07002683 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002684 public void onAnimationCancel(Animator animation) {
2685 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002686 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002687 });
2688
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002689 if (workspaceAnim != null) {
2690 mStateAnimation.play(workspaceAnim);
2691 }
Michael Jurka1899a362011-11-03 13:50:45 -07002692
2693 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002694
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002695 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2696 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002697
2698 // If any of the objects being animated haven't been measured/laid out
2699 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002700 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002701 (mWorkspace.getMeasuredWidth() == 0) ||
2702 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002703 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002704 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002705
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002706 final AnimatorSet stateAnimation = mStateAnimation;
2707 final Runnable startAnimRunnable = new Runnable() {
2708 public void run() {
2709 // Check that mStateAnimation hasn't changed while
2710 // we waited for a layout/draw pass
2711 if (mStateAnimation != stateAnimation)
2712 return;
2713 setPivotsForZoom(toView, scale);
2714 dispatchOnLauncherTransitionStart(fromView, animated, false);
2715 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002716 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002717 }
2718 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002719 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002720 final ViewTreeObserver observer = toView.getViewTreeObserver();
2721 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2722 public void onGlobalLayout() {
2723 startAnimRunnable.run();
2724 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2725 }
2726 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002727 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002728 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002729 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002730 } else {
2731 toView.setTranslationX(0.0f);
2732 toView.setTranslationY(0.0f);
2733 toView.setScaleX(1.0f);
2734 toView.setScaleY(1.0f);
2735 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002736 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002737
Daniel Sandlere4f98912013-06-25 15:13:26 -04002738 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002739 // Hide the workspace scrollbar
2740 mWorkspace.hideScrollingIndicator(true);
2741 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002742
2743 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002744 if (mSearchDropTargetBar != null) {
2745 mSearchDropTargetBar.hideSearchBar(false);
2746 }
Michael Jurkaabded662011-03-04 12:06:57 -08002747 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002748 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002749 dispatchOnLauncherTransitionStart(fromView, animated, false);
2750 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002751 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002752 dispatchOnLauncherTransitionStart(toView, animated, false);
2753 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002754 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002755 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002756 }
2757
2758 /**
2759 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002760 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002761 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002762 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002763 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2764 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002765
Michael Jurkab3e22d92011-10-31 15:58:33 -07002766 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002767 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002768 mStateAnimation.cancel();
2769 mStateAnimation = null;
2770 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002772
Winson Chungf0ea4d32011-06-06 14:27:16 -07002773 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002774 final int fadeOutDuration =
2775 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002776 final float scaleFactor = (float)
2777 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2778 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002779 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002780 Animator workspaceAnim = null;
2781
2782 if (toState == State.WORKSPACE) {
2783 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2784 workspaceAnim = mWorkspace.getChangeStateAnimation(
2785 Workspace.State.NORMAL, animated, stagger);
2786 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2787 workspaceAnim = mWorkspace.getChangeStateAnimation(
2788 Workspace.State.SPRING_LOADED, animated);
2789 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002790
Michael Jurkab3e22d92011-10-31 15:58:33 -07002791 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002792 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002793 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002794 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002795 final LauncherViewPropertyAnimator scaleAnim =
2796 new LauncherViewPropertyAnimator(fromView);
2797 scaleAnim.
2798 scaleX(scaleFactor).scaleY(scaleFactor).
2799 setDuration(duration).
2800 setInterpolator(new Workspace.ZoomInInterpolator());
2801
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002802 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002803 .ofFloat(fromView, "alpha", 1f, 0f)
2804 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002805 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002806 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2807 @Override
2808 public void onAnimationUpdate(ValueAnimator animation) {
2809 float t = 1f - (Float) animation.getAnimatedValue();
2810 dispatchOnLauncherTransitionStep(fromView, t);
2811 dispatchOnLauncherTransitionStep(toView, t);
2812 }
2813 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002814
Michael Jurka2ecf9952012-06-18 12:52:28 -07002815 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002816
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002817 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2818 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002819 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002820
2821 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002822 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002823 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002824 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002825 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002826 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2827 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002828 if (mWorkspace != null) {
2829 mWorkspace.hideScrollingIndicator(false);
2830 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002831 if (onCompleteRunnable != null) {
2832 onCompleteRunnable.run();
2833 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002834 mAppsCustomizeContent.updateCurrentPageScroll();
2835 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002836 }
2837 });
2838
Winson Chungf0ea4d32011-06-06 14:27:16 -07002839 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002840 if (workspaceAnim != null) {
2841 mStateAnimation.play(workspaceAnim);
2842 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002843 dispatchOnLauncherTransitionStart(fromView, animated, true);
2844 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002845 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002846 } else {
2847 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002848 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002849 dispatchOnLauncherTransitionStart(fromView, animated, true);
2850 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002851 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002852 dispatchOnLauncherTransitionStart(toView, animated, true);
2853 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002854 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002855 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002856 }
2857
Michael Jurkae326f182011-11-21 14:05:46 -08002858 @Override
2859 public void onTrimMemory(int level) {
2860 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002861 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002862 mAppsCustomizeTabHost.onTrimMemory();
2863 }
2864 }
2865
Winson Chung18f41f82012-05-09 13:28:10 -07002866 @Override
2867 public void onWindowFocusChanged(boolean hasFocus) {
2868 if (!hasFocus) {
2869 // When another window occludes launcher (like the notification shade, or recents),
2870 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2871 updateWallpaperVisibility(true);
2872 } else {
2873 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002874 mWorkspace.postDelayed(new Runnable() {
2875 @Override
2876 public void run() {
2877 disableWallpaperIfInAllApps();
2878 }
2879 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002880 }
2881 }
2882
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002883 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002884 showWorkspace(animated, null);
2885 }
2886
2887 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002888 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002889 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002890 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002891 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002892
Winson Chungc7d2b602012-05-16 17:02:20 -07002893 // Show the search bar (only animate if we were showing the drop target bar in spring
2894 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002895 if (mSearchDropTargetBar != null) {
2896 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2897 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002898
Michael Jurkab737ee62011-11-15 15:57:22 -08002899 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002900 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002901
2902 // Set focus to the AppsCustomize button
2903 if (mAllAppsButton != null) {
2904 mAllAppsButton.requestFocus();
2905 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002906 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002907
Michael Jurkab737ee62011-11-15 15:57:22 -08002908 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002909
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002910 // Change the state *after* we've called all the transition code
2911 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002912
Winson Chung5fb63472011-02-02 17:03:37 -08002913 // Resume the auto-advance of widgets
2914 mUserPresent = true;
2915 updateRunning();
2916
alanv1d4fde62012-10-17 13:15:47 -07002917 // Send an accessibility event to announce the context change
2918 getWindow().getDecorView()
2919 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002920
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002921 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002922 }
2923
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002924 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002925 }
2926
Michael Jurkab3e22d92011-10-31 15:58:33 -07002927 void showAllApps(boolean animated) {
2928 if (mState != State.WORKSPACE) return;
2929
2930 showAppsCustomizeHelper(animated, false);
2931 mAppsCustomizeTabHost.requestFocus();
2932
Michael Jurkab3e22d92011-10-31 15:58:33 -07002933 // Change the state *after* we've called all the transition code
2934 mState = State.APPS_CUSTOMIZE;
2935
2936 // Pause the auto-advance of widgets until we are out of AllApps
2937 mUserPresent = false;
2938 updateRunning();
2939 closeFolder();
2940
2941 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002942 getWindow().getDecorView()
2943 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002944 }
2945
Adam Cohen7777d962011-08-18 18:58:38 -07002946 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002947 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002948 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002949 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002950 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002951 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002952 }
Adam Cohen7777d962011-08-18 18:58:38 -07002953
Adam Cohened66b2b2012-01-23 17:28:51 -08002954 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2955 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002956 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2957
Winson Chunge7a03942011-08-05 15:05:12 -07002958 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002959 @Override
2960 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002961 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002962 // Before we show workspace, hide all apps again because
2963 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2964 // clean up our state transition functions
2965 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002966 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002967 } else {
2968 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002969 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002970 }
2971 }, (extendedDelay ?
2972 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2973 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2974 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002975
Michael Jurkad3ef3062010-11-23 16:23:58 -08002976 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002977 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002978 final boolean animated = true;
2979 final boolean springLoaded = true;
2980 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002981 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002982 }
2983 // Otherwise, we are not in spring loaded mode, so don't do anything.
2984 }
2985
Michael Jurkab737ee62011-11-15 15:57:22 -08002986 void hideDockDivider() {
2987 if (mQsbDivider != null && mDockDivider != null) {
2988 mQsbDivider.setVisibility(View.INVISIBLE);
2989 mDockDivider.setVisibility(View.INVISIBLE);
2990 }
2991 }
2992
2993 void showDockDivider(boolean animated) {
2994 if (mQsbDivider != null && mDockDivider != null) {
2995 mQsbDivider.setVisibility(View.VISIBLE);
2996 mDockDivider.setVisibility(View.VISIBLE);
2997 if (mDividerAnimator != null) {
2998 mDividerAnimator.cancel();
2999 mQsbDivider.setAlpha(1f);
3000 mDockDivider.setAlpha(1f);
3001 mDividerAnimator = null;
3002 }
3003 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003004 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3005 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3006 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003007 int duration = 0;
3008 if (mSearchDropTargetBar != null) {
3009 duration = mSearchDropTargetBar.getTransitionInDuration();
3010 }
3011 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003012 mDividerAnimator.start();
3013 }
3014 }
3015 }
3016
Michael Jurkab3e22d92011-10-31 15:58:33 -07003017 void lockAllApps() {
3018 // TODO
3019 }
3020
3021 void unlockAllApps() {
3022 // TODO
3023 }
3024
Winson Chungf0ea4d32011-06-06 14:27:16 -07003025 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003026 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003027 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003028 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003029 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003030 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003031 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003032 int duration = 0;
3033 if (mSearchDropTargetBar != null) {
3034 duration = mSearchDropTargetBar.getTransitionInDuration();
3035 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003036 mHotseat.animate().alpha(1f).setDuration(duration);
3037 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003038 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003039 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003040 }
3041 }
3042 }
3043
3044 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003045 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003046 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003047 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003048 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003049 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003050 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003051 int duration = 0;
3052 if (mSearchDropTargetBar != null) {
3053 duration = mSearchDropTargetBar.getTransitionOutDuration();
3054 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003055 mHotseat.animate().alpha(0f).setDuration(duration);
3056 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003057 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003058 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003059 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003060 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003061 }
3062
Patrick Dubroy5f445422011-02-18 14:35:21 -08003063 /**
3064 * Add an item from all apps or customize onto the given workspace screen.
3065 * If layout is null, add to the current screen.
3066 */
3067 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003068 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003069 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003070 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003071 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003072
Winson Chungdff8ebb2011-09-08 17:25:31 -07003073 /** Maps the current orientation to an index for referencing orientation correct global icons */
3074 private int getCurrentOrientationIndexForGlobalIcons() {
3075 // default - 0, landscape - 1
3076 switch (getResources().getConfiguration().orientation) {
3077 case Configuration.ORIENTATION_LANDSCAPE:
3078 return 1;
3079 default:
3080 return 0;
3081 }
3082 }
3083
Michael Jurkaae65ee32012-04-30 11:45:54 -07003084 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003085 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003086 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003087 // Look for the toolbar icon specified in the activity meta-data
3088 Bundle metaData = packageManager.getActivityInfo(
3089 activityName, PackageManager.GET_META_DATA).metaData;
3090 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003091 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003092 if (iconResId != 0) {
3093 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003094 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003095 }
3096 }
3097 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003098 // This can happen if the activity defines an invalid drawable
3099 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3100 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003101 } catch (Resources.NotFoundException nfe) {
3102 // This can happen if the activity defines an invalid drawable
3103 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3104 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003105 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003106 return null;
3107 }
3108
3109 // if successful in getting icon, return it; otherwise, set button to use default drawable
3110 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003111 int buttonId, ComponentName activityName, int fallbackDrawableId,
3112 String toolbarResourceName) {
3113 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003114 Resources r = getResources();
3115 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3116 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003117
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003118 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003119 // If we were unable to find the icon via the meta-data, use a generic one
3120 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003121 toolbarIcon = r.getDrawable(fallbackDrawableId);
3122 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003123 if (button != null) {
3124 button.setCompoundDrawables(toolbarIcon, null, null, null);
3125 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003126 return null;
3127 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003128 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003129 if (button != null) {
3130 button.setCompoundDrawables(toolbarIcon, null, null, null);
3131 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003132 return toolbarIcon.getConstantState();
3133 }
3134 }
3135
3136 // if successful in getting icon, return it; otherwise, set button to use default drawable
3137 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003138 int buttonId, ComponentName activityName, int fallbackDrawableId,
3139 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003140 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003141 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003142
Michael Jurka19e0fc52011-07-22 18:00:21 -07003143 if (button != null) {
3144 // If we were unable to find the icon via the meta-data, use a
3145 // generic one
3146 if (toolbarIcon == null) {
3147 button.setImageResource(fallbackDrawableId);
3148 } else {
3149 button.setImageDrawable(toolbarIcon);
3150 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003151 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003152
3153 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3154
Michael Jurka0423dcf2010-10-05 14:56:18 -07003155 }
3156
Winson Chung1b884092012-06-08 17:13:02 -07003157 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003158 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003159 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003160 }
3161
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003162 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003163 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003164 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003165 }
3166
Winson Chungbb185bd2011-11-21 12:31:42 -08003167 private void invalidatePressedFocusedStates(View container, View button) {
3168 if (container instanceof HolographicLinearLayout) {
3169 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3170 layout.invalidatePressedFocusedStates();
3171 } else if (button instanceof HolographicImageView) {
3172 HolographicImageView view = (HolographicImageView) button;
3173 view.invalidatePressedFocusedStates();
3174 }
3175 }
3176
Winson Chungc51db6a2011-10-05 11:44:49 -07003177 private boolean updateGlobalSearchIcon() {
3178 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003179 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003180 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003181 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003182
Winson Chung1cad91e2011-05-25 17:41:01 -07003183 final SearchManager searchManager =
3184 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3185 ComponentName activityName = searchManager.getGlobalSearchActivity();
3186 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003187 int coi = getCurrentOrientationIndexForGlobalIcons();
3188 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003189 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3190 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3191 if (sGlobalSearchIcon[coi] == null) {
3192 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3193 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3194 TOOLBAR_ICON_METADATA_NAME);
3195 }
3196
Winson Chungc51db6a2011-10-05 11:44:49 -07003197 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3198 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003199 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003200 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003201 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003202 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003203 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3204 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3205 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003206 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003207 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003208 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003209 }
3210 }
3211
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003212 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003213 final View searchButtonContainer = findViewById(R.id.search_button_container);
3214 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003215 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003216 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003217 }
3218
Winson Chungc51db6a2011-10-05 11:44:49 -07003219 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003220 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003221 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003222
Winson Chungc51db6a2011-10-05 11:44:49 -07003223 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003224 final SearchManager searchManager =
3225 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3226 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3227
3228 ComponentName activityName = null;
3229 if (globalSearchActivity != null) {
3230 // Check if the global search activity handles voice search
3231 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3232 intent.setPackage(globalSearchActivity.getPackageName());
3233 activityName = intent.resolveActivity(getPackageManager());
3234 }
3235
3236 if (activityName == null) {
3237 // Fallback: check if an activity other than the global search activity
3238 // resolves this
3239 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3240 activityName = intent.resolveActivity(getPackageManager());
3241 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003242 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003243 int coi = getCurrentOrientationIndexForGlobalIcons();
3244 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003245 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3246 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3247 if (sVoiceSearchIcon[coi] == null) {
3248 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3249 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3250 TOOLBAR_ICON_METADATA_NAME);
3251 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003252 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003253 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003254 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003255 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003256 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003257 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003258 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003259 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003260 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003262 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003263 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003264
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003265 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003266 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3267 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003268 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003269 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003270 }
3271
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003272 public void setVoiceButtonProxyVisible(boolean visible) {
3273 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3274 if (voiceButtonProxy != null) {
3275 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3276 }
3277 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003278 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003279 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003280 */
3281 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003282 final View marketButton = findViewById(R.id.market_button);
3283 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3284 // Find the app market activity by resolving an intent.
3285 // (If multiple app markets are installed, it will return the ResolverActivity.)
3286 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003287 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003288 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003289 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003290 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003291 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3292 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003293 marketButton.setVisibility(View.VISIBLE);
3294 } else {
3295 // We should hide and disable the view so that we don't try and restore the visibility
3296 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3297 marketButton.setVisibility(View.GONE);
3298 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003299 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003300 }
3301
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003302 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003303 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3304 Resources r = getResources();
3305 Drawable marketIconDrawable = d.newDrawable(r);
3306 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3307 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3308 marketIconDrawable.setBounds(0, 0, w, h);
3309
3310 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003311 }
3312
Michael Jurkad7c28052012-04-27 15:43:36 -07003313 @Override
3314 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003315 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003316 final List<CharSequence> text = event.getText();
3317 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003318 // Populate event with a fake title based on the current state.
3319 if (mState == State.APPS_CUSTOMIZE) {
3320 text.add(getString(R.string.all_apps_button_label));
3321 } else {
3322 text.add(getString(R.string.all_apps_home_button_label));
3323 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003324 return result;
3325 }
3326
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003327 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003328 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003329 */
3330 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3331 @Override
3332 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003333 closeSystemDialogs();
3334 }
3335 }
3336
3337 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003338 * Receives notifications whenever the appwidgets are reset.
3339 */
3340 private class AppWidgetResetObserver extends ContentObserver {
3341 public AppWidgetResetObserver() {
3342 super(new Handler());
3343 }
3344
3345 @Override
3346 public void onChange(boolean selfChange) {
3347 onAppWidgetReset();
3348 }
3349 }
3350
3351 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003352 * If the activity is currently paused, signal that we need to run the passed Runnable
3353 * in onResume.
3354 *
3355 * This needs to be called from incoming places where resources might have been loaded
3356 * while we are paused. That is becaues the Configuration might be wrong
3357 * when we're not running, and if it comes back to what it was when we
3358 * were paused, we are not restarted.
3359 *
3360 * Implementation of the method from LauncherModel.Callbacks.
3361 *
3362 * @return true if we are currently paused. The caller might be able to
3363 * skip some work in that case since we will come back again.
3364 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003365 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003366 if (mPaused) {
3367 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003368 if (deletePreviousRunnables) {
3369 while (mOnResumeCallbacks.remove(run)) {
3370 }
3371 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003372 mOnResumeCallbacks.add(run);
3373 return true;
3374 } else {
3375 return false;
3376 }
3377 }
3378
Michael Jurkac402cd92013-05-20 15:49:32 +02003379 private boolean waitUntilResume(Runnable run) {
3380 return waitUntilResume(run, false);
3381 }
3382
Michael Jurka7607c2f2013-04-03 14:33:19 -07003383 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003384 * If the activity is currently paused, signal that we need to re-run the loader
3385 * in onResume.
3386 *
3387 * This needs to be called from incoming places where resources might have been loaded
3388 * while we are paused. That is becaues the Configuration might be wrong
3389 * when we're not running, and if it comes back to what it was when we
3390 * were paused, we are not restarted.
3391 *
3392 * Implementation of the method from LauncherModel.Callbacks.
3393 *
3394 * @return true if we are currently paused. The caller might be able to
3395 * skip some work in that case since we will come back again.
3396 */
3397 public boolean setLoadOnResume() {
3398 if (mPaused) {
3399 Log.i(TAG, "setLoadOnResume");
3400 mOnResumeNeedsLoad = true;
3401 return true;
3402 } else {
3403 return false;
3404 }
3405 }
3406
3407 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003409 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003410 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003411 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003412 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003413 } else {
3414 return SCREEN_COUNT / 2;
3415 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003417
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 /**
3419 * Refreshes the shortcuts shown on the workspace.
3420 *
3421 * Implementation of the method from LauncherModel.Callbacks.
3422 */
3423 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003424 // If we're starting binding all over again, clear any bind calls we'd postponed in
3425 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3426 // from scratch again
3427 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003428
Michael Jurka7607c2f2013-04-03 14:33:19 -07003429 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003430 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003431 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003432 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 int count = workspace.getChildCount();
3434 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003435 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003436 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3437 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003438 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003439 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003440 if (mHotseat != null) {
3441 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003442 }
3443 }
3444
Adam Cohendcd297f2013-06-18 13:13:40 -07003445 @Override
3446 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003447 bindAddScreens(orderedScreenIds);
3448 mWorkspace.addExtraEmptyScreen();
3449 }
3450
3451 @Override
3452 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003453 int count = orderedScreenIds.size();
3454 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003455 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003456 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003457 }
3458
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003459 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003460 * Bind the items start-end from the list.
3461 *
3462 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003463 */
Winson Chung64359a52013-07-08 17:17:08 -07003464 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3465 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003466 if (waitUntilResume(new Runnable() {
3467 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003468 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003469 }
3470 })) {
3471 return;
3472 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003473
Winson Chungf0c6ae02012-03-21 16:10:31 -07003474 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3475 Set<String> newApps = new HashSet<String>();
3476 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3477
Winson Chung64359a52013-07-08 17:17:08 -07003478 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3479 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003480 Workspace workspace = mWorkspace;
3481 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003482 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003483
3484 // Short circuit if we are loading dock items for a configuration which has no dock
3485 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3486 mHotseat == null) {
3487 continue;
3488 }
3489
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 switch (item.itemType) {
3491 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3492 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003493 ShortcutInfo info = (ShortcutInfo) item;
3494 String uri = info.intent.toUri(0).toString();
3495 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003496
Winson Chung64359a52013-07-08 17:17:08 -07003497 /*
3498 * TODO: FIX collision case
3499 */
Winson Chungce376632013-07-11 16:12:41 -07003500 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3501 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3502 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3503 throw new RuntimeException("OCCUPIED");
3504 }
Winson Chung64359a52013-07-08 17:17:08 -07003505 }
3506
Adam Cohendcd297f2013-06-18 13:13:40 -07003507 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3508 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003509 boolean animateIconUp = false;
3510 synchronized (newApps) {
3511 if (newApps.contains(uri)) {
3512 animateIconUp = newApps.remove(uri);
3513 }
3514 }
Winson Chung64359a52013-07-08 17:17:08 -07003515 if (forceAnimateIcons) {
3516 // Animate all the applications up now
3517 shortcut.setAlpha(0f);
3518 shortcut.setScaleX(0f);
3519 shortcut.setScaleY(0f);
3520 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3521 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003522 // Prepare the view to be animated up
3523 shortcut.setAlpha(0f);
3524 shortcut.setScaleX(0f);
3525 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003526 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003527 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3528 mNewShortcutAnimateViews.add(shortcut);
3529 }
3530 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003531 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003532 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003533 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003534 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003535 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003536 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3537 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003540 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003541
Winson Chung64359a52013-07-08 17:17:08 -07003542 if (forceAnimateIcons) {
3543 // We post the animation slightly delayed to prevent slowdowns when we are loading
3544 // right after we return to launcher.
3545 mWorkspace.postDelayed(new Runnable() {
3546 public void run() {
3547 anim.playTogether(bounceAnims);
3548 anim.start();
3549 }
3550 }, NEW_APPS_ANIMATION_DELAY);
3551 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003552 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003553 }
3554
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 /**
3556 * Implementation of the method from LauncherModel.Callbacks.
3557 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003558 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3559 if (waitUntilResume(new Runnable() {
3560 public void run() {
3561 bindFolders(folders);
3562 }
3563 })) {
3564 return;
3565 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003566 sFolders.clear();
3567 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003568 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569
3570 /**
3571 * Add the views for a widget to the workspace.
3572 *
3573 * Implementation of the method from LauncherModel.Callbacks.
3574 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003575 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3576 if (waitUntilResume(new Runnable() {
3577 public void run() {
3578 bindAppWidget(item);
3579 }
3580 })) {
3581 return;
3582 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003583
Daniel Sandler843e8602010-06-07 14:59:01 -04003584 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3585 if (DEBUG_WIDGETS) {
3586 Log.d(TAG, "bindAppWidget: " + item);
3587 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003588 final Workspace workspace = mWorkspace;
3589
3590 final int appWidgetId = item.appWidgetId;
3591 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003592 if (DEBUG_WIDGETS) {
3593 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3594 }
3595
Joe Onorato9c1289c2009-08-17 11:03:03 -04003596 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3597
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003599 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600
Adam Cohendcd297f2013-06-18 13:13:40 -07003601 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003603 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3604
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 workspace.requestLayout();
3606
Daniel Sandler843e8602010-06-07 14:59:01 -04003607 if (DEBUG_WIDGETS) {
3608 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3609 + (SystemClock.uptimeMillis()-start) + "ms");
3610 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 }
3612
Adam Cohen1462de32012-07-24 22:34:36 -07003613 public void onPageBoundSynchronously(int page) {
3614 mSynchronouslyBoundPages.add(page);
3615 }
3616
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 /**
3618 * Callback saying that there aren't any more items to bind.
3619 *
3620 * Implementation of the method from LauncherModel.Callbacks.
3621 */
Adam Cohene25af792013-06-06 23:08:25 -07003622 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003623 if (waitUntilResume(new Runnable() {
3624 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003625 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003626 }
3627 })) {
3628 return;
3629 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 if (mSavedState != null) {
3631 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003632 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003634 mSavedState = null;
3635 }
3636
Adam Cohen1462de32012-07-24 22:34:36 -07003637 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003639 // If we received the result of any pending adds while the loader was running (e.g. the
3640 // widget configuration forced an orientation change), process them now.
3641 for (int i = 0; i < sPendingAddList.size(); i++) {
3642 completeAdd(sPendingAddList.get(i));
3643 }
3644 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003645
Winson Chungc51db6a2011-10-05 11:44:49 -07003646 // Update the market app icon as necessary (the other icons will be managed in response to
3647 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003648 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003649
Winson Chungf0c6ae02012-03-21 16:10:31 -07003650 // Animate up any icons as necessary
3651 if (mVisible || mWorkspaceLoading) {
3652 Runnable newAppsRunnable = new Runnable() {
3653 @Override
3654 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003655 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003656 }
3657 };
Winson Chunga2413752012-04-03 14:22:34 -07003658
Adam Cohendcd297f2013-06-18 13:13:40 -07003659 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3660 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003661 if (canRunNewAppsAnimation()) {
3662 // If the user has not interacted recently, then either snap to the new page to show
3663 // the new-apps animation or just run them if they are to appear on the current page
3664 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003665 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003666 } else {
3667 runNewAppsAnimation(false);
3668 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003669 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003670 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003671 // are on another page (or just normally if they are added to the current page)
3672 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003673 }
3674 }
3675
3676 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003677 if (upgradePath) {
3678 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003679 mWorkspace.stripDuplicateApps();
3680 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003681 }
Adam Cohen99894d92013-06-14 11:22:59 -07003682
Adam Cohen66a01fd2013-06-11 12:48:00 -07003683 mWorkspace.post(new Runnable() {
3684 @Override
3685 public void run() {
3686 onFinishBindingItems();
3687 }
3688 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003689 }
3690
Winson Chunga2413752012-04-03 14:22:34 -07003691 private boolean canRunNewAppsAnimation() {
3692 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3693 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3694 }
3695
Winson Chungc9168342013-06-26 14:54:55 -07003696 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3697 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3698 PropertyValuesHolder.ofFloat("alpha", 1f),
3699 PropertyValuesHolder.ofFloat("scaleX", 1f),
3700 PropertyValuesHolder.ofFloat("scaleY", 1f));
3701 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3702 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3703 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3704 return bounceAnim;
3705 }
3706
Winson Chungf0c6ae02012-03-21 16:10:31 -07003707 /**
3708 * Runs a new animation that scales up icons that were added while Launcher was in the
3709 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003710 *
3711 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003712 */
Winson Chunga2413752012-04-03 14:22:34 -07003713 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003714 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003715 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003716
3717 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003718 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3719 @Override
3720 public int compare(View a, View b) {
3721 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3722 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3723 int cellCountX = LauncherModel.getCellCountX();
3724 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3725 }
3726 });
Winson Chunga2413752012-04-03 14:22:34 -07003727
3728 // Animate each of the views in place (or show them immediately if requested)
3729 if (immediate) {
3730 for (View v : mNewShortcutAnimateViews) {
3731 v.setAlpha(1f);
3732 v.setScaleX(1f);
3733 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003734 }
Winson Chunga2413752012-04-03 14:22:34 -07003735 } else {
3736 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3737 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003738 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003739 }
3740 anim.playTogether(bounceAnims);
3741 anim.addListener(new AnimatorListenerAdapter() {
3742 @Override
3743 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003744 if (mWorkspace != null) {
3745 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3746 }
Winson Chunga2413752012-04-03 14:22:34 -07003747 }
3748 });
3749 anim.start();
3750 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003751
3752 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003753 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003754 mNewShortcutAnimateViews.clear();
3755 new Thread("clearNewAppsThread") {
3756 public void run() {
3757 mSharedPrefs.edit()
3758 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3759 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3760 .commit();
3761 }
3762 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003763 }
3764
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003765 @Override
3766 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003767 boolean searchVisible = updateGlobalSearchIcon();
3768 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003769 if (mSearchDropTargetBar != null) {
3770 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3771 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003772 }
3773
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003774 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003775 * Add the icons for all apps.
3776 *
3777 * Implementation of the method from LauncherModel.Callbacks.
3778 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003779 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003780 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3781 getHotseat().addAllAppsFolder(mIconCache, apps,
3782 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3783 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003784 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003785 }
3786
3787 /**
3788 * A package was updated.
3789 *
3790 * Implementation of the method from LauncherModel.Callbacks.
3791 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003792 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3793 if (waitUntilResume(new Runnable() {
3794 public void run() {
3795 bindAppsUpdated(apps);
3796 }
3797 })) {
3798 return;
3799 }
3800
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003801 if (mWorkspace != null) {
3802 mWorkspace.updateShortcuts(apps);
3803 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003804 }
3805
3806 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003807 * A package was uninstalled. We take both the super set of packageNames
3808 * in addition to specific applications to remove, the reason being that
3809 * this can be called when a package is updated as well. In that scenario,
3810 * we only remove specific components from the workspace, where as
3811 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003812 *
3813 * Implementation of the method from LauncherModel.Callbacks.
3814 */
Winson Chung83892cc2013-05-01 16:53:33 -07003815 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3816 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003817 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003818 if (waitUntilResume(new Runnable() {
3819 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003820 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003821 }
3822 })) {
3823 return;
3824 }
3825
Winson Chung64359a52013-07-08 17:17:08 -07003826 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003827 mWorkspace.removeItemsByPackageName(packageNames);
3828 } else {
3829 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003830 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003831
Winson Chunga1820962011-10-03 16:31:06 -07003832 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003833 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003834 }
Joe Onoratobe386092009-11-17 17:32:16 -08003835
3836 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003837 * A number of packages were updated.
3838 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003839 private ArrayList<Object> mWidgetsAndShortcuts;
3840 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003841 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003842 bindPackagesUpdated(mWidgetsAndShortcuts);
3843 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003844 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003845 };
3846
3847 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3848 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3849 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003850 return;
3851 }
3852
Winson Chung64359a52013-07-08 17:17:08 -07003853 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003854 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003855 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003856 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003857 }
3858
Winson Chung400438b2011-01-16 17:53:48 -08003859 private int mapConfigurationOriActivityInfoOri(int configOri) {
3860 final Display d = getWindowManager().getDefaultDisplay();
3861 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3862 switch (d.getRotation()) {
3863 case Surface.ROTATION_0:
3864 case Surface.ROTATION_180:
3865 // We are currently in the same basic orientation as the natural orientation
3866 naturalOri = configOri;
3867 break;
3868 case Surface.ROTATION_90:
3869 case Surface.ROTATION_270:
3870 // We are currently in the other basic orientation to the natural orientation
3871 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3872 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3873 break;
3874 }
3875
3876 int[] oriMap = {
3877 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3878 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3879 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3880 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3881 };
3882 // Since the map starts at portrait, we need to offset if this device's natural orientation
3883 // is landscape.
3884 int indexOffset = 0;
3885 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3886 indexOffset = 1;
3887 }
3888 return oriMap[(d.getRotation() + indexOffset) % 4];
3889 }
Adam Cohen446e9402011-09-15 18:21:21 -07003890
Winson Chung4b919f82012-05-01 10:44:08 -07003891 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003892 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003893 getResources().getBoolean(R.bool.allow_rotation);
3894 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003895 }
Winson Chung4b919f82012-05-01 10:44:08 -07003896 public void lockScreenOrientation() {
3897 if (isRotationEnabled()) {
3898 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3899 .getConfiguration().orientation));
3900 }
3901 }
3902 public void unlockScreenOrientation(boolean immediate) {
3903 if (isRotationEnabled()) {
3904 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003905 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003906 } else {
3907 mHandler.postDelayed(new Runnable() {
3908 public void run() {
3909 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3910 }
3911 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003912 }
Winson Chung4b919f82012-05-01 10:44:08 -07003913 }
Winson Chung400438b2011-01-16 17:53:48 -08003914 }
3915
Winson Chung82f55532011-08-09 14:14:23 -07003916 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003917 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003918 if (DISABLE_CLINGS) {
3919 return false;
3920 }
3921
Brett Chabot2a9e2282011-08-23 15:03:13 -07003922 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003923 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003924
Michael Jurkae233a8b2013-03-19 13:49:20 +01003925 // Restricted secondary users (child mode) will potentially have very few apps
3926 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003927// boolean supportsLimitedUsers =
3928// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3929// Account[] accounts = AccountManager.get(this).getAccounts();
3930// if (supportsLimitedUsers && accounts.length == 0) {
3931// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3932// Bundle restrictions = um.getUserRestrictions();
3933// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3934// return false;
3935// }
3936// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003937 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003938 }
Michael Jurka22143132012-10-04 17:42:38 +02003939
Winson Chung7d7541e2011-09-16 20:14:36 -07003940 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003941 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003942 if (cling != null) {
3943 cling.init(this, positionData);
3944 cling.setVisibility(View.VISIBLE);
3945 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3946 if (animate) {
3947 cling.buildLayer();
3948 cling.setAlpha(0f);
3949 cling.animate()
3950 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003951 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003952 .setDuration(SHOW_CLING_DURATION)
3953 .setStartDelay(delay)
3954 .start();
3955 } else {
3956 cling.setAlpha(1f);
3957 }
Michael Jurka22143132012-10-04 17:42:38 +02003958 cling.setFocusableInTouchMode(true);
3959 cling.post(new Runnable() {
3960 public void run() {
3961 cling.setFocusable(true);
3962 cling.requestFocus();
3963 }
3964 });
3965 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3966 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003967 }
3968 return cling;
3969 }
Michael Jurka22143132012-10-04 17:42:38 +02003970
Winson Chung7d7541e2011-09-16 20:14:36 -07003971 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003972 // To catch cases where siblings of top-level views are made invisible, just check whether
3973 // the cling is directly set to GONE before dismissing it.
3974 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003975 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003976 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003977 anim.addListener(new AnimatorListenerAdapter() {
3978 public void onAnimationEnd(Animator animation) {
3979 cling.setVisibility(View.GONE);
3980 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003981 // We should update the shared preferences on a background thread
3982 new Thread("dismissClingThread") {
3983 public void run() {
3984 SharedPreferences.Editor editor = mSharedPrefs.edit();
3985 editor.putBoolean(flag, true);
3986 editor.commit();
3987 }
3988 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003989 };
3990 });
3991 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003992 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003993 }
3994 }
Michael Jurka22143132012-10-04 17:42:38 +02003995
Winson Chung9d9d74f2011-09-19 11:49:12 -07003996 private void removeCling(int id) {
3997 final View cling = findViewById(id);
3998 if (cling != null) {
3999 final ViewGroup parent = (ViewGroup) cling.getParent();
4000 parent.post(new Runnable() {
4001 @Override
4002 public void run() {
4003 parent.removeView(cling);
4004 }
4005 });
Michael Jurka22143132012-10-04 17:42:38 +02004006 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004007 }
4008 }
Michael Jurka974c3862012-05-22 22:00:31 -07004009
4010 private boolean skipCustomClingIfNoAccounts() {
4011 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4012 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4013 if (customCling) {
4014 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004015 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004016 Account[] accounts = am.getAccountsByType("com.google");
4017 return accounts.length == 0;
4018 }
4019 return false;
4020 }
4021
Winson Chung7d7541e2011-09-16 20:14:36 -07004022 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004023 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004024 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004025 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4026 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004027 // If we're not using the default workspace layout, replace workspace cling
4028 // with a custom workspace cling (usually specified in an overlay)
4029 // For now, only do this on tablets
4030 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004031 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004032 // Use a custom cling
4033 View cling = findViewById(R.id.workspace_cling);
4034 ViewGroup clingParent = (ViewGroup) cling.getParent();
4035 int clingIndex = clingParent.indexOfChild(cling);
4036 clingParent.removeViewAt(clingIndex);
4037 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4038 clingParent.addView(customCling, clingIndex);
4039 customCling.setId(R.id.workspace_cling);
4040 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004041 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004042 } else {
4043 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004044 }
4045 }
4046 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004047 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004048 if (isClingsEnabled() &&
4049 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004050 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004051 } else {
4052 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004053 }
4054 }
4055 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004056 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004057 if (isClingsEnabled() &&
4058 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4059 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004060 } else {
4061 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004062 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004063 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004064 }
4065 public boolean isFolderClingVisible() {
4066 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004067 if (cling != null) {
4068 return cling.getVisibility() == View.VISIBLE;
4069 }
4070 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004071 }
Winson Chung82f55532011-08-09 14:14:23 -07004072 public void dismissWorkspaceCling(View v) {
4073 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004074 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004075 }
4076 public void dismissAllAppsCling(View v) {
4077 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004078 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4079 }
4080 public void dismissFolderCling(View v) {
4081 Cling cling = (Cling) findViewById(R.id.folder_cling);
4082 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004083 }
4084
Winson Chung80baf5a2010-08-09 16:03:15 -07004085 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004086 * Prints out out state for debugging.
4087 */
4088 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004089 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004090 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004091 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4092 Log.d(TAG, "mRestoring=" + mRestoring);
4093 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4094 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004095 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004096 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004097
Winson Chung785d2eb2011-04-14 16:08:02 -07004098 if (mAppsCustomizeContent != null) {
4099 mAppsCustomizeContent.dumpState();
4100 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004101 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004102 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004103
4104 @Override
4105 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4106 super.dump(prefix, fd, writer, args);
4107 writer.println(" ");
4108 writer.println("Debug logs: ");
4109 for (int i = 0; i < sDumpLogs.size(); i++) {
4110 writer.println(" " + sDumpLogs.get(i));
4111 }
4112 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004113
4114 public static void dumpDebugLogsToConsole() {
4115 Log.d(TAG, "");
4116 Log.d(TAG, "*********************");
4117 Log.d(TAG, "Launcher debug logs: ");
4118 for (int i = 0; i < sDumpLogs.size(); i++) {
4119 Log.d(TAG, " " + sDumpLogs.get(i));
4120 }
4121 Log.d(TAG, "*********************");
4122 Log.d(TAG, "");
4123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004124}
Michael Jurka2763be32011-02-24 11:19:57 -08004125
Michael Jurkaabded662011-03-04 12:06:57 -08004126interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004127 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004128 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004129 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004130 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004131 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004132}