blob: 95aefe674e9605a1759dbe89b3db427884d029b7 [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.
2435 //mWorkspace.startReordering();
2436 // TODO: need to have a new way to set wallpaper or start reordering
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002437 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002439 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002441 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 }
2443 }
2444 }
2445 return true;
2446 }
2447
Winson Chung3d503fb2011-07-13 17:25:49 -07002448 boolean isHotseatLayout(View layout) {
2449 return mHotseat != null && layout != null &&
2450 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2451 }
2452 Hotseat getHotseat() {
2453 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002454 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002455 SearchDropTargetBar getSearchBar() {
2456 return mSearchDropTargetBar;
2457 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002458
Winson Chung3d503fb2011-07-13 17:25:49 -07002459 /**
2460 * Returns the CellLayout of the specified container at the specified screen.
2461 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002462 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002463 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2464 if (mHotseat != null) {
2465 return mHotseat.getLayout();
2466 } else {
2467 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002468 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002469 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002470 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002471 }
2472 }
2473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 Workspace getWorkspace() {
2475 return mWorkspace;
2476 }
2477
Daniel Sandler843e8602010-06-07 14:59:01 -04002478 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002479 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002480 }
2481
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002482 /**
2483 * Helper method for the cameraZoomIn/cameraZoomOut animations
2484 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002485 * @param scaleFactor The scale factor used for the zoom
2486 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002487 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002488 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002489 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002490 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002491
Winson Chung18f41f82012-05-09 13:28:10 -07002492 void disableWallpaperIfInAllApps() {
2493 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002494 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002495 if (mAppsCustomizeTabHost != null &&
2496 !mAppsCustomizeTabHost.isTransitioning()) {
2497 updateWallpaperVisibility(false);
2498 }
2499 }
2500 }
2501
Craig Mautner360310b2012-10-26 15:13:08 -07002502 private void setWorkspaceBackground(boolean workspace) {
2503 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002504 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002505 }
2506
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002507 void updateWallpaperVisibility(boolean visible) {
2508 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2509 int curflags = getWindow().getAttributes().flags
2510 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2511 if (wpflags != curflags) {
2512 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2513 }
Craig Mautner360310b2012-10-26 15:13:08 -07002514 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002515 }
2516
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002517 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2518 if (v instanceof LauncherTransitionable) {
2519 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2520 }
2521 }
2522
Michael Jurkabed61d22012-02-14 22:51:29 -08002523 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2524 if (v instanceof LauncherTransitionable) {
2525 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2526 }
Winson Chung70442722012-02-10 15:43:22 -08002527
2528 // Update the workspace transition step as well
2529 dispatchOnLauncherTransitionStep(v, 0f);
2530 }
2531
2532 private void dispatchOnLauncherTransitionStep(View v, float t) {
2533 if (v instanceof LauncherTransitionable) {
2534 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2535 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002536 }
2537
2538 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2539 if (v instanceof LauncherTransitionable) {
2540 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2541 }
Winson Chung70442722012-02-10 15:43:22 -08002542
2543 // Update the workspace transition step as well
2544 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002545 }
2546
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002547 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002548 * Things to test when changing the following seven functions.
2549 * - Home from workspace
2550 * - from center screen
2551 * - from other screens
2552 * - Home from all apps
2553 * - from center screen
2554 * - from other screens
2555 * - Back from all apps
2556 * - from center screen
2557 * - from other screens
2558 * - Launch app from workspace and quit
2559 * - with back
2560 * - with home
2561 * - Launch app from all apps and quit
2562 * - with back
2563 * - with home
2564 * - Go to a screen that's not the default, then all
2565 * apps, and launch and app, and go back
2566 * - with back
2567 * -with home
2568 * - On workspace, long press power and go back
2569 * - with back
2570 * - with home
2571 * - On all apps, long press power and go back
2572 * - with back
2573 * - with home
2574 * - On workspace, power off
2575 * - On all apps, power off
2576 * - Launch an app and turn off the screen while in that app
2577 * - Go back with home key
2578 * - Go back with back key TODO: make this not go to workspace
2579 * - From all apps
2580 * - From workspace
2581 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2582 * - From all apps
2583 * - From the center workspace
2584 * - From another workspace
2585 */
2586
2587 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002588 * Zoom the camera out from the workspace to reveal 'toView'.
2589 * Assumes that the view to show is anchored at either the very top or very bottom
2590 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002591 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002592 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002593 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002594 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002595 mStateAnimation.cancel();
2596 mStateAnimation = null;
2597 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002598 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002599
Winson Chungf0ea4d32011-06-06 14:27:16 -07002600 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2601 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2602 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002603 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002604 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002605 final int startDelay =
2606 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002607
Michael Jurkab3e22d92011-10-31 15:58:33 -07002608 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609
Michael Jurkad74c9842011-07-10 12:44:21 -07002610 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002611 Animator workspaceAnim =
2612 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002613
2614 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002615 toView.setScaleX(scale);
2616 toView.setScaleY(scale);
2617 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2618 scaleAnim.
2619 scaleX(1f).scaleY(1f).
2620 setDuration(duration).
2621 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002622
Winson Chungf0ea4d32011-06-06 14:27:16 -07002623 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002624 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002625 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002626 .ofFloat(toView, "alpha", 0f, 1f)
2627 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002628 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002629 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2630 @Override
2631 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002632 if (animation == null) {
2633 throw new RuntimeException("animation is null");
2634 }
Winson Chung70442722012-02-10 15:43:22 -08002635 float t = (Float) animation.getAnimatedValue();
2636 dispatchOnLauncherTransitionStep(fromView, t);
2637 dispatchOnLauncherTransitionStep(toView, t);
2638 }
2639 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002640
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002641 // toView should appear right at the end of the workspace shrink
2642 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002643 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002644 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002645 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002646
2647 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002648 boolean animationCancelled = false;
2649
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002650 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002651 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002652 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002653 // Prepare the position
2654 toView.setTranslationX(0.0f);
2655 toView.setTranslationY(0.0f);
2656 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002657 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002658 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002659 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002660 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002661 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2662 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002663
Daniel Sandlere4f98912013-06-25 15:13:26 -04002664 if (mWorkspace != null
2665 && !springLoaded
2666 && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002667 // Hide the workspace scrollbar
2668 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002669 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002670 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002671 if (!animationCancelled) {
2672 updateWallpaperVisibility(false);
2673 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002674
2675 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002676 if (mSearchDropTargetBar != null) {
2677 mSearchDropTargetBar.hideSearchBar(false);
2678 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002679 }
2680
Adam Cohencff6af82011-09-13 14:51:53 -07002681 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002682 public void onAnimationCancel(Animator animation) {
2683 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002684 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002685 });
2686
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002687 if (workspaceAnim != null) {
2688 mStateAnimation.play(workspaceAnim);
2689 }
Michael Jurka1899a362011-11-03 13:50:45 -07002690
2691 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002692
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002693 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2694 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002695
2696 // If any of the objects being animated haven't been measured/laid out
2697 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002698 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002699 (mWorkspace.getMeasuredWidth() == 0) ||
2700 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002701 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002702 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002703
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002704 final AnimatorSet stateAnimation = mStateAnimation;
2705 final Runnable startAnimRunnable = new Runnable() {
2706 public void run() {
2707 // Check that mStateAnimation hasn't changed while
2708 // we waited for a layout/draw pass
2709 if (mStateAnimation != stateAnimation)
2710 return;
2711 setPivotsForZoom(toView, scale);
2712 dispatchOnLauncherTransitionStart(fromView, animated, false);
2713 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002714 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002715 }
2716 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002717 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002718 final ViewTreeObserver observer = toView.getViewTreeObserver();
2719 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2720 public void onGlobalLayout() {
2721 startAnimRunnable.run();
2722 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2723 }
2724 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002725 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002726 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002727 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002728 } else {
2729 toView.setTranslationX(0.0f);
2730 toView.setTranslationY(0.0f);
2731 toView.setScaleX(1.0f);
2732 toView.setScaleY(1.0f);
2733 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002734 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002735
Daniel Sandlere4f98912013-06-25 15:13:26 -04002736 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002737 // Hide the workspace scrollbar
2738 mWorkspace.hideScrollingIndicator(true);
2739 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002740
2741 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002742 if (mSearchDropTargetBar != null) {
2743 mSearchDropTargetBar.hideSearchBar(false);
2744 }
Michael Jurkaabded662011-03-04 12:06:57 -08002745 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002746 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002747 dispatchOnLauncherTransitionStart(fromView, animated, false);
2748 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002749 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002750 dispatchOnLauncherTransitionStart(toView, animated, false);
2751 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002752 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002753 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002754 }
2755
2756 /**
2757 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002758 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002759 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002760 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002761 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2762 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002763
Michael Jurkab3e22d92011-10-31 15:58:33 -07002764 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002765 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002766 mStateAnimation.cancel();
2767 mStateAnimation = null;
2768 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002769 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002770
Winson Chungf0ea4d32011-06-06 14:27:16 -07002771 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002772 final int fadeOutDuration =
2773 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002774 final float scaleFactor = (float)
2775 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2776 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002777 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002778 Animator workspaceAnim = null;
2779
2780 if (toState == State.WORKSPACE) {
2781 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2782 workspaceAnim = mWorkspace.getChangeStateAnimation(
2783 Workspace.State.NORMAL, animated, stagger);
2784 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2785 workspaceAnim = mWorkspace.getChangeStateAnimation(
2786 Workspace.State.SPRING_LOADED, animated);
2787 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002788
Michael Jurkab3e22d92011-10-31 15:58:33 -07002789 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002790 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002791 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002792 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002793 final LauncherViewPropertyAnimator scaleAnim =
2794 new LauncherViewPropertyAnimator(fromView);
2795 scaleAnim.
2796 scaleX(scaleFactor).scaleY(scaleFactor).
2797 setDuration(duration).
2798 setInterpolator(new Workspace.ZoomInInterpolator());
2799
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002800 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002801 .ofFloat(fromView, "alpha", 1f, 0f)
2802 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002803 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002804 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2805 @Override
2806 public void onAnimationUpdate(ValueAnimator animation) {
2807 float t = 1f - (Float) animation.getAnimatedValue();
2808 dispatchOnLauncherTransitionStep(fromView, t);
2809 dispatchOnLauncherTransitionStep(toView, t);
2810 }
2811 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002812
Michael Jurka2ecf9952012-06-18 12:52:28 -07002813 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002814
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002815 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2816 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002817 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002818
2819 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002820 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002821 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002822 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002823 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002824 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2825 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002826 if (mWorkspace != null) {
2827 mWorkspace.hideScrollingIndicator(false);
2828 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002829 if (onCompleteRunnable != null) {
2830 onCompleteRunnable.run();
2831 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002832 mAppsCustomizeContent.updateCurrentPageScroll();
2833 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002834 }
2835 });
2836
Winson Chungf0ea4d32011-06-06 14:27:16 -07002837 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002838 if (workspaceAnim != null) {
2839 mStateAnimation.play(workspaceAnim);
2840 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002841 dispatchOnLauncherTransitionStart(fromView, animated, true);
2842 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002843 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002844 } else {
2845 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002846 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002847 dispatchOnLauncherTransitionStart(fromView, animated, true);
2848 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002849 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002850 dispatchOnLauncherTransitionStart(toView, animated, true);
2851 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002852 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002853 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002854 }
2855
Michael Jurkae326f182011-11-21 14:05:46 -08002856 @Override
2857 public void onTrimMemory(int level) {
2858 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002859 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002860 mAppsCustomizeTabHost.onTrimMemory();
2861 }
2862 }
2863
Winson Chung18f41f82012-05-09 13:28:10 -07002864 @Override
2865 public void onWindowFocusChanged(boolean hasFocus) {
2866 if (!hasFocus) {
2867 // When another window occludes launcher (like the notification shade, or recents),
2868 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2869 updateWallpaperVisibility(true);
2870 } else {
2871 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002872 mWorkspace.postDelayed(new Runnable() {
2873 @Override
2874 public void run() {
2875 disableWallpaperIfInAllApps();
2876 }
2877 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002878 }
2879 }
2880
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002881 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002882 showWorkspace(animated, null);
2883 }
2884
2885 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002886 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002887 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002888 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002889 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002890
Winson Chungc7d2b602012-05-16 17:02:20 -07002891 // Show the search bar (only animate if we were showing the drop target bar in spring
2892 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002893 if (mSearchDropTargetBar != null) {
2894 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2895 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002896
Michael Jurkab737ee62011-11-15 15:57:22 -08002897 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002898 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002899
2900 // Set focus to the AppsCustomize button
2901 if (mAllAppsButton != null) {
2902 mAllAppsButton.requestFocus();
2903 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002904 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002905
Michael Jurkab737ee62011-11-15 15:57:22 -08002906 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002907
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002908 // Change the state *after* we've called all the transition code
2909 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002910
Winson Chung5fb63472011-02-02 17:03:37 -08002911 // Resume the auto-advance of widgets
2912 mUserPresent = true;
2913 updateRunning();
2914
alanv1d4fde62012-10-17 13:15:47 -07002915 // Send an accessibility event to announce the context change
2916 getWindow().getDecorView()
2917 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002918
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002919 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002920 }
2921
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002922 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002923 }
2924
Michael Jurkab3e22d92011-10-31 15:58:33 -07002925 void showAllApps(boolean animated) {
2926 if (mState != State.WORKSPACE) return;
2927
2928 showAppsCustomizeHelper(animated, false);
2929 mAppsCustomizeTabHost.requestFocus();
2930
Michael Jurkab3e22d92011-10-31 15:58:33 -07002931 // Change the state *after* we've called all the transition code
2932 mState = State.APPS_CUSTOMIZE;
2933
2934 // Pause the auto-advance of widgets until we are out of AllApps
2935 mUserPresent = false;
2936 updateRunning();
2937 closeFolder();
2938
2939 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002940 getWindow().getDecorView()
2941 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002942 }
2943
Adam Cohen7777d962011-08-18 18:58:38 -07002944 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002945 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002946 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002947 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002948 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002949 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002950 }
Adam Cohen7777d962011-08-18 18:58:38 -07002951
Adam Cohened66b2b2012-01-23 17:28:51 -08002952 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2953 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002954 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2955
Winson Chunge7a03942011-08-05 15:05:12 -07002956 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002957 @Override
2958 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002959 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002960 // Before we show workspace, hide all apps again because
2961 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2962 // clean up our state transition functions
2963 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002964 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002965 } else {
2966 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002967 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002968 }
2969 }, (extendedDelay ?
2970 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2971 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2972 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002973
Michael Jurkad3ef3062010-11-23 16:23:58 -08002974 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002975 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002976 final boolean animated = true;
2977 final boolean springLoaded = true;
2978 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002979 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002980 }
2981 // Otherwise, we are not in spring loaded mode, so don't do anything.
2982 }
2983
Michael Jurkab737ee62011-11-15 15:57:22 -08002984 void hideDockDivider() {
2985 if (mQsbDivider != null && mDockDivider != null) {
2986 mQsbDivider.setVisibility(View.INVISIBLE);
2987 mDockDivider.setVisibility(View.INVISIBLE);
2988 }
2989 }
2990
2991 void showDockDivider(boolean animated) {
2992 if (mQsbDivider != null && mDockDivider != null) {
2993 mQsbDivider.setVisibility(View.VISIBLE);
2994 mDockDivider.setVisibility(View.VISIBLE);
2995 if (mDividerAnimator != null) {
2996 mDividerAnimator.cancel();
2997 mQsbDivider.setAlpha(1f);
2998 mDockDivider.setAlpha(1f);
2999 mDividerAnimator = null;
3000 }
3001 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003002 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3003 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3004 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003005 int duration = 0;
3006 if (mSearchDropTargetBar != null) {
3007 duration = mSearchDropTargetBar.getTransitionInDuration();
3008 }
3009 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003010 mDividerAnimator.start();
3011 }
3012 }
3013 }
3014
Michael Jurkab3e22d92011-10-31 15:58:33 -07003015 void lockAllApps() {
3016 // TODO
3017 }
3018
3019 void unlockAllApps() {
3020 // TODO
3021 }
3022
Winson Chungf0ea4d32011-06-06 14:27:16 -07003023 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003024 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003025 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003026 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003027 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003028 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003029 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003030 int duration = 0;
3031 if (mSearchDropTargetBar != null) {
3032 duration = mSearchDropTargetBar.getTransitionInDuration();
3033 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003034 mHotseat.animate().alpha(1f).setDuration(duration);
3035 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003036 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003037 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003038 }
3039 }
3040 }
3041
3042 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003043 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003044 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003045 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003046 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003047 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003048 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003049 int duration = 0;
3050 if (mSearchDropTargetBar != null) {
3051 duration = mSearchDropTargetBar.getTransitionOutDuration();
3052 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003053 mHotseat.animate().alpha(0f).setDuration(duration);
3054 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003055 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003056 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003057 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003058 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003059 }
3060
Patrick Dubroy5f445422011-02-18 14:35:21 -08003061 /**
3062 * Add an item from all apps or customize onto the given workspace screen.
3063 * If layout is null, add to the current screen.
3064 */
3065 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003066 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003067 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003068 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003069 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003070
Winson Chungdff8ebb2011-09-08 17:25:31 -07003071 /** Maps the current orientation to an index for referencing orientation correct global icons */
3072 private int getCurrentOrientationIndexForGlobalIcons() {
3073 // default - 0, landscape - 1
3074 switch (getResources().getConfiguration().orientation) {
3075 case Configuration.ORIENTATION_LANDSCAPE:
3076 return 1;
3077 default:
3078 return 0;
3079 }
3080 }
3081
Michael Jurkaae65ee32012-04-30 11:45:54 -07003082 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003083 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003084 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003085 // Look for the toolbar icon specified in the activity meta-data
3086 Bundle metaData = packageManager.getActivityInfo(
3087 activityName, PackageManager.GET_META_DATA).metaData;
3088 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003089 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003090 if (iconResId != 0) {
3091 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003092 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003093 }
3094 }
3095 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003096 // This can happen if the activity defines an invalid drawable
3097 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3098 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003099 } catch (Resources.NotFoundException nfe) {
3100 // This can happen if the activity defines an invalid drawable
3101 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3102 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003103 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003104 return null;
3105 }
3106
3107 // if successful in getting icon, return it; otherwise, set button to use default drawable
3108 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003109 int buttonId, ComponentName activityName, int fallbackDrawableId,
3110 String toolbarResourceName) {
3111 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003112 Resources r = getResources();
3113 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3114 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003115
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003116 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003117 // If we were unable to find the icon via the meta-data, use a generic one
3118 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003119 toolbarIcon = r.getDrawable(fallbackDrawableId);
3120 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003121 if (button != null) {
3122 button.setCompoundDrawables(toolbarIcon, null, null, null);
3123 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003124 return null;
3125 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003126 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003127 if (button != null) {
3128 button.setCompoundDrawables(toolbarIcon, null, null, null);
3129 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003130 return toolbarIcon.getConstantState();
3131 }
3132 }
3133
3134 // if successful in getting icon, return it; otherwise, set button to use default drawable
3135 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003136 int buttonId, ComponentName activityName, int fallbackDrawableId,
3137 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003138 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003139 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003140
Michael Jurka19e0fc52011-07-22 18:00:21 -07003141 if (button != null) {
3142 // If we were unable to find the icon via the meta-data, use a
3143 // generic one
3144 if (toolbarIcon == null) {
3145 button.setImageResource(fallbackDrawableId);
3146 } else {
3147 button.setImageDrawable(toolbarIcon);
3148 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003149 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003150
3151 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3152
Michael Jurka0423dcf2010-10-05 14:56:18 -07003153 }
3154
Winson Chung1b884092012-06-08 17:13:02 -07003155 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003156 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003157 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003158 }
3159
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003160 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003161 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003162 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003163 }
3164
Winson Chungbb185bd2011-11-21 12:31:42 -08003165 private void invalidatePressedFocusedStates(View container, View button) {
3166 if (container instanceof HolographicLinearLayout) {
3167 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3168 layout.invalidatePressedFocusedStates();
3169 } else if (button instanceof HolographicImageView) {
3170 HolographicImageView view = (HolographicImageView) button;
3171 view.invalidatePressedFocusedStates();
3172 }
3173 }
3174
Winson Chungc51db6a2011-10-05 11:44:49 -07003175 private boolean updateGlobalSearchIcon() {
3176 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003177 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003178 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003179 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003180
Winson Chung1cad91e2011-05-25 17:41:01 -07003181 final SearchManager searchManager =
3182 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3183 ComponentName activityName = searchManager.getGlobalSearchActivity();
3184 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003185 int coi = getCurrentOrientationIndexForGlobalIcons();
3186 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003187 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3188 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3189 if (sGlobalSearchIcon[coi] == null) {
3190 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3191 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3192 TOOLBAR_ICON_METADATA_NAME);
3193 }
3194
Winson Chungc51db6a2011-10-05 11:44:49 -07003195 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3196 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003197 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003198 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003199 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003200 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003201 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3202 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3203 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003204 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003205 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003206 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003207 }
3208 }
3209
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003210 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003211 final View searchButtonContainer = findViewById(R.id.search_button_container);
3212 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003213 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003214 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003215 }
3216
Winson Chungc51db6a2011-10-05 11:44:49 -07003217 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003218 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003219 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003220
Winson Chungc51db6a2011-10-05 11:44:49 -07003221 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003222 final SearchManager searchManager =
3223 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3224 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3225
3226 ComponentName activityName = null;
3227 if (globalSearchActivity != null) {
3228 // Check if the global search activity handles voice search
3229 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3230 intent.setPackage(globalSearchActivity.getPackageName());
3231 activityName = intent.resolveActivity(getPackageManager());
3232 }
3233
3234 if (activityName == null) {
3235 // Fallback: check if an activity other than the global search activity
3236 // resolves this
3237 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3238 activityName = intent.resolveActivity(getPackageManager());
3239 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003240 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003241 int coi = getCurrentOrientationIndexForGlobalIcons();
3242 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003243 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3244 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3245 if (sVoiceSearchIcon[coi] == null) {
3246 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3247 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3248 TOOLBAR_ICON_METADATA_NAME);
3249 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003250 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003251 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003252 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003253 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003254 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003255 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003256 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003257 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003258 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003259 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003260 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003261 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003262
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003263 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003264 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3265 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003266 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003267 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003268 }
3269
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003270 public void setVoiceButtonProxyVisible(boolean visible) {
3271 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3272 if (voiceButtonProxy != null) {
3273 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3274 }
3275 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003276 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003277 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003278 */
3279 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003280 final View marketButton = findViewById(R.id.market_button);
3281 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3282 // Find the app market activity by resolving an intent.
3283 // (If multiple app markets are installed, it will return the ResolverActivity.)
3284 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003285 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003286 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003287 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003288 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003289 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3290 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003291 marketButton.setVisibility(View.VISIBLE);
3292 } else {
3293 // We should hide and disable the view so that we don't try and restore the visibility
3294 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3295 marketButton.setVisibility(View.GONE);
3296 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003297 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003298 }
3299
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003300 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003301 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3302 Resources r = getResources();
3303 Drawable marketIconDrawable = d.newDrawable(r);
3304 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3305 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3306 marketIconDrawable.setBounds(0, 0, w, h);
3307
3308 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003309 }
3310
Michael Jurkad7c28052012-04-27 15:43:36 -07003311 @Override
3312 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003313 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003314 final List<CharSequence> text = event.getText();
3315 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003316 // Populate event with a fake title based on the current state.
3317 if (mState == State.APPS_CUSTOMIZE) {
3318 text.add(getString(R.string.all_apps_button_label));
3319 } else {
3320 text.add(getString(R.string.all_apps_home_button_label));
3321 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003322 return result;
3323 }
3324
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003325 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003326 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003327 */
3328 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3329 @Override
3330 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003331 closeSystemDialogs();
3332 }
3333 }
3334
3335 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003336 * Receives notifications whenever the appwidgets are reset.
3337 */
3338 private class AppWidgetResetObserver extends ContentObserver {
3339 public AppWidgetResetObserver() {
3340 super(new Handler());
3341 }
3342
3343 @Override
3344 public void onChange(boolean selfChange) {
3345 onAppWidgetReset();
3346 }
3347 }
3348
3349 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003350 * If the activity is currently paused, signal that we need to run the passed Runnable
3351 * in onResume.
3352 *
3353 * This needs to be called from incoming places where resources might have been loaded
3354 * while we are paused. That is becaues the Configuration might be wrong
3355 * when we're not running, and if it comes back to what it was when we
3356 * were paused, we are not restarted.
3357 *
3358 * Implementation of the method from LauncherModel.Callbacks.
3359 *
3360 * @return true if we are currently paused. The caller might be able to
3361 * skip some work in that case since we will come back again.
3362 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003363 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003364 if (mPaused) {
3365 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003366 if (deletePreviousRunnables) {
3367 while (mOnResumeCallbacks.remove(run)) {
3368 }
3369 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003370 mOnResumeCallbacks.add(run);
3371 return true;
3372 } else {
3373 return false;
3374 }
3375 }
3376
Michael Jurkac402cd92013-05-20 15:49:32 +02003377 private boolean waitUntilResume(Runnable run) {
3378 return waitUntilResume(run, false);
3379 }
3380
Michael Jurka7607c2f2013-04-03 14:33:19 -07003381 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003382 * If the activity is currently paused, signal that we need to re-run the loader
3383 * in onResume.
3384 *
3385 * This needs to be called from incoming places where resources might have been loaded
3386 * while we are paused. That is becaues the Configuration might be wrong
3387 * when we're not running, and if it comes back to what it was when we
3388 * were paused, we are not restarted.
3389 *
3390 * Implementation of the method from LauncherModel.Callbacks.
3391 *
3392 * @return true if we are currently paused. The caller might be able to
3393 * skip some work in that case since we will come back again.
3394 */
3395 public boolean setLoadOnResume() {
3396 if (mPaused) {
3397 Log.i(TAG, "setLoadOnResume");
3398 mOnResumeNeedsLoad = true;
3399 return true;
3400 } else {
3401 return false;
3402 }
3403 }
3404
3405 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003407 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003409 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003410 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003411 } else {
3412 return SCREEN_COUNT / 2;
3413 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003415
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 /**
3417 * Refreshes the shortcuts shown on the workspace.
3418 *
3419 * Implementation of the method from LauncherModel.Callbacks.
3420 */
3421 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003422 // If we're starting binding all over again, clear any bind calls we'd postponed in
3423 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3424 // from scratch again
3425 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003426
Michael Jurka7607c2f2013-04-03 14:33:19 -07003427 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003428 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003429 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003430 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 int count = workspace.getChildCount();
3432 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003433 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003434 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3435 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003436 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003437 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003438 if (mHotseat != null) {
3439 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003440 }
3441 }
3442
Adam Cohendcd297f2013-06-18 13:13:40 -07003443 @Override
3444 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003445 bindAddScreens(orderedScreenIds);
3446 mWorkspace.addExtraEmptyScreen();
3447 }
3448
3449 @Override
3450 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003451 int count = orderedScreenIds.size();
3452 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003453 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003454 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003455 }
3456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003457 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 * Bind the items start-end from the list.
3459 *
3460 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003461 */
Winson Chung64359a52013-07-08 17:17:08 -07003462 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3463 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003464 if (waitUntilResume(new Runnable() {
3465 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003466 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003467 }
3468 })) {
3469 return;
3470 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003471
Winson Chungf0c6ae02012-03-21 16:10:31 -07003472 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3473 Set<String> newApps = new HashSet<String>();
3474 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3475
Winson Chung64359a52013-07-08 17:17:08 -07003476 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3477 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003478 Workspace workspace = mWorkspace;
3479 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003481
3482 // Short circuit if we are loading dock items for a configuration which has no dock
3483 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3484 mHotseat == null) {
3485 continue;
3486 }
3487
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 switch (item.itemType) {
3489 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3490 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003491 ShortcutInfo info = (ShortcutInfo) item;
3492 String uri = info.intent.toUri(0).toString();
3493 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003494
Winson Chung64359a52013-07-08 17:17:08 -07003495 /*
3496 * TODO: FIX collision case
3497 */
3498 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3499 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3500 throw new RuntimeException("OCCUPIED");
3501 }
3502
Adam Cohendcd297f2013-06-18 13:13:40 -07003503 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3504 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003505 boolean animateIconUp = false;
3506 synchronized (newApps) {
3507 if (newApps.contains(uri)) {
3508 animateIconUp = newApps.remove(uri);
3509 }
3510 }
Winson Chung64359a52013-07-08 17:17:08 -07003511 if (forceAnimateIcons) {
3512 // Animate all the applications up now
3513 shortcut.setAlpha(0f);
3514 shortcut.setScaleX(0f);
3515 shortcut.setScaleY(0f);
3516 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3517 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003518 // Prepare the view to be animated up
3519 shortcut.setAlpha(0f);
3520 shortcut.setScaleX(0f);
3521 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003522 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003523 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3524 mNewShortcutAnimateViews.add(shortcut);
3525 }
3526 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003528 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003529 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003530 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003531 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003532 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3533 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003536 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003537
Winson Chung64359a52013-07-08 17:17:08 -07003538 if (forceAnimateIcons) {
3539 // We post the animation slightly delayed to prevent slowdowns when we are loading
3540 // right after we return to launcher.
3541 mWorkspace.postDelayed(new Runnable() {
3542 public void run() {
3543 anim.playTogether(bounceAnims);
3544 anim.start();
3545 }
3546 }, NEW_APPS_ANIMATION_DELAY);
3547 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003548 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003549 }
3550
Joe Onorato9c1289c2009-08-17 11:03:03 -04003551 /**
3552 * Implementation of the method from LauncherModel.Callbacks.
3553 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003554 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3555 if (waitUntilResume(new Runnable() {
3556 public void run() {
3557 bindFolders(folders);
3558 }
3559 })) {
3560 return;
3561 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003562 sFolders.clear();
3563 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565
3566 /**
3567 * Add the views for a widget to the workspace.
3568 *
3569 * Implementation of the method from LauncherModel.Callbacks.
3570 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003571 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3572 if (waitUntilResume(new Runnable() {
3573 public void run() {
3574 bindAppWidget(item);
3575 }
3576 })) {
3577 return;
3578 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003579
Daniel Sandler843e8602010-06-07 14:59:01 -04003580 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3581 if (DEBUG_WIDGETS) {
3582 Log.d(TAG, "bindAppWidget: " + item);
3583 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 final Workspace workspace = mWorkspace;
3585
3586 final int appWidgetId = item.appWidgetId;
3587 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003588 if (DEBUG_WIDGETS) {
3589 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3590 }
3591
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3593
Joe Onorato9c1289c2009-08-17 11:03:03 -04003594 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003595 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003596
Adam Cohendcd297f2013-06-18 13:13:40 -07003597 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003599 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3600
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 workspace.requestLayout();
3602
Daniel Sandler843e8602010-06-07 14:59:01 -04003603 if (DEBUG_WIDGETS) {
3604 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3605 + (SystemClock.uptimeMillis()-start) + "ms");
3606 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 }
3608
Adam Cohen1462de32012-07-24 22:34:36 -07003609 public void onPageBoundSynchronously(int page) {
3610 mSynchronouslyBoundPages.add(page);
3611 }
3612
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 /**
3614 * Callback saying that there aren't any more items to bind.
3615 *
3616 * Implementation of the method from LauncherModel.Callbacks.
3617 */
Adam Cohene25af792013-06-06 23:08:25 -07003618 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003619 if (waitUntilResume(new Runnable() {
3620 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003621 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003622 }
3623 })) {
3624 return;
3625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 if (mSavedState != null) {
3627 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003628 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003629 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 mSavedState = null;
3631 }
3632
Adam Cohen1462de32012-07-24 22:34:36 -07003633 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003634
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003635 // If we received the result of any pending adds while the loader was running (e.g. the
3636 // widget configuration forced an orientation change), process them now.
3637 for (int i = 0; i < sPendingAddList.size(); i++) {
3638 completeAdd(sPendingAddList.get(i));
3639 }
3640 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003641
Winson Chungc51db6a2011-10-05 11:44:49 -07003642 // Update the market app icon as necessary (the other icons will be managed in response to
3643 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003644 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003645
Winson Chungf0c6ae02012-03-21 16:10:31 -07003646 // Animate up any icons as necessary
3647 if (mVisible || mWorkspaceLoading) {
3648 Runnable newAppsRunnable = new Runnable() {
3649 @Override
3650 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003651 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003652 }
3653 };
Winson Chunga2413752012-04-03 14:22:34 -07003654
Adam Cohendcd297f2013-06-18 13:13:40 -07003655 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3656 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003657 if (canRunNewAppsAnimation()) {
3658 // If the user has not interacted recently, then either snap to the new page to show
3659 // the new-apps animation or just run them if they are to appear on the current page
3660 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003661 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003662 } else {
3663 runNewAppsAnimation(false);
3664 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003665 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003666 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003667 // are on another page (or just normally if they are added to the current page)
3668 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003669 }
3670 }
3671
3672 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003673 if (upgradePath) {
3674 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003675 mWorkspace.stripDuplicateApps();
3676 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003677 }
Adam Cohen99894d92013-06-14 11:22:59 -07003678
Adam Cohen66a01fd2013-06-11 12:48:00 -07003679 mWorkspace.post(new Runnable() {
3680 @Override
3681 public void run() {
3682 onFinishBindingItems();
3683 }
3684 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003685 }
3686
Winson Chunga2413752012-04-03 14:22:34 -07003687 private boolean canRunNewAppsAnimation() {
3688 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3689 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3690 }
3691
Winson Chungc9168342013-06-26 14:54:55 -07003692 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3693 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3694 PropertyValuesHolder.ofFloat("alpha", 1f),
3695 PropertyValuesHolder.ofFloat("scaleX", 1f),
3696 PropertyValuesHolder.ofFloat("scaleY", 1f));
3697 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3698 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3699 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3700 return bounceAnim;
3701 }
3702
Winson Chungf0c6ae02012-03-21 16:10:31 -07003703 /**
3704 * Runs a new animation that scales up icons that were added while Launcher was in the
3705 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003706 *
3707 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003708 */
Winson Chunga2413752012-04-03 14:22:34 -07003709 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003710 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003711 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003712
3713 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003714 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3715 @Override
3716 public int compare(View a, View b) {
3717 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3718 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3719 int cellCountX = LauncherModel.getCellCountX();
3720 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3721 }
3722 });
Winson Chunga2413752012-04-03 14:22:34 -07003723
3724 // Animate each of the views in place (or show them immediately if requested)
3725 if (immediate) {
3726 for (View v : mNewShortcutAnimateViews) {
3727 v.setAlpha(1f);
3728 v.setScaleX(1f);
3729 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003730 }
Winson Chunga2413752012-04-03 14:22:34 -07003731 } else {
3732 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3733 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003734 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003735 }
3736 anim.playTogether(bounceAnims);
3737 anim.addListener(new AnimatorListenerAdapter() {
3738 @Override
3739 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003740 if (mWorkspace != null) {
3741 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3742 }
Winson Chunga2413752012-04-03 14:22:34 -07003743 }
3744 });
3745 anim.start();
3746 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003747
3748 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003749 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003750 mNewShortcutAnimateViews.clear();
3751 new Thread("clearNewAppsThread") {
3752 public void run() {
3753 mSharedPrefs.edit()
3754 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3755 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3756 .commit();
3757 }
3758 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003759 }
3760
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003761 @Override
3762 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003763 boolean searchVisible = updateGlobalSearchIcon();
3764 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003765 if (mSearchDropTargetBar != null) {
3766 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3767 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003768 }
3769
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003770 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003771 * Add the icons for all apps.
3772 *
3773 * Implementation of the method from LauncherModel.Callbacks.
3774 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003775 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003776 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3777 getHotseat().addAllAppsFolder(mIconCache, apps,
3778 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3779 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003780 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003781 }
3782
3783 /**
3784 * A package was updated.
3785 *
3786 * Implementation of the method from LauncherModel.Callbacks.
3787 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003788 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3789 if (waitUntilResume(new Runnable() {
3790 public void run() {
3791 bindAppsUpdated(apps);
3792 }
3793 })) {
3794 return;
3795 }
3796
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003797 if (mWorkspace != null) {
3798 mWorkspace.updateShortcuts(apps);
3799 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 }
3801
3802 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003803 * A package was uninstalled. We take both the super set of packageNames
3804 * in addition to specific applications to remove, the reason being that
3805 * this can be called when a package is updated as well. In that scenario,
3806 * we only remove specific components from the workspace, where as
3807 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 *
3809 * Implementation of the method from LauncherModel.Callbacks.
3810 */
Winson Chung83892cc2013-05-01 16:53:33 -07003811 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3812 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003813 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003814 if (waitUntilResume(new Runnable() {
3815 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003816 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003817 }
3818 })) {
3819 return;
3820 }
3821
Winson Chung64359a52013-07-08 17:17:08 -07003822 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003823 mWorkspace.removeItemsByPackageName(packageNames);
3824 } else {
3825 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003826 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003827
Winson Chunga1820962011-10-03 16:31:06 -07003828 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003829 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003830 }
Joe Onoratobe386092009-11-17 17:32:16 -08003831
3832 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003833 * A number of packages were updated.
3834 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003835 private ArrayList<Object> mWidgetsAndShortcuts;
3836 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003837 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003838 bindPackagesUpdated(mWidgetsAndShortcuts);
3839 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003840 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003841 };
3842
3843 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3844 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3845 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003846 return;
3847 }
3848
Winson Chung64359a52013-07-08 17:17:08 -07003849 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003850 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003851 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003852 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003853 }
3854
Winson Chung400438b2011-01-16 17:53:48 -08003855 private int mapConfigurationOriActivityInfoOri(int configOri) {
3856 final Display d = getWindowManager().getDefaultDisplay();
3857 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3858 switch (d.getRotation()) {
3859 case Surface.ROTATION_0:
3860 case Surface.ROTATION_180:
3861 // We are currently in the same basic orientation as the natural orientation
3862 naturalOri = configOri;
3863 break;
3864 case Surface.ROTATION_90:
3865 case Surface.ROTATION_270:
3866 // We are currently in the other basic orientation to the natural orientation
3867 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3868 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3869 break;
3870 }
3871
3872 int[] oriMap = {
3873 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3874 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3875 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3876 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3877 };
3878 // Since the map starts at portrait, we need to offset if this device's natural orientation
3879 // is landscape.
3880 int indexOffset = 0;
3881 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3882 indexOffset = 1;
3883 }
3884 return oriMap[(d.getRotation() + indexOffset) % 4];
3885 }
Adam Cohen446e9402011-09-15 18:21:21 -07003886
Winson Chung4b919f82012-05-01 10:44:08 -07003887 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003888 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003889 getResources().getBoolean(R.bool.allow_rotation);
3890 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003891 }
Winson Chung4b919f82012-05-01 10:44:08 -07003892 public void lockScreenOrientation() {
3893 if (isRotationEnabled()) {
3894 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3895 .getConfiguration().orientation));
3896 }
3897 }
3898 public void unlockScreenOrientation(boolean immediate) {
3899 if (isRotationEnabled()) {
3900 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003901 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003902 } else {
3903 mHandler.postDelayed(new Runnable() {
3904 public void run() {
3905 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3906 }
3907 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003908 }
Winson Chung4b919f82012-05-01 10:44:08 -07003909 }
Winson Chung400438b2011-01-16 17:53:48 -08003910 }
3911
Winson Chung82f55532011-08-09 14:14:23 -07003912 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003913 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003914 if (DISABLE_CLINGS) {
3915 return false;
3916 }
3917
Brett Chabot2a9e2282011-08-23 15:03:13 -07003918 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003919 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003920
Michael Jurkae233a8b2013-03-19 13:49:20 +01003921 // Restricted secondary users (child mode) will potentially have very few apps
3922 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003923// boolean supportsLimitedUsers =
3924// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3925// Account[] accounts = AccountManager.get(this).getAccounts();
3926// if (supportsLimitedUsers && accounts.length == 0) {
3927// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3928// Bundle restrictions = um.getUserRestrictions();
3929// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3930// return false;
3931// }
3932// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003933 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003934 }
Michael Jurka22143132012-10-04 17:42:38 +02003935
Winson Chung7d7541e2011-09-16 20:14:36 -07003936 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003937 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003938 if (cling != null) {
3939 cling.init(this, positionData);
3940 cling.setVisibility(View.VISIBLE);
3941 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3942 if (animate) {
3943 cling.buildLayer();
3944 cling.setAlpha(0f);
3945 cling.animate()
3946 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003947 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003948 .setDuration(SHOW_CLING_DURATION)
3949 .setStartDelay(delay)
3950 .start();
3951 } else {
3952 cling.setAlpha(1f);
3953 }
Michael Jurka22143132012-10-04 17:42:38 +02003954 cling.setFocusableInTouchMode(true);
3955 cling.post(new Runnable() {
3956 public void run() {
3957 cling.setFocusable(true);
3958 cling.requestFocus();
3959 }
3960 });
3961 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3962 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003963 }
3964 return cling;
3965 }
Michael Jurka22143132012-10-04 17:42:38 +02003966
Winson Chung7d7541e2011-09-16 20:14:36 -07003967 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003968 // To catch cases where siblings of top-level views are made invisible, just check whether
3969 // the cling is directly set to GONE before dismissing it.
3970 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003971 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003972 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003973 anim.addListener(new AnimatorListenerAdapter() {
3974 public void onAnimationEnd(Animator animation) {
3975 cling.setVisibility(View.GONE);
3976 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003977 // We should update the shared preferences on a background thread
3978 new Thread("dismissClingThread") {
3979 public void run() {
3980 SharedPreferences.Editor editor = mSharedPrefs.edit();
3981 editor.putBoolean(flag, true);
3982 editor.commit();
3983 }
3984 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003985 };
3986 });
3987 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003988 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003989 }
3990 }
Michael Jurka22143132012-10-04 17:42:38 +02003991
Winson Chung9d9d74f2011-09-19 11:49:12 -07003992 private void removeCling(int id) {
3993 final View cling = findViewById(id);
3994 if (cling != null) {
3995 final ViewGroup parent = (ViewGroup) cling.getParent();
3996 parent.post(new Runnable() {
3997 @Override
3998 public void run() {
3999 parent.removeView(cling);
4000 }
4001 });
Michael Jurka22143132012-10-04 17:42:38 +02004002 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004003 }
4004 }
Michael Jurka974c3862012-05-22 22:00:31 -07004005
4006 private boolean skipCustomClingIfNoAccounts() {
4007 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4008 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4009 if (customCling) {
4010 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004011 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004012 Account[] accounts = am.getAccountsByType("com.google");
4013 return accounts.length == 0;
4014 }
4015 return false;
4016 }
4017
Winson Chung7d7541e2011-09-16 20:14:36 -07004018 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004019 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004020 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004021 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4022 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004023 // If we're not using the default workspace layout, replace workspace cling
4024 // with a custom workspace cling (usually specified in an overlay)
4025 // For now, only do this on tablets
4026 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004027 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004028 // Use a custom cling
4029 View cling = findViewById(R.id.workspace_cling);
4030 ViewGroup clingParent = (ViewGroup) cling.getParent();
4031 int clingIndex = clingParent.indexOfChild(cling);
4032 clingParent.removeViewAt(clingIndex);
4033 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4034 clingParent.addView(customCling, clingIndex);
4035 customCling.setId(R.id.workspace_cling);
4036 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004037 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004038 } else {
4039 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004040 }
4041 }
4042 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004043 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004044 if (isClingsEnabled() &&
4045 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004046 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004047 } else {
4048 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004049 }
4050 }
4051 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004052 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004053 if (isClingsEnabled() &&
4054 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4055 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004056 } else {
4057 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004058 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004059 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004060 }
4061 public boolean isFolderClingVisible() {
4062 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004063 if (cling != null) {
4064 return cling.getVisibility() == View.VISIBLE;
4065 }
4066 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004067 }
Winson Chung82f55532011-08-09 14:14:23 -07004068 public void dismissWorkspaceCling(View v) {
4069 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004070 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004071 }
4072 public void dismissAllAppsCling(View v) {
4073 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004074 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4075 }
4076 public void dismissFolderCling(View v) {
4077 Cling cling = (Cling) findViewById(R.id.folder_cling);
4078 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004079 }
4080
Winson Chung80baf5a2010-08-09 16:03:15 -07004081 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004082 * Prints out out state for debugging.
4083 */
4084 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004085 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004086 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004087 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4088 Log.d(TAG, "mRestoring=" + mRestoring);
4089 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4090 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004091 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004092 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004093
Winson Chung785d2eb2011-04-14 16:08:02 -07004094 if (mAppsCustomizeContent != null) {
4095 mAppsCustomizeContent.dumpState();
4096 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004097 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004098 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004099
4100 @Override
4101 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4102 super.dump(prefix, fd, writer, args);
4103 writer.println(" ");
4104 writer.println("Debug logs: ");
4105 for (int i = 0; i < sDumpLogs.size(); i++) {
4106 writer.println(" " + sDumpLogs.get(i));
4107 }
4108 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004109
4110 public static void dumpDebugLogsToConsole() {
4111 Log.d(TAG, "");
4112 Log.d(TAG, "*********************");
4113 Log.d(TAG, "Launcher debug logs: ");
4114 for (int i = 0; i < sDumpLogs.size(); i++) {
4115 Log.d(TAG, " " + sDumpLogs.get(i));
4116 }
4117 Log.d(TAG, "*********************");
4118 Log.d(TAG, "");
4119 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004120}
Michael Jurka2763be32011-02-24 11:19:57 -08004121
Michael Jurkaabded662011-03-04 12:06:57 -08004122interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004123 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004124 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004125 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004126 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004127 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004128}