blob: 3a9b2c19549f02636c2951665a044759fa59b659 [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;
196
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800197 private final BroadcastReceiver mCloseSystemDialogsReceiver
198 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800199 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private LayoutInflater mInflater;
202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800204 private View mQsbDivider;
205 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700206 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800207 private DragLayer mDragLayer;
208 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700209
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 private AppWidgetManager mAppWidgetManager;
211 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700212
Michael Jurkac9d95c52011-08-29 14:03:34 -0700213 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700214 private AppWidgetProviderInfo mPendingAddWidgetInfo;
215
Michael Jurka0280c3b2010-09-17 15:00:07 -0700216 private int[] mTmpAddItemCellCoordinates = new int[2];
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private FolderInfo mFolderInfo;
219
Winson Chung3d503fb2011-07-13 17:25:49 -0700220 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800221 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700222
Winson Chungc51db6a2011-10-05 11:44:49 -0700223 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700224 private AppsCustomizeTabHost mAppsCustomizeTabHost;
225 private AppsCustomizePagedView mAppsCustomizeContent;
226 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700229 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
230 // scroll issues (because the workspace may not have been measured yet) and extra work.
231 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
232 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
234 private SpannableStringBuilder mDefaultKeySsb = null;
235
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 private boolean mWorkspaceLoading = true;
237
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800238 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private boolean mRestoring;
240 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700241 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242
Michael Jurka7607c2f2013-04-03 14:33:19 -0700243 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
244
Winson Chung4a2afa32012-07-19 14:53:05 -0700245 // Keep track of whether the user has left launcher
246 private static boolean sPausedFromUserAction = false;
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private Bundle mSavedInstanceState;
249
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800251 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800252 private boolean mUserPresent = true;
253 private boolean mVisible = false;
254 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700255 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700257 private static LocaleConfiguration sLocaleConfiguration = null;
258
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700259 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700260
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700261 private Intent mAppMarketIntent = null;
262
Adam Cohended9f8d2010-11-03 13:25:16 -0700263 // Related to the auto-advancing of widgets
264 private final int ADVANCE_MSG = 1;
265 private final int mAdvanceInterval = 20000;
266 private final int mAdvanceStagger = 250;
267 private long mAutoAdvanceSentTime;
268 private long mAutoAdvanceTimeLeft = -1;
269 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
270 new HashMap<View, AppWidgetProviderInfo>();
271
Winson Chung400438b2011-01-16 17:53:48 -0800272 // Determines how long to wait after a rotation before restoring the screen orientation to
273 // match the sensor state.
274 private final int mRestoreScreenOrientationDelay = 500;
275
Michael Jurka4ef207b2010-11-29 17:05:45 -0800276 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700277 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
278 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
279 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800280
Craig Mautner360310b2012-10-26 15:13:08 -0700281 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700282
Adam Cohen1462de32012-07-24 22:34:36 -0700283 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
284
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700285 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
286
Winson Chung46353de2012-02-16 14:05:10 -0800287 // We only want to get the SharedPreferences once since it does an FS stat each time we get
288 // it from the context.
289 private SharedPreferences mSharedPrefs;
290
Adam Cohene25af792013-06-06 23:08:25 -0700291 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
292
Winson Chungf0c6ae02012-03-21 16:10:31 -0700293 // Holds the page that we need to animate to, and the icon views that we need to animate up
294 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700295 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700296 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700297 private ImageView mFolderIconImageView;
298 private Bitmap mFolderIconBitmap;
299 private Canvas mFolderIconCanvas;
300 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700301
Michael Jurkaddd62e92011-02-16 17:49:14 -0800302 private BubbleTextView mWaitingForResume;
303
Michael Jurka22143132012-10-04 17:42:38 +0200304 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
305 = new HideFromAccessibilityHelper();
306
Michael Jurkac1f5d262011-09-30 19:32:27 -0700307 private Runnable mBuildLayersRunnable = new Runnable() {
308 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700309 if (mWorkspace != null) {
310 mWorkspace.buildPageHardwareLayers();
311 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700312 }
313 };
314
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800315 private static ArrayList<PendingAddArguments> sPendingAddList
316 = new ArrayList<PendingAddArguments>();
317
Michael Jurka0a457bf2012-11-19 14:05:05 -0800318 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
319
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800320 private static class PendingAddArguments {
321 int requestCode;
322 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700323 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700324 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800325 int cellX;
326 int cellY;
327 }
328
Michael Jurka0a457bf2012-11-19 14:05:05 -0800329 private static boolean isPropertyEnabled(String propertyName) {
330 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700331 }
332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 @Override
334 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700335 if (DEBUG_STRICT_MODE) {
336 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
337 .detectDiskReads()
338 .detectDiskWrites()
339 .detectNetwork() // or .detectAll() for all detectable problems
340 .penaltyLog()
341 .build());
342 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
343 .detectLeakedSqlLiteObjects()
344 .detectLeakedClosableObjects()
345 .penaltyLog()
346 .penaltyDeath()
347 .build());
348 }
349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 super.onCreate(savedInstanceState);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400351 LauncherAppState app = LauncherAppState.getInstance();
352 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700353 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800354 mModel = app.setLauncher(this);
355 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400356 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700358
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700359 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700360 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
361 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700362
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700363 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
364 // this also ensures that any synchronous binding below doesn't re-trigger another
365 // LauncherModel load.
366 mPaused = false;
367
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200369 android.os.Debug.startMethodTracing(
370 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 }
372
373 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 setContentView(R.layout.launcher);
375 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700376 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800378 registerContentObservers();
379
Joe Onorato7c312c12009-08-13 21:36:53 -0700380 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 mSavedState = savedInstanceState;
383 restoreState(mSavedState);
384
Winson Chunga12a2502010-12-20 14:41:35 -0800385 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700386 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200387 mAppsCustomizeContent.onPackagesUpdated(
388 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700389 }
Winson Chunga12a2502010-12-20 14:41:35 -0800390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 if (PROFILE_STARTUP) {
392 android.os.Debug.stopMethodTracing();
393 }
394
395 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700396 if (sPausedFromUserAction) {
397 // If the user leaves launcher, then we should just load items asynchronously when
398 // they return.
399 mModel.startLoader(true, -1);
400 } else {
401 // We only load the page synchronously if the user rotates (or triggers a
402 // configuration change) while launcher is in the foreground
403 mModel.startLoader(true, mWorkspace.getCurrentPage());
404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 }
406
Michael Jurkac57b7a82011-08-09 22:02:20 -0700407 if (!mModel.isAllAppsLoaded()) {
408 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
409 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
410 }
411
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 // For handling default keys
413 mDefaultKeySsb = new SpannableStringBuilder();
414 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800415
416 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
417 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800418
Adam Cohenf9426d52012-06-04 17:26:21 -0700419 updateGlobalIcons();
420
421 // On large interfaces, we want the screen to auto-rotate based on the current orientation
422 unlockScreenOrientation(true);
423 }
424
Winson Chung4a2afa32012-07-19 14:53:05 -0700425 protected void onUserLeaveHint() {
426 super.onUserLeaveHint();
427 sPausedFromUserAction = true;
428 }
429
Adam Cohenf9426d52012-06-04 17:26:21 -0700430 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700431 boolean searchVisible = false;
432 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800433 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700434 int coi = getCurrentOrientationIndexForGlobalIcons();
435 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
436 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700437 updateAppMarketIcon();
438 searchVisible = updateGlobalSearchIcon();
439 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700440 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700441 if (sGlobalSearchIcon[coi] != null) {
442 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700443 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700444 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700445 if (sVoiceSearchIcon[coi] != null) {
446 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700447 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700448 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700449 if (sAppMarketIcon[coi] != null) {
450 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800451 }
Winson Chungadf0c182012-08-23 14:59:07 -0700452 if (mSearchDropTargetBar != null) {
453 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
Romain Guycbb89e42009-06-08 15:52:54 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700458 if (sLocaleConfiguration == null) {
459 new AsyncTask<Void, Void, LocaleConfiguration>() {
460 @Override
461 protected LocaleConfiguration doInBackground(Void... unused) {
462 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
463 readConfiguration(Launcher.this, localeConfiguration);
464 return localeConfiguration;
465 }
466
467 @Override
468 protected void onPostExecute(LocaleConfiguration result) {
469 sLocaleConfiguration = result;
470 checkForLocaleChange(); // recursive, but now with a locale configuration
471 }
472 }.execute();
473 return;
474 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700475
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 final Configuration configuration = getResources().getConfiguration();
477
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700478 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 final String locale = configuration.locale.toString();
480
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700481 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 final int mcc = configuration.mcc;
483
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 final int mnc = configuration.mnc;
486
Romain Guy84f296c2009-11-04 15:00:44 -0800487 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488
Romain Guy84f296c2009-11-04 15:00:44 -0800489 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700490 sLocaleConfiguration.locale = locale;
491 sLocaleConfiguration.mcc = mcc;
492 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700493
Joe Onorato0589f0f2010-02-08 13:44:00 -0800494 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495
496 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
497 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700498 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700499 public void run() {
500 writeConfiguration(Launcher.this, localeConfiguration);
501 }
502 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700503 }
504 }
505
506 private static class LocaleConfiguration {
507 public String locale;
508 public int mcc = -1;
509 public int mnc = -1;
510 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700511
Romain Guy98d01652009-06-30 16:21:04 -0700512 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
513 DataInputStream in = null;
514 try {
515 in = new DataInputStream(context.openFileInput(PREFERENCES));
516 configuration.locale = in.readUTF();
517 configuration.mcc = in.readInt();
518 configuration.mnc = in.readInt();
519 } catch (FileNotFoundException e) {
520 // Ignore
521 } catch (IOException e) {
522 // Ignore
523 } finally {
524 if (in != null) {
525 try {
526 in.close();
527 } catch (IOException e) {
528 // Ignore
529 }
530 }
531 }
532 }
533
534 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
535 DataOutputStream out = null;
536 try {
537 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
538 out.writeUTF(configuration.locale);
539 out.writeInt(configuration.mcc);
540 out.writeInt(configuration.mnc);
541 out.flush();
542 } catch (FileNotFoundException e) {
543 // Ignore
544 } catch (IOException e) {
545 //noinspection ResultOfMethodCallIgnored
546 context.getFileStreamPath(PREFERENCES).delete();
547 } finally {
548 if (out != null) {
549 try {
550 out.close();
551 } catch (IOException e) {
552 // Ignore
553 }
554 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 }
556 }
557
Bjorn Bringertc459e522013-06-07 19:36:01 +0100558 public LayoutInflater getInflater() {
559 return mInflater;
560 }
561
Adam Cohen716b51e2011-06-30 12:09:54 -0700562 public DragLayer getDragLayer() {
563 return mDragLayer;
564 }
565
Winson Chung36a62fe2012-05-06 18:04:42 -0700566 boolean isDraggingEnabled() {
567 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
568 // that is subsequently removed from the workspace in startBinding().
569 return !mModel.isLoadingWorkspace();
570 }
571
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 static int getScreen() {
573 synchronized (sLock) {
574 return sScreen;
575 }
576 }
577
578 static void setScreen(int screen) {
579 synchronized (sLock) {
580 sScreen = screen;
581 }
582 }
583
Winson Chung557d6ed2011-07-08 15:34:52 -0700584 /**
585 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
586 * a configuration step, this allows the proper animations to run after other transitions.
587 */
588 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700589 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800590 switch (args.requestCode) {
591 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700592 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700593 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800594 break;
595 case REQUEST_PICK_SHORTCUT:
596 processShortcut(args.intent);
597 break;
598 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700599 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700600 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700601 result = true;
602 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800603 case REQUEST_CREATE_APPWIDGET:
604 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700605 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700606 result = true;
607 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800608 case REQUEST_PICK_WALLPAPER:
609 // We just wanted the activity result here so we can clear mWaitingForResult
610 break;
611 }
Michael Jurka27614d22012-04-02 06:40:22 -0700612 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
613 // if you turned the screen off and then back while in All Apps, Launcher would not
614 // return to the workspace. Clearing mAddInfo.container here fixes this issue
615 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700616 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800617 }
618
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700620 protected void onActivityResult(
621 final int requestCode, final int resultCode, final Intent data) {
622 if (requestCode == REQUEST_BIND_APPWIDGET) {
623 int appWidgetId = data != null ?
624 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
625 if (resultCode == RESULT_CANCELED) {
626 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
627 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700628 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700629 }
630 return;
631 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700632 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800633 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
634 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700635 mWaitingForResult = false;
636
Adam Cohened66b2b2012-01-23 17:28:51 -0800637 // We have special handling for widgets
638 if (isWidgetDrop) {
639 int appWidgetId = data != null ?
640 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700641 if (appWidgetId < 0) {
642 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
643 "widget configuration activity.");
644 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
645 } else {
646 completeTwoStageWidgetDrop(resultCode, appWidgetId);
647 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800648 return;
649 }
650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 // The pattern used here is that a user PICKs a specific application,
652 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
655 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700656 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800657 final PendingAddArguments args = new PendingAddArguments();
658 args.requestCode = requestCode;
659 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700660 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700661 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700662 args.cellX = mPendingAddInfo.cellX;
663 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 if (isWorkspaceLocked()) {
665 sPendingAddList.add(args);
666 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700667 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800670 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700671 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
673 null);
674 }
675
676 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700677 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700678 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 Runnable onCompleteRunnable = null;
680 int animationType = 0;
681
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700682 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800683 if (resultCode == RESULT_OK) {
684 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
685 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700686 mPendingAddWidgetInfo);
687 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800688 onCompleteRunnable = new Runnable() {
689 @Override
690 public void run() {
691 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700692 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800693 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
694 null);
695 }
696 };
697 } else if (resultCode == RESULT_CANCELED) {
698 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
699 onCompleteRunnable = new Runnable() {
700 @Override
701 public void run() {
702 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
703 null);
704 }
705 };
706 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700707 if (mDragLayer.getAnimatedView() != null) {
708 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
709 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
710 animationType, boundWidget, true);
711 } else {
712 // The animated view may be null in the case of a rotation during widget configuration
713 onCompleteRunnable.run();
714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 }
716
717 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700718 protected void onStop() {
719 super.onStop();
720 FirstFrameAnimatorHelper.setIsVisible(false);
721 }
722
723 @Override
724 protected void onStart() {
725 super.onStart();
726 FirstFrameAnimatorHelper.setIsVisible(true);
727 }
728
729 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200731 long startTime = 0;
732 if (DEBUG_RESUME_TIME) {
733 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400734 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700737
Winson Chung4a2afa32012-07-19 14:53:05 -0700738 // Restore the previous launcher state
739 if (mOnResumeState == State.WORKSPACE) {
740 showWorkspace(false);
741 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
742 showAllApps(false);
743 }
744 mOnResumeState = State.NONE;
745
Craig Mautner360310b2012-10-26 15:13:08 -0700746 // Background was set to gradient in onPause(), restore to black if in all apps.
747 setWorkspaceBackground(mState == State.WORKSPACE);
748
Winson Chungde0fb8f2012-05-08 14:37:08 -0700749 // Process any items that were added while Launcher was away
750 InstallShortcutReceiver.flushInstallQueue(this);
751
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800752 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700753 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700754 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400755 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700756 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700758 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200760 if (mOnResumeCallbacks.size() > 0) {
761 // We might have postponed some bind calls until onResume (see waitUntilResume) --
762 // execute them here
763 long startTimeCallbacks = 0;
764 if (DEBUG_RESUME_TIME) {
765 startTimeCallbacks = System.currentTimeMillis();
766 }
767
768 if (mAppsCustomizeContent != null) {
769 mAppsCustomizeContent.setBulkBind(true);
770 }
771 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
772 mOnResumeCallbacks.get(i).run();
773 }
774 if (mAppsCustomizeContent != null) {
775 mAppsCustomizeContent.setBulkBind(false);
776 }
777 mOnResumeCallbacks.clear();
778 if (DEBUG_RESUME_TIME) {
779 Log.d(TAG, "Time spent processing callbacks in onResume: " +
780 (System.currentTimeMillis() - startTimeCallbacks));
781 }
Michael Jurka447bf842013-05-15 14:52:15 +0200782 }
Winson Chunge4e50662012-01-23 14:45:13 -0800783
784 // Reset the pressed state of icons that were locked in the press state while activities
785 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800786 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800787 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800788 mWaitingForResume.setStayPressed(false);
789 }
Winson Chunge4e50662012-01-23 14:45:13 -0800790 if (mAppsCustomizeContent != null) {
791 // Resets the previous all apps icon press state
792 mAppsCustomizeContent.resetDrawableState();
793 }
Adam Cohen06dff352012-06-01 17:17:08 -0700794 // It is possible that widgets can receive updates while launcher is not in the foreground.
795 // Consequently, the widgets will be inflated in the orientation of the foreground activity
796 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
797 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700798 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700799
800 // Again, as with the above scenario, it's possible that one or more of the global icons
801 // were updated in the wrong orientation.
802 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200803 if (DEBUG_RESUME_TIME) {
804 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
805 }
Adam Cohen06dff352012-06-01 17:17:08 -0700806 }
807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700809 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700810 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
811 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
812 // when Launcher resumes and we are still in AllApps.
813 updateWallpaperVisibility(true);
814
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700815 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700816 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800817 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700818 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700819 }
Romain Guycbb89e42009-06-08 15:52:54 -0700820
Adam Cohen66a01fd2013-06-11 12:48:00 -0700821 protected void onFinishBindingItems() {
822 }
823
824 // Add a fullscreen unpadded view to the workspace to the left all other screens.
825 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700826 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700827 }
828
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700829 @Override
830 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400831 // Flag the loader to stop early before switching
832 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700833 if (mAppsCustomizeContent != null) {
834 mAppsCustomizeContent.surrender();
835 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800836 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700837 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700838
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800839 // We can't hide the IME if it was forced open. So don't bother
840 /*
841 @Override
842 public void onWindowFocusChanged(boolean hasFocus) {
843 super.onWindowFocusChanged(hasFocus);
844
845 if (hasFocus) {
846 final InputMethodManager inputManager = (InputMethodManager)
847 getSystemService(Context.INPUT_METHOD_SERVICE);
848 WindowManager.LayoutParams lp = getWindow().getAttributes();
849 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
850 android.os.Handler()) {
851 protected void onReceiveResult(int resultCode, Bundle resultData) {
852 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
853 }
854 });
855 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
856 }
857 }
858 */
859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 private boolean acceptFilter() {
861 final InputMethodManager inputManager = (InputMethodManager)
862 getSystemService(Context.INPUT_METHOD_SERVICE);
863 return !inputManager.isFullscreenMode();
864 }
865
866 @Override
867 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700868 final int uniChar = event.getUnicodeChar();
869 final boolean handled = super.onKeyDown(keyCode, event);
870 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
871 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
873 keyCode, event);
874 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700875 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700876 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700877 // showSearchDialog()
878 // If there are multiple keystrokes before the search dialog takes focus,
879 // onSearchRequested() will be called for every keystroke,
880 // but it is idempotent, so it's fine.
881 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 }
883 }
884
Joe Onorato8a9625e2010-01-28 15:55:35 -0800885 // Eat the long press event so the keyboard doesn't come up.
886 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
887 return true;
888 }
889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 return handled;
891 }
892
Karl Rosaen138a0412009-04-23 19:00:21 -0700893 private String getTypedText() {
894 return mDefaultKeySsb.toString();
895 }
896
897 private void clearTypedText() {
898 mDefaultKeySsb.clear();
899 mDefaultKeySsb.clearSpans();
900 Selection.setSelection(mDefaultKeySsb, 0);
901 }
902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700904 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
905 * State
906 */
907 private static State intToState(int stateOrdinal) {
908 State state = State.WORKSPACE;
909 final State[] stateValues = State.values();
910 for (int i = 0; i < stateValues.length; i++) {
911 if (stateValues[i].ordinal() == stateOrdinal) {
912 state = stateValues[i];
913 break;
914 }
915 }
916 return state;
917 }
918
919 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 * Restores the previous state, if it exists.
921 *
922 * @param savedState The previous state.
923 */
924 private void restoreState(Bundle savedState) {
925 if (savedState == null) {
926 return;
927 }
928
Michael Jurka883f55b2010-10-21 15:47:14 -0700929 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700930 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700931 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800932 }
933
Winson Chungf0c6ae02012-03-21 16:10:31 -0700934 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700936 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700940 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700941
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
943 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700944 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
946 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700947 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
948 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
949 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700950 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 mRestoring = true;
952 }
953
954 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
955 if (renameFolder) {
956 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700957 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 mRestoring = true;
959 }
Winson Chunga12a2502010-12-20 14:41:35 -0800960
Winson Chung785d2eb2011-04-14 16:08:02 -0700961 // Restore the AppsCustomize tab
962 if (mAppsCustomizeTabHost != null) {
963 String curTab = savedState.getString("apps_customize_currentTab");
964 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700965 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700966 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700967 mAppsCustomizeContent.loadAssociatedPages(
968 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700969 }
970
Winson Chung5afbf7b2011-07-25 11:53:08 -0700971 int currentIndex = savedState.getInt("apps_customize_currentIndex");
972 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 }
975
976 /**
977 * Finds all the views we need and configure them properly.
978 */
979 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700980 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400981
Craig Mautner360310b2012-10-26 15:13:08 -0700982 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700983 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
984 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700985 mQsbDivider = findViewById(R.id.qsb_divider);
986 mDockDivider = findViewById(R.id.dock_divider);
987
988 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
989 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990
Winson Chung4c98d922011-05-31 16:50:48 -0700991 // Setup the drag layer
992 mDragLayer.setup(this, dragController);
993
Winson Chung3d503fb2011-07-13 17:25:49 -0700994 // Setup the hotseat
995 mHotseat = (Hotseat) findViewById(R.id.hotseat);
996 if (mHotseat != null) {
997 mHotseat.setup(this);
998 }
999
Winson Chung4c98d922011-05-31 16:50:48 -07001000 // Setup the workspace
1001 mWorkspace.setHapticFeedbackEnabled(false);
1002 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001003 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001004 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001005
Winson Chungf0ea4d32011-06-06 14:27:16 -07001006 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001007 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001008
Winson Chungf0ea4d32011-06-06 14:27:16 -07001009 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001010 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001011 mAppsCustomizeContent = (AppsCustomizePagedView)
1012 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1013 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001014
Winson Chung3d503fb2011-07-13 17:25:49 -07001015 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001016 dragController.setDragScoller(mWorkspace);
1017 dragController.setScrollView(mDragLayer);
1018 dragController.setMoveTarget(mWorkspace);
1019 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001020 if (mSearchDropTargetBar != null) {
1021 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001022 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001023
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001024 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001025 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001026 final View ww = new WeightWatcher(this);
1027 ww.setAlpha(0.5f);
1028 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001029 new FrameLayout.LayoutParams(
1030 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001031 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001032 Gravity.BOTTOM)
1033 );
1034 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
1036
1037 /**
1038 * Creates a view representing a shortcut.
1039 *
1040 * @param info The data structure describing the shortcut.
1041 *
1042 * @return A View inflated from R.layout.application.
1043 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001044 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001046 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 }
1048
1049 /**
1050 * Creates a view representing a shortcut inflated from the specified resource.
1051 *
1052 * @param layoutResId The id of the XML layout used to create the shortcut.
1053 * @param parent The group the shortcut belongs to.
1054 * @param info The data structure describing the shortcut.
1055 *
1056 * @return A View inflated from layoutResId.
1057 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001058 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001059 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1060 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 return favorite;
1063 }
1064
1065 /**
1066 * Add an application shortcut to the workspace.
1067 *
1068 * @param data The intent describing the application.
1069 * @param cellInfo The position on screen where to create the shortcut.
1070 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001071 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001072 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001073 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001074
Adam Cohenfbba09b2011-07-18 21:43:05 -07001075 // First we check if we already know the exact location where we want to add this item.
1076 if (cellX >= 0 && cellY >= 0) {
1077 cellXY[0] = cellX;
1078 cellXY[1] = cellY;
1079 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001080 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001081 return;
1082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001084 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001085
Romain Guy73b979d2009-06-09 12:57:21 -07001086 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001087 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001089 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001090 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001091 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001092 } else {
1093 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
1095 }
Romain Guycbb89e42009-06-08 15:52:54 -07001096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 /**
1098 * Add a shortcut to the workspace.
1099 *
1100 * @param data The intent describing the shortcut.
1101 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001103 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001104 int cellY) {
1105 int[] cellXY = mTmpAddItemCellCoordinates;
1106 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001107 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Michael Jurkad3ef3062010-11-23 16:23:58 -08001109 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001110
Adam Cohen558baaf2011-08-15 15:22:57 -07001111 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001112 if (info == null) {
1113 return;
1114 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001115 final View view = createShortcut(info);
1116
Adam Cohenfbba09b2011-07-18 21:43:05 -07001117 // First we check if we already know the exact location where we want to add this item.
1118 if (cellX >= 0 && cellY >= 0) {
1119 cellXY[0] = cellX;
1120 cellXY[1] = cellY;
1121 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001122
1123 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001124 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001125 true, null,null)) {
1126 return;
1127 }
1128 DragObject dragObject = new DragObject();
1129 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001130 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1131 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001132 return;
1133 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001134 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001135 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001136 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001137 foundCellSpan = (result != null);
1138 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001139 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001140 }
1141
1142 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001143 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001144 return;
1145 }
1146
Adam Cohendcd297f2013-06-18 13:13:40 -07001147 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148
1149 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001150 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001151 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
1153 }
1154
Adam Cohen2f093b62012-04-30 18:59:53 -07001155 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1156 int minHeight) {
1157 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001158 // We want to account for the extra amount of padding that we are adding to the widget
1159 // to ensure that it gets the full amount of space that it has requested
1160 int requiredWidth = minWidth + padding.left + padding.right;
1161 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001162 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001163 }
1164
Adam Cohen2f093b62012-04-30 18:59:53 -07001165 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1166 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001167 }
1168
Adam Cohen2f093b62012-04-30 18:59:53 -07001169 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1170 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001171 }
1172
Adam Cohen2f093b62012-04-30 18:59:53 -07001173 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1174 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001175 }
1176
Adam Cohen2f093b62012-04-30 18:59:53 -07001177 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1178 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001179 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001180 }
1181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001185 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001186 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001188 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001189 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1190 if (appWidgetInfo == null) {
1191 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1192 }
Romain Guycbb89e42009-06-08 15:52:54 -07001193
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001194 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001195 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001196
Adam Cohen2f093b62012-04-30 18:59:53 -07001197 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1198 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001199
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001201 // We have saved the position to which the widget was dragged-- this really only matters
1202 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001203 int[] cellXY = mTmpAddItemCellCoordinates;
1204 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001205 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001206 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001207 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1208 cellXY[0] = mPendingAddInfo.cellX;
1209 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001210 spanXY[0] = mPendingAddInfo.spanX;
1211 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001212 foundCellSpan = true;
1213 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001215 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001216 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1217 spanXY[1], cellXY, finalSpan);
1218 spanXY[0] = finalSpan[0];
1219 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001220 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001222 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001223 }
1224
Michael Jurka0280c3b2010-09-17 15:00:07 -07001225 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001226 if (appWidgetId != -1) {
1227 // Deleting an app widget ID is a void call but writes to disk before returning
1228 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001229 new Thread("deleteAppWidgetId") {
1230 public void run() {
1231 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1232 }
1233 }.start();
1234 }
Winson Chung93eef082012-03-23 15:59:27 -07001235 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001236 return;
1237 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001239 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001240 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1241 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 launcherInfo.spanX = spanXY[0];
1243 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001244 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1245 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001248 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249
1250 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001251 if (hostView == null) {
1252 // Perform actual inflation because we're live
1253 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1254 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1255 } else {
1256 // The AppWidgetHostView has already been inflated and instantiated
1257 launcherInfo.hostView = hostView;
1258 }
Romain Guycbb89e42009-06-08 15:52:54 -07001259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001261 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001262 launcherInfo.notifyWidgetSizeChanged(this);
1263
Adam Cohendcd297f2013-06-18 13:13:40 -07001264 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001265 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001266
1267 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001269 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
Romain Guycbb89e42009-06-08 15:52:54 -07001271
Adam Cohended9f8d2010-11-03 13:25:16 -07001272 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1273 @Override
1274 public void onReceive(Context context, Intent intent) {
1275 final String action = intent.getAction();
1276 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1277 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001278 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001279 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001280
Winson Chungc100e8e2011-08-09 16:02:43 -07001281 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001282 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001283 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1284 mAppsCustomizeTabHost.reset();
1285 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001286 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001287 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1288 mUserPresent = true;
1289 updateRunning();
1290 }
1291 }
1292 };
1293
1294 @Override
1295 public void onAttachedToWindow() {
1296 super.onAttachedToWindow();
1297
1298 // Listen for broadcasts related to user-presence
1299 final IntentFilter filter = new IntentFilter();
1300 filter.addAction(Intent.ACTION_SCREEN_OFF);
1301 filter.addAction(Intent.ACTION_USER_PRESENT);
1302 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001303 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001304 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001305 mVisible = true;
1306 }
1307
1308 @Override
1309 public void onDetachedFromWindow() {
1310 super.onDetachedFromWindow();
1311 mVisible = false;
1312
Adam Cohend113e0c2010-11-11 10:48:05 -08001313 if (mAttached) {
1314 unregisterReceiver(mReceiver);
1315 mAttached = false;
1316 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001317 updateRunning();
1318 }
1319
1320 public void onWindowVisibilityChanged(int visibility) {
1321 mVisible = visibility == View.VISIBLE;
1322 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001323 // The following code used to be in onResume, but it turns out onResume is called when
1324 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1325 // is a more appropriate event to handle
1326 if (mVisible) {
1327 mAppsCustomizeTabHost.onWindowVisible();
1328 if (!mWorkspaceLoading) {
1329 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001330 // We want to let Launcher draw itself at least once before we force it to build
1331 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001332 // apps is nice and speedy.
1333 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001334 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001335 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001336 if (mStarted) return;
1337 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001338 // We delay the layer building a bit in order to give
1339 // other message processing a time to run. In particular
1340 // this avoids a delay in hiding the IME if it was
1341 // currently shown, because doing that may involve
1342 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001343 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001344 final ViewTreeObserver.OnDrawListener listener = this;
1345 mWorkspace.post(new Runnable() {
1346 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001347 if (mWorkspace != null &&
1348 mWorkspace.getViewTreeObserver() != null) {
1349 mWorkspace.getViewTreeObserver().
1350 removeOnDrawListener(listener);
1351 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001352 }
1353 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001354 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001355 }
1356 });
1357 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001358 // When Launcher comes back to foreground, a different Activity might be responsible for
1359 // the app market intent, so refresh the icon
1360 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001361 clearTypedText();
1362 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001363 }
1364
1365 private void sendAdvanceMessage(long delay) {
1366 mHandler.removeMessages(ADVANCE_MSG);
1367 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1368 mHandler.sendMessageDelayed(msg, delay);
1369 mAutoAdvanceSentTime = System.currentTimeMillis();
1370 }
1371
1372 private void updateRunning() {
1373 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1374 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1375 mAutoAdvanceRunning = autoAdvanceRunning;
1376 if (autoAdvanceRunning) {
1377 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1378 sendAdvanceMessage(delay);
1379 } else {
1380 if (!mWidgetsToAdvance.isEmpty()) {
1381 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1382 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1383 }
1384 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001385 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001386 }
1387 }
1388 }
1389
1390 private final Handler mHandler = new Handler() {
1391 @Override
1392 public void handleMessage(Message msg) {
1393 if (msg.what == ADVANCE_MSG) {
1394 int i = 0;
1395 for (View key: mWidgetsToAdvance.keySet()) {
1396 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1397 final int delay = mAdvanceStagger * i;
1398 if (v instanceof Advanceable) {
1399 postDelayed(new Runnable() {
1400 public void run() {
1401 ((Advanceable) v).advance();
1402 }
1403 }, delay);
1404 }
1405 i++;
1406 }
1407 sendAdvanceMessage(mAdvanceInterval);
1408 }
1409 }
1410 };
1411
1412 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001413 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001414 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1415 if (v instanceof Advanceable) {
1416 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001417 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001418 updateRunning();
1419 }
1420 }
1421
1422 void removeWidgetToAutoAdvance(View hostView) {
1423 if (mWidgetsToAdvance.containsKey(hostView)) {
1424 mWidgetsToAdvance.remove(hostView);
1425 updateRunning();
1426 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001427 }
1428
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001430 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001431 launcherInfo.hostView = null;
1432 }
1433
Winson Chung93eef082012-03-23 15:59:27 -07001434 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1435 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1436 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001437 }
1438
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001439 public LauncherAppWidgetHost getAppWidgetHost() {
1440 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
Romain Guycbb89e42009-06-08 15:52:54 -07001442
Winson Chunga9abd0e2010-10-27 17:18:37 -07001443 public LauncherModel getModel() {
1444 return mModel;
1445 }
1446
Bjorn Bringertc459e522013-06-07 19:36:01 +01001447 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001448 getWindow().closeAllPanels();
1449
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001450 // Whatever we were doing is hereby canceled.
1451 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001452 }
1453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 @Override
1455 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001456 long startTime = 0;
1457 if (DEBUG_RESUME_TIME) {
1458 startTime = System.currentTimeMillis();
1459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 super.onNewIntent(intent);
1461
1462 // Close the menu
1463 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001464 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001465 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001466
Jamie Genniscb222e82012-10-23 15:44:26 -07001467 final boolean alreadyOnHome =
1468 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001469 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001470
Jamie Genniscb222e82012-10-23 15:44:26 -07001471 Runnable processIntent = new Runnable() {
1472 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001473 if (mWorkspace == null) {
1474 // Can be cases where mWorkspace is null, this prevents a NPE
1475 return;
1476 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001477 Folder openFolder = mWorkspace.getOpenFolder();
1478 // In all these cases, only animate if we're already on home
1479 mWorkspace.exitWidgetResizeMode();
1480 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1481 openFolder == null) {
1482 mWorkspace.moveToDefaultScreen(true);
1483 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001484
Jamie Genniscb222e82012-10-23 15:44:26 -07001485 closeFolder();
1486 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001487
Jamie Genniscb222e82012-10-23 15:44:26 -07001488 // If we are already on home, then just animate back to the workspace,
1489 // otherwise, just wait until onResume to set the state back to Workspace
1490 if (alreadyOnHome) {
1491 showWorkspace(true);
1492 } else {
1493 mOnResumeState = State.WORKSPACE;
1494 }
1495
1496 final View v = getWindow().peekDecorView();
1497 if (v != null && v.getWindowToken() != null) {
1498 InputMethodManager imm = (InputMethodManager)getSystemService(
1499 INPUT_METHOD_SERVICE);
1500 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1501 }
1502
1503 // Reset AllApps to its initial state
1504 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1505 mAppsCustomizeTabHost.reset();
1506 }
1507 }
1508 };
1509
1510 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1511 // Delay processing of the intent to allow the status bar animation to finish
1512 // first in order to avoid janky animations.
1513 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001514 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001515 // Process the intent immediately.
1516 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001517 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
Michael Jurka447bf842013-05-15 14:52:15 +02001520 if (DEBUG_RESUME_TIME) {
1521 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
1524
1525 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001526 public void onRestoreInstanceState(Bundle state) {
1527 super.onRestoreInstanceState(state);
1528 for (int page: mSynchronouslyBoundPages) {
1529 mWorkspace.restoreInstanceStateForChild(page);
1530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
1532
1533 @Override
1534 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001535 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001536 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537
Michael Jurka883f55b2010-10-21 15:47:14 -07001538 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001539 // We close any open folder since it will not be re-opened, and we need to make sure
1540 // this state is reflected.
1541 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
Adam Cohendcd297f2013-06-18 13:13:40 -07001543 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001544 mWaitingForResult) {
1545 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1548 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001549 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1550 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1551 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
1553
1554 if (mFolderInfo != null && mWaitingForResult) {
1555 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1556 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1557 }
Michael Jurka946ad472010-07-09 18:05:18 -07001558
Winson Chung785d2eb2011-04-14 16:08:02 -07001559 // Save the current AppsCustomize tab
1560 if (mAppsCustomizeTabHost != null) {
1561 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1562 if (currentTabTag != null) {
1563 outState.putString("apps_customize_currentTab", currentTabTag);
1564 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001565 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1566 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
1569
1570 @Override
1571 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001573
Winson Chunge7a03942011-08-05 15:05:12 -07001574 // Remove all pending runnables
1575 mHandler.removeMessages(ADVANCE_MSG);
1576 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001577 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001578
Winson Chungcd2b0142011-06-08 16:02:26 -07001579 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001580 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001581 mModel.stopLoader();
1582 app.setLauncher(null);
1583
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001585 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001587 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001589 mAppWidgetHost = null;
1590
1591 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592
1593 TextKeyListener.getInstance().release();
1594
Winson Chung81b52252012-08-27 15:34:29 -07001595 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1596 // to prevent leaking Launcher activities on orientation change.
1597 if (mModel != null) {
1598 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1599 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001600
1601 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001602 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001603
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001604 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001605 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1606 mWorkspace.removeAllViews();
1607 mWorkspace = null;
1608 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001609
Michael Jurka2ecf9952012-06-18 12:52:28 -07001610 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 }
1612
Adam Cohena9cf38f2011-05-02 15:36:58 -07001613 public DragController getDragController() {
1614 return mDragController;
1615 }
1616
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 @Override
1618 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001619 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 super.startActivityForResult(intent, requestCode);
1621 }
1622
Winson Chung70d72102011-08-12 11:24:35 -07001623 /**
1624 * Indicates that we want global search for this activity by setting the globalSearch
1625 * argument for {@link #startSearch} to true.
1626 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001628 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001630
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001631 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001632
Karl Rosaen138a0412009-04-23 19:00:21 -07001633 if (initialQuery == null) {
1634 // Use any text typed in the launcher as the initial query
1635 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 if (appSearchData == null) {
1638 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001639 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
Winson Chungadf0c182012-08-23 14:59:07 -07001641 Rect sourceBounds = new Rect();
1642 if (mSearchDropTargetBar != null) {
1643 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1644 }
Romain Guycbb89e42009-06-08 15:52:54 -07001645
Bjorn Bringertc459e522013-06-07 19:36:01 +01001646 startSearch(initialQuery, selectInitialQuery,
1647 appSearchData, sourceBounds);
1648 }
1649
1650 public void startSearch(String initialQuery,
1651 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001652 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001653 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001654 }
1655
1656 /**
1657 * Starts the global search activity. This code is a copied from SearchManager
1658 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001659 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001660 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001661 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001662 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1663 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1664 if (globalSearchActivity == null) {
1665 Log.w(TAG, "No global search activity found.");
1666 return;
1667 }
1668 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1669 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1670 intent.setComponent(globalSearchActivity);
1671 // Make sure that we have a Bundle to put source in
1672 if (appSearchData == null) {
1673 appSearchData = new Bundle();
1674 } else {
1675 appSearchData = new Bundle(appSearchData);
1676 }
1677 // Set source to package name of app that starts global search, if not set already.
1678 if (!appSearchData.containsKey("source")) {
1679 appSearchData.putString("source", getPackageName());
1680 }
1681 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1682 if (!TextUtils.isEmpty(initialQuery)) {
1683 intent.putExtra(SearchManager.QUERY, initialQuery);
1684 }
1685 if (selectInitialQuery) {
1686 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1687 }
1688 intent.setSourceBounds(sourceBounds);
1689 try {
1690 startActivity(intent);
1691 } catch (ActivityNotFoundException ex) {
1692 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 }
1695
Winson Chung70d72102011-08-12 11:24:35 -07001696 @Override
1697 public boolean onCreateOptionsMenu(Menu menu) {
1698 if (isWorkspaceLocked()) {
1699 return false;
1700 }
1701
1702 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001703
1704 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1705 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1706 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001707 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1708 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1709 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001710 String helpUrl = getString(R.string.help_url);
1711 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001712 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1713 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1714
Winson Chung70d72102011-08-12 11:24:35 -07001715 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1716 .setIcon(android.R.drawable.ic_menu_gallery)
1717 .setAlphabeticShortcut('W');
1718 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1719 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001720 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001721 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001722 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1723 .setIcon(android.R.drawable.ic_menu_preferences)
1724 .setIntent(settings)
1725 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001726 if (!helpUrl.isEmpty()) {
1727 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1728 .setIcon(android.R.drawable.ic_menu_help)
1729 .setIntent(help)
1730 .setAlphabeticShortcut('H');
1731 }
Winson Chung70d72102011-08-12 11:24:35 -07001732 return true;
1733 }
1734
1735 @Override
1736 public boolean onPrepareOptionsMenu(Menu menu) {
1737 super.onPrepareOptionsMenu(menu);
1738
1739 if (mAppsCustomizeTabHost.isTransitioning()) {
1740 return false;
1741 }
1742 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1743 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1744
1745 return true;
1746 }
1747
1748 @Override
1749 public boolean onOptionsItemSelected(MenuItem item) {
1750 switch (item.getItemId()) {
1751 case MENU_WALLPAPER_SETTINGS:
1752 startWallpaper();
1753 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001754 }
1755
1756 return super.onOptionsItemSelected(item);
1757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001759 @Override
1760 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001761 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001762 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001763 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001764 }
1765
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766 public boolean isWorkspaceLocked() {
1767 return mWorkspaceLoading || mWaitingForResult;
1768 }
1769
Michael Jurka0280c3b2010-09-17 15:00:07 -07001770 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001771 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001772 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001773 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1774 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001775 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001776 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001777 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001778
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001779 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1780 AppWidgetProviderInfo appWidgetInfo) {
1781 if (appWidgetInfo.configure != null) {
1782 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001783
Bjorn Bringert7984c942009-12-09 15:38:25 +00001784 // Launch over to configure widget, if needed
1785 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001786 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001787 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001788 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001790 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001791 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001792 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001793 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001794 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 }
1796 }
Romain Guycbb89e42009-06-08 15:52:54 -07001797
Adam Cohenfbba09b2011-07-18 21:43:05 -07001798 /**
1799 * Process a shortcut drop.
1800 *
1801 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001802 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001803 * @param cell The cell it should be added to, optional
1804 * @param position The location on the screen where it was dropped, optional
1805 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001806 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001807 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001808 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001809 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001810 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001811 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001812
1813 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001814 mPendingAddInfo.cellX = cell[0];
1815 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001816 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817
1818 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1819 createShortcutIntent.setComponent(componentName);
1820 processShortcut(createShortcutIntent);
1821 }
1822
Adam Cohenfbba09b2011-07-18 21:43:05 -07001823 /**
1824 * Process a widget drop.
1825 *
1826 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001827 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001828 * @param cell The cell it should be added to, optional
1829 * @param position The location on the screen where it was dropped, optional
1830 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001831 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001832 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001833 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001834 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001835 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001836 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001837 mPendingAddInfo.minSpanX = info.minSpanX;
1838 mPendingAddInfo.minSpanY = info.minSpanY;
1839
Adam Cohenfbba09b2011-07-18 21:43:05 -07001840 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001841 mPendingAddInfo.cellX = cell[0];
1842 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001843 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001844 if (span != null) {
1845 mPendingAddInfo.spanX = span[0];
1846 mPendingAddInfo.spanY = span[1];
1847 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001848
Adam Cohened66b2b2012-01-23 17:28:51 -08001849 AppWidgetHostView hostView = info.boundWidget;
1850 int appWidgetId;
1851 if (hostView != null) {
1852 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001853 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001854 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001855 // In this case, we either need to start an activity to get permission to bind
1856 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001857 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001858 Bundle options = info.bindOptions;
1859
1860 boolean success = false;
1861 if (options != null) {
1862 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1863 info.componentName, options);
1864 } else {
1865 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1866 info.componentName);
1867 }
1868 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001869 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001870 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001871 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001872 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1873 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1874 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001875 // TODO: we need to make sure that this accounts for the options bundle.
1876 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001877 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1878 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001879 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001880 }
1881
Joe Onoratodeb98af2010-02-19 14:59:39 -08001882 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001883 // Handle case where user selected "Applications"
1884 String applicationName = getResources().getString(R.string.group_applications);
1885 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001886
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001887 if (applicationName != null && applicationName.equals(shortcutName)) {
1888 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1889 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001890
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001891 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1892 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001893 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001894 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001895 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001896 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
1899
Winson Chung24ab2f12010-09-16 14:10:47 -07001900 void processWallpaper(Intent intent) {
1901 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1902 }
1903
Adam Cohendcd297f2013-06-18 13:13:40 -07001904 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001905 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001906 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 folderInfo.title = getText(R.string.folder_name);
1908
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001910 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001911 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001912 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913
1914 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001915 FolderIcon newFolder =
1916 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001917 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001918 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001919 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
Romain Guycbb89e42009-06-08 15:52:54 -07001921
Joe Onorato9c1289c2009-08-17 11:03:03 -04001922 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001923 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 }
1925
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001927 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001929 Intent chooser = Intent.createChooser(pickWallpaper,
1930 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001931 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1932 // Removed in Eclair MR1
1933// WallpaperManager wm = (WallpaperManager)
1934// getSystemService(Context.WALLPAPER_SERVICE);
1935// WallpaperInfo wi = wm.getWallpaperInfo();
1936// if (wi != null && wi.getSettingsActivity() != null) {
1937// LabeledIntent li = new LabeledIntent(getPackageName(),
1938// R.string.configure_wallpaper, 0);
1939// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1940// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1941// }
Mike Clerona0618e42009-10-22 13:55:21 -07001942 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 }
1944
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001945 /**
1946 * Registers various content observers. The current implementation registers
1947 * only a favorites observer to keep track of the favorites applications.
1948 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001949 private void registerContentObservers() {
1950 ContentResolver resolver = getContentResolver();
1951 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1952 true, mWidgetObserver);
1953 }
1954
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 @Override
1956 public boolean dispatchKeyEvent(KeyEvent event) {
1957 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1958 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001960 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001961 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001962 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001963 dumpState();
1964 return true;
1965 }
1966 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001967 }
1968 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1969 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001970 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 return true;
1972 }
1973 }
1974
1975 return super.dispatchKeyEvent(event);
1976 }
1977
Joe Onorato88ec0992009-11-19 13:16:06 -08001978 @Override
1979 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001980 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001981 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001982 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001983 Folder openFolder = mWorkspace.getOpenFolder();
1984 if (openFolder.isEditingName()) {
1985 openFolder.dismissEditingName();
1986 } else {
1987 closeFolder();
1988 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001989 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001990 mWorkspace.exitWidgetResizeMode();
1991
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001992 // Back button is a no-op here, but give at least some feedback for the button press
1993 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001994 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001995 }
1996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001998 * Re-listen when widgets are reset.
1999 */
2000 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002001 if (mAppWidgetHost != null) {
2002 mAppWidgetHost.startListening();
2003 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002004 }
2005
2006 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 * Launches the intent referred by the clicked shortcut.
2008 *
2009 * @param v The view representing the clicked shortcut.
2010 */
2011 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002012 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2013 // view has detached (it's possible for this to happen if the view is removed mid touch).
2014 if (v.getWindowToken() == null) {
2015 return;
2016 }
2017
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002018 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002019 return;
2020 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002023 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002025 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002026
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002027 // Check for special shortcuts
2028 if (intent.getComponent() != null) {
2029 final String shortcutClass = intent.getComponent().getClassName();
2030
2031 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2032 showAllApps(true);
2033 return;
2034 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2035 MemoryDumpActivity.startDump(this);
2036 return;
2037 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002038 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002039
2040 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002041 int[] pos = new int[2];
2042 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002043 intent.setSourceBounds(new Rect(pos[0], pos[1],
2044 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002045
2046 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002047
2048 if (success && v instanceof BubbleTextView) {
2049 mWaitingForResume = (BubbleTextView) v;
2050 mWaitingForResume.setStayPressed(true);
2051 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002053 if (v instanceof FolderIcon) {
2054 FolderIcon fi = (FolderIcon) v;
2055 handleFolderClick(fi);
2056 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002057 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002058 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002059 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002060 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002061 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 }
2064 }
2065
Michael Jurka0e260592010-06-30 17:07:39 -07002066 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002067 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002068 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002069 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2070 showWorkspace(true);
2071 }
Michael Jurka0e260592010-06-30 17:07:39 -07002072 return false;
2073 }
2074
Michael Jurkaaf442092010-06-10 17:01:57 -07002075 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002076 * Event handler for the search button
2077 *
2078 * @param v The view that was clicked.
2079 */
2080 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002081 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2082
Amith Yamasania135ba82011-08-09 17:42:01 -07002083 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002084 }
2085
2086 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002087 * Event handler for the voice button
2088 *
2089 * @param v The view that was clicked.
2090 */
2091 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002092 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002093
Bjorn Bringertc459e522013-06-07 19:36:01 +01002094 startVoice();
2095 }
2096
2097 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002098 try {
2099 final SearchManager searchManager =
2100 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2101 ComponentName activityName = searchManager.getGlobalSearchActivity();
2102 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002103 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002104 if (activityName != null) {
2105 intent.setPackage(activityName.getPackageName());
2106 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002107 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002108 } catch (ActivityNotFoundException e) {
2109 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002110 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002111 startActivitySafely(null, intent, "onClickVoiceButton");
2112 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002113 }
2114
2115 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002116 * Event handler for the "grid" button that appears on the home screen, which
2117 * enters all apps mode.
2118 *
2119 * @param v The view that was clicked.
2120 */
2121 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002122 showAllApps(true);
2123 }
2124
2125 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002126 // Provide the same haptic feedback that the system offers for virtual keys.
2127 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002128 }
2129
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002130 public void onClickAppMarketButton(View v) {
2131 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002132 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002133 } else {
2134 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002135 }
2136 }
2137
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002138 void startApplicationDetailsActivity(ComponentName componentName) {
2139 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002140 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2141 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002142 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002143 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002144 }
2145
Patrick Dubroy5539af72010-09-07 15:22:01 -07002146 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2147 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2148 // System applications cannot be installed. For now, show a toast explaining that.
2149 // We may give them the option of disabling apps this way.
2150 int messageId = R.string.uninstall_system_app_text;
2151 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2152 } else {
2153 String packageName = appInfo.componentName.getPackageName();
2154 String className = appInfo.componentName.getClassName();
2155 Intent intent = new Intent(
2156 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002157 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2158 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002159 startActivity(intent);
2160 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002161 }
2162
Michael Jurka86a720e2012-05-09 11:23:23 -07002163 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002165
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002167 // Only launch using the new animation if the shortcut has not opted out (this is a
2168 // private contract between launcher and may be ignored in the future).
2169 boolean useLaunchAnimation = (v != null) &&
2170 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2171 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002172 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2173 v.getMeasuredWidth(), v.getMeasuredHeight());
2174
2175 startActivity(intent, opts.toBundle());
2176 } else {
2177 startActivity(intent);
2178 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002179 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 } catch (SecurityException e) {
2181 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002182 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002184 "or use the exported attribute for this activity. "
2185 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002187 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002189
Michael Jurka86a720e2012-05-09 11:23:23 -07002190 boolean startActivitySafely(View v, Intent intent, Object tag) {
2191 boolean success = false;
2192 try {
2193 success = startActivity(v, intent, tag);
2194 } catch (ActivityNotFoundException e) {
2195 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2196 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2197 }
2198 return success;
2199 }
2200
Romain Guy8e633c52010-03-04 12:51:36 -08002201 void startActivityForResultSafely(Intent intent, int requestCode) {
2202 try {
2203 startActivityForResult(intent, requestCode);
2204 } catch (ActivityNotFoundException e) {
2205 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2206 } catch (SecurityException e) {
2207 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2208 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2209 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2210 "or use the exported attribute for this activity.", e);
2211 }
2212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213
Adam Cohena9cf38f2011-05-02 15:36:58 -07002214 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002215 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002216 Folder openFolder = mWorkspace.getFolderForTag(info);
2217
2218 // If the folder info reports that the associated folder is open, then verify that
2219 // it is actually opened. There have been a few instances where this gets out of sync.
2220 if (info.opened && openFolder == null) {
2221 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002222 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002223 info.opened = false;
2224 }
2225
Adam Cohenfb91f302012-06-11 15:45:18 -07002226 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 // Close any open folder
2228 closeFolder();
2229 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002230 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 } else {
2232 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 int folderScreen;
2234 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002235 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 // .. and close it
2237 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002238 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 // Close any folder open on the current screen
2240 closeFolder();
2241 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002242 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 }
2244 }
2245 }
2246 }
2247
Adam Cohen268c4752012-06-06 17:47:33 -07002248 /**
2249 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2250 * in the DragLayer in the exact absolute location of the original FolderIcon.
2251 */
2252 private void copyFolderIconToImage(FolderIcon fi) {
2253 final int width = fi.getMeasuredWidth();
2254 final int height = fi.getMeasuredHeight();
2255
2256 // Lazy load ImageView, Bitmap and Canvas
2257 if (mFolderIconImageView == null) {
2258 mFolderIconImageView = new ImageView(this);
2259 }
2260 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2261 mFolderIconBitmap.getHeight() != height) {
2262 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2263 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2264 }
2265
2266 DragLayer.LayoutParams lp;
2267 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2268 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2269 } else {
2270 lp = new DragLayer.LayoutParams(width, height);
2271 }
2272
Adam Cohen307fe232012-08-16 17:55:58 -07002273 // The layout from which the folder is being opened may be scaled, adjust the starting
2274 // view size by this scale factor.
2275 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002276 lp.customPosition = true;
2277 lp.x = mRectForFolderAnimation.left;
2278 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002279 lp.width = (int) (scale * width);
2280 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002281
2282 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2283 fi.draw(mFolderIconCanvas);
2284 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002285 if (fi.getFolder() != null) {
2286 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2287 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002288 }
Adam Cohen268c4752012-06-06 17:47:33 -07002289 // Just in case this image view is still in the drag layer from a previous animation,
2290 // we remove it and re-add it.
2291 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2292 mDragLayer.removeView(mFolderIconImageView);
2293 }
2294 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002295 if (fi.getFolder() != null) {
2296 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002297 }
Adam Cohen268c4752012-06-06 17:47:33 -07002298 }
2299
Adam Cohen2801caf2011-05-13 20:57:39 -07002300 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002301 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002302 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2303 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2304 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2305
Adam Cohenc51934b2011-07-26 21:07:43 -07002306 FolderInfo info = (FolderInfo) fi.getTag();
2307 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2308 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002309 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2310 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002311 }
2312
Adam Cohen268c4752012-06-06 17:47:33 -07002313 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2314 copyFolderIconToImage(fi);
2315 fi.setVisibility(View.INVISIBLE);
2316
Michael Jurka2ecf9952012-06-18 12:52:28 -07002317 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002318 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002319 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2320 oa.start();
2321 }
2322
Adam Cohen268c4752012-06-06 17:47:33 -07002323 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002324 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002325 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2326 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2327 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2328
Adam Cohen268c4752012-06-06 17:47:33 -07002329 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002330
Adam Cohen268c4752012-06-06 17:47:33 -07002331 // We remove and re-draw the FolderIcon in-case it has changed
2332 mDragLayer.removeView(mFolderIconImageView);
2333 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002334 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002335 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002336 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002337 oa.addListener(new AnimatorListenerAdapter() {
2338 @Override
2339 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002340 if (cl != null) {
2341 cl.clearFolderLeaveBehind();
2342 // Remove the ImageView copy of the FolderIcon and make the original visible.
2343 mDragLayer.removeView(mFolderIconImageView);
2344 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002345 }
2346 }
2347 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002348 oa.start();
2349 }
2350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002352 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 * is animated relative to the specified View. If the View is null, no animation
2354 * is played.
2355 *
2356 * @param folderInfo The FolderInfo describing the folder to open.
2357 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002358 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002359 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002360 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361
Adam Cohena9cf38f2011-05-02 15:36:58 -07002362 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363
Adam Cohen4554ee12011-08-03 16:13:21 -07002364 // Just verify that the folder hasn't already been added to the DragLayer.
2365 // There was a one-off crash where the folder had a parent already.
2366 if (folder.getParent() == null) {
2367 mDragLayer.addView(folder);
2368 mDragController.addDropTarget((DropTarget) folder);
2369 } else {
2370 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2371 folder.getParent() + ").");
2372 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002373 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002374 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002375
2376 // Notify the accessibility manager that this folder "window" has appeared and occluded
2377 // the workspace items
2378 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2379 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002380 }
2381
2382 public void closeFolder() {
2383 Folder folder = mWorkspace.getOpenFolder();
2384 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002385 if (folder.isEditingName()) {
2386 folder.dismissEditingName();
2387 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002388 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002389
2390 // Dismiss the folder cling
2391 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002392 }
2393 }
2394
2395 void closeFolder(Folder folder) {
2396 folder.getInfo().opened = false;
2397
2398 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2399 if (parent != null) {
2400 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2401 shrinkAndFadeInFolderIcon(fi);
2402 }
2403 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002404
2405 // Notify the accessibility manager that this folder "window" has disappeard and no
2406 // longer occludeds the workspace items
2407 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 }
2409
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002411 if (!isDraggingEnabled()) return false;
2412 if (isWorkspaceLocked()) return false;
2413 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414
2415 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002416 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 }
2418
Michael Jurka0280c3b2010-09-17 15:00:07 -07002419 resetAddInfo();
2420 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002422 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 return true;
2424 }
2425
Winson Chung3d503fb2011-07-13 17:25:49 -07002426 // The hotseat touch handling does not go through Workspace, and we always allow long press
2427 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002428 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2430 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002431 if (itemUnderLongClick == null) {
2432 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002433 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2434 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002435 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002437 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002439 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 }
2441 }
2442 }
2443 return true;
2444 }
2445
Winson Chung3d503fb2011-07-13 17:25:49 -07002446 boolean isHotseatLayout(View layout) {
2447 return mHotseat != null && layout != null &&
2448 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2449 }
2450 Hotseat getHotseat() {
2451 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002452 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002453 SearchDropTargetBar getSearchBar() {
2454 return mSearchDropTargetBar;
2455 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002456
Winson Chung3d503fb2011-07-13 17:25:49 -07002457 /**
2458 * Returns the CellLayout of the specified container at the specified screen.
2459 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002460 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002461 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2462 if (mHotseat != null) {
2463 return mHotseat.getLayout();
2464 } else {
2465 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002466 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002467 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002468 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002469 }
2470 }
2471
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002472 Workspace getWorkspace() {
2473 return mWorkspace;
2474 }
2475
Daniel Sandler843e8602010-06-07 14:59:01 -04002476 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002477 @Override
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
Craig Mautner360310b2012-10-26 15:13:08 -07002482 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002483 public boolean isAllAppsButtonRank(int rank) {
2484 return mHotseat.isAllAppsButtonRank(rank);
2485 }
2486
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002487 /**
2488 * Helper method for the cameraZoomIn/cameraZoomOut animations
2489 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002490 * @param scaleFactor The scale factor used for the zoom
2491 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002492 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002493 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002494 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002495 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002496
Winson Chung18f41f82012-05-09 13:28:10 -07002497 void disableWallpaperIfInAllApps() {
2498 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002499 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002500 if (mAppsCustomizeTabHost != null &&
2501 !mAppsCustomizeTabHost.isTransitioning()) {
2502 updateWallpaperVisibility(false);
2503 }
2504 }
2505 }
2506
Craig Mautner360310b2012-10-26 15:13:08 -07002507 private void setWorkspaceBackground(boolean workspace) {
2508 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002509 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002510 }
2511
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002512 void updateWallpaperVisibility(boolean visible) {
2513 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2514 int curflags = getWindow().getAttributes().flags
2515 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2516 if (wpflags != curflags) {
2517 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2518 }
Craig Mautner360310b2012-10-26 15:13:08 -07002519 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002520 }
2521
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002522 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2523 if (v instanceof LauncherTransitionable) {
2524 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2525 }
2526 }
2527
Michael Jurkabed61d22012-02-14 22:51:29 -08002528 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2529 if (v instanceof LauncherTransitionable) {
2530 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2531 }
Winson Chung70442722012-02-10 15:43:22 -08002532
2533 // Update the workspace transition step as well
2534 dispatchOnLauncherTransitionStep(v, 0f);
2535 }
2536
2537 private void dispatchOnLauncherTransitionStep(View v, float t) {
2538 if (v instanceof LauncherTransitionable) {
2539 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2540 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002541 }
2542
2543 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2544 if (v instanceof LauncherTransitionable) {
2545 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2546 }
Winson Chung70442722012-02-10 15:43:22 -08002547
2548 // Update the workspace transition step as well
2549 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002550 }
2551
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002552 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002553 * Things to test when changing the following seven functions.
2554 * - Home from workspace
2555 * - from center screen
2556 * - from other screens
2557 * - Home from all apps
2558 * - from center screen
2559 * - from other screens
2560 * - Back from all apps
2561 * - from center screen
2562 * - from other screens
2563 * - Launch app from workspace and quit
2564 * - with back
2565 * - with home
2566 * - Launch app from all apps and quit
2567 * - with back
2568 * - with home
2569 * - Go to a screen that's not the default, then all
2570 * apps, and launch and app, and go back
2571 * - with back
2572 * -with home
2573 * - On workspace, long press power and go back
2574 * - with back
2575 * - with home
2576 * - On all apps, long press power and go back
2577 * - with back
2578 * - with home
2579 * - On workspace, power off
2580 * - On all apps, power off
2581 * - Launch an app and turn off the screen while in that app
2582 * - Go back with home key
2583 * - Go back with back key TODO: make this not go to workspace
2584 * - From all apps
2585 * - From workspace
2586 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2587 * - From all apps
2588 * - From the center workspace
2589 * - From another workspace
2590 */
2591
2592 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002593 * Zoom the camera out from the workspace to reveal 'toView'.
2594 * Assumes that the view to show is anchored at either the very top or very bottom
2595 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002596 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002597 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002598 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002599 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002600 mStateAnimation.cancel();
2601 mStateAnimation = null;
2602 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002603 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002604
Winson Chungf0ea4d32011-06-06 14:27:16 -07002605 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2606 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2607 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002608 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002609 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002610 final int startDelay =
2611 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002612
Michael Jurkab3e22d92011-10-31 15:58:33 -07002613 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002614
Michael Jurkad74c9842011-07-10 12:44:21 -07002615 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002616 Animator workspaceAnim =
2617 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002618
2619 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002620 toView.setScaleX(scale);
2621 toView.setScaleY(scale);
2622 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2623 scaleAnim.
2624 scaleX(1f).scaleY(1f).
2625 setDuration(duration).
2626 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002627
Winson Chungf0ea4d32011-06-06 14:27:16 -07002628 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002629 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002630 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002631 .ofFloat(toView, "alpha", 0f, 1f)
2632 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002633 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002634 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2635 @Override
2636 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002637 if (animation == null) {
2638 throw new RuntimeException("animation is null");
2639 }
Winson Chung70442722012-02-10 15:43:22 -08002640 float t = (Float) animation.getAnimatedValue();
2641 dispatchOnLauncherTransitionStep(fromView, t);
2642 dispatchOnLauncherTransitionStep(toView, t);
2643 }
2644 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002645
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002646 // toView should appear right at the end of the workspace shrink
2647 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002648 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002649 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002650 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002651
2652 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002653 boolean animationCancelled = false;
2654
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002655 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002656 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002657 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002658 // Prepare the position
2659 toView.setTranslationX(0.0f);
2660 toView.setTranslationY(0.0f);
2661 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002662 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002663 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002664 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002665 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002666 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2667 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002668
Daniel Sandlere4f98912013-06-25 15:13:26 -04002669 if (mWorkspace != null
2670 && !springLoaded
2671 && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002672 // Hide the workspace scrollbar
2673 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002674 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002675 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002676 if (!animationCancelled) {
2677 updateWallpaperVisibility(false);
2678 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002679
2680 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002681 if (mSearchDropTargetBar != null) {
2682 mSearchDropTargetBar.hideSearchBar(false);
2683 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002684 }
2685
Adam Cohencff6af82011-09-13 14:51:53 -07002686 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002687 public void onAnimationCancel(Animator animation) {
2688 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002689 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002690 });
2691
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002692 if (workspaceAnim != null) {
2693 mStateAnimation.play(workspaceAnim);
2694 }
Michael Jurka1899a362011-11-03 13:50:45 -07002695
2696 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002697
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002698 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2699 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002700
2701 // If any of the objects being animated haven't been measured/laid out
2702 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002703 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002704 (mWorkspace.getMeasuredWidth() == 0) ||
2705 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002706 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002707 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002708
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002709 final AnimatorSet stateAnimation = mStateAnimation;
2710 final Runnable startAnimRunnable = new Runnable() {
2711 public void run() {
2712 // Check that mStateAnimation hasn't changed while
2713 // we waited for a layout/draw pass
2714 if (mStateAnimation != stateAnimation)
2715 return;
2716 setPivotsForZoom(toView, scale);
2717 dispatchOnLauncherTransitionStart(fromView, animated, false);
2718 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002719 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002720 }
2721 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002722 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002723 final ViewTreeObserver observer = toView.getViewTreeObserver();
2724 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2725 public void onGlobalLayout() {
2726 startAnimRunnable.run();
2727 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2728 }
2729 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002730 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002731 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002732 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002733 } else {
2734 toView.setTranslationX(0.0f);
2735 toView.setTranslationY(0.0f);
2736 toView.setScaleX(1.0f);
2737 toView.setScaleY(1.0f);
2738 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002739 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002740
Daniel Sandlere4f98912013-06-25 15:13:26 -04002741 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002742 // Hide the workspace scrollbar
2743 mWorkspace.hideScrollingIndicator(true);
2744 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002745
2746 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002747 if (mSearchDropTargetBar != null) {
2748 mSearchDropTargetBar.hideSearchBar(false);
2749 }
Michael Jurkaabded662011-03-04 12:06:57 -08002750 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002751 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002752 dispatchOnLauncherTransitionStart(fromView, animated, false);
2753 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002754 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002755 dispatchOnLauncherTransitionStart(toView, animated, false);
2756 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002757 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002758 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002759 }
2760
2761 /**
2762 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002763 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002764 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002766 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2767 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002768
Michael Jurkab3e22d92011-10-31 15:58:33 -07002769 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002770 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002771 mStateAnimation.cancel();
2772 mStateAnimation = null;
2773 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002774 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002775
Winson Chungf0ea4d32011-06-06 14:27:16 -07002776 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002777 final int fadeOutDuration =
2778 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002779 final float scaleFactor = (float)
2780 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2781 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002782 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002783 Animator workspaceAnim = null;
2784
2785 if (toState == State.WORKSPACE) {
2786 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2787 workspaceAnim = mWorkspace.getChangeStateAnimation(
2788 Workspace.State.NORMAL, animated, stagger);
2789 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2790 workspaceAnim = mWorkspace.getChangeStateAnimation(
2791 Workspace.State.SPRING_LOADED, animated);
2792 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002793
Michael Jurkab3e22d92011-10-31 15:58:33 -07002794 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002795 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002796 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002797 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002798 final LauncherViewPropertyAnimator scaleAnim =
2799 new LauncherViewPropertyAnimator(fromView);
2800 scaleAnim.
2801 scaleX(scaleFactor).scaleY(scaleFactor).
2802 setDuration(duration).
2803 setInterpolator(new Workspace.ZoomInInterpolator());
2804
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002805 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002806 .ofFloat(fromView, "alpha", 1f, 0f)
2807 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002808 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002809 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2810 @Override
2811 public void onAnimationUpdate(ValueAnimator animation) {
2812 float t = 1f - (Float) animation.getAnimatedValue();
2813 dispatchOnLauncherTransitionStep(fromView, t);
2814 dispatchOnLauncherTransitionStep(toView, t);
2815 }
2816 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002817
Michael Jurka2ecf9952012-06-18 12:52:28 -07002818 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002819
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002820 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2821 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002822 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002823
2824 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002825 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002826 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002827 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002828 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002829 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2830 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002831 if (mWorkspace != null) {
2832 mWorkspace.hideScrollingIndicator(false);
2833 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002834 if (onCompleteRunnable != null) {
2835 onCompleteRunnable.run();
2836 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002837 mAppsCustomizeContent.updateCurrentPageScroll();
2838 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002839 }
2840 });
2841
Winson Chungf0ea4d32011-06-06 14:27:16 -07002842 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002843 if (workspaceAnim != null) {
2844 mStateAnimation.play(workspaceAnim);
2845 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002846 dispatchOnLauncherTransitionStart(fromView, animated, true);
2847 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002848 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002849 } else {
2850 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002851 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002852 dispatchOnLauncherTransitionStart(fromView, animated, true);
2853 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002854 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002855 dispatchOnLauncherTransitionStart(toView, animated, true);
2856 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002857 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002858 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002859 }
2860
Michael Jurkae326f182011-11-21 14:05:46 -08002861 @Override
2862 public void onTrimMemory(int level) {
2863 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002864 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002865 mAppsCustomizeTabHost.onTrimMemory();
2866 }
2867 }
2868
Winson Chung18f41f82012-05-09 13:28:10 -07002869 @Override
2870 public void onWindowFocusChanged(boolean hasFocus) {
2871 if (!hasFocus) {
2872 // When another window occludes launcher (like the notification shade, or recents),
2873 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2874 updateWallpaperVisibility(true);
2875 } else {
2876 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002877 mWorkspace.postDelayed(new Runnable() {
2878 @Override
2879 public void run() {
2880 disableWallpaperIfInAllApps();
2881 }
2882 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002883 }
2884 }
2885
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002886 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002887 showWorkspace(animated, null);
2888 }
2889
2890 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002891 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002892 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002893 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002894 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002895
Winson Chungc7d2b602012-05-16 17:02:20 -07002896 // Show the search bar (only animate if we were showing the drop target bar in spring
2897 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002898 if (mSearchDropTargetBar != null) {
2899 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2900 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002901
Michael Jurkab737ee62011-11-15 15:57:22 -08002902 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002903 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002904
2905 // Set focus to the AppsCustomize button
2906 if (mAllAppsButton != null) {
2907 mAllAppsButton.requestFocus();
2908 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002909 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002910
Michael Jurkab737ee62011-11-15 15:57:22 -08002911 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002912
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002913 // Change the state *after* we've called all the transition code
2914 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002915
Winson Chung5fb63472011-02-02 17:03:37 -08002916 // Resume the auto-advance of widgets
2917 mUserPresent = true;
2918 updateRunning();
2919
alanv1d4fde62012-10-17 13:15:47 -07002920 // Send an accessibility event to announce the context change
2921 getWindow().getDecorView()
2922 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002923
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002924 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002925 }
2926
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002927 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002928 }
2929
Michael Jurkab3e22d92011-10-31 15:58:33 -07002930 void showAllApps(boolean animated) {
2931 if (mState != State.WORKSPACE) return;
2932
2933 showAppsCustomizeHelper(animated, false);
2934 mAppsCustomizeTabHost.requestFocus();
2935
Michael Jurkab3e22d92011-10-31 15:58:33 -07002936 // Change the state *after* we've called all the transition code
2937 mState = State.APPS_CUSTOMIZE;
2938
2939 // Pause the auto-advance of widgets until we are out of AllApps
2940 mUserPresent = false;
2941 updateRunning();
2942 closeFolder();
2943
2944 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002945 getWindow().getDecorView()
2946 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002947 }
2948
Adam Cohen7777d962011-08-18 18:58:38 -07002949 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002950 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002951 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002952 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002953 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002954 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002955 }
Adam Cohen7777d962011-08-18 18:58:38 -07002956
Adam Cohened66b2b2012-01-23 17:28:51 -08002957 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2958 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002959 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2960
Winson Chunge7a03942011-08-05 15:05:12 -07002961 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002962 @Override
2963 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002964 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002965 // Before we show workspace, hide all apps again because
2966 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2967 // clean up our state transition functions
2968 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002969 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002970 } else {
2971 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002972 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002973 }
2974 }, (extendedDelay ?
2975 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2976 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2977 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002978
Michael Jurkad3ef3062010-11-23 16:23:58 -08002979 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002980 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002981 final boolean animated = true;
2982 final boolean springLoaded = true;
2983 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002984 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002985 }
2986 // Otherwise, we are not in spring loaded mode, so don't do anything.
2987 }
2988
Michael Jurkab737ee62011-11-15 15:57:22 -08002989 void hideDockDivider() {
2990 if (mQsbDivider != null && mDockDivider != null) {
2991 mQsbDivider.setVisibility(View.INVISIBLE);
2992 mDockDivider.setVisibility(View.INVISIBLE);
2993 }
2994 }
2995
2996 void showDockDivider(boolean animated) {
2997 if (mQsbDivider != null && mDockDivider != null) {
2998 mQsbDivider.setVisibility(View.VISIBLE);
2999 mDockDivider.setVisibility(View.VISIBLE);
3000 if (mDividerAnimator != null) {
3001 mDividerAnimator.cancel();
3002 mQsbDivider.setAlpha(1f);
3003 mDockDivider.setAlpha(1f);
3004 mDividerAnimator = null;
3005 }
3006 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003007 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3008 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3009 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003010 int duration = 0;
3011 if (mSearchDropTargetBar != null) {
3012 duration = mSearchDropTargetBar.getTransitionInDuration();
3013 }
3014 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003015 mDividerAnimator.start();
3016 }
3017 }
3018 }
3019
Michael Jurkab3e22d92011-10-31 15:58:33 -07003020 void lockAllApps() {
3021 // TODO
3022 }
3023
3024 void unlockAllApps() {
3025 // TODO
3026 }
3027
Winson Chungf0ea4d32011-06-06 14:27:16 -07003028 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003029 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003030 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003031 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003032 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003033 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003034 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003035 int duration = 0;
3036 if (mSearchDropTargetBar != null) {
3037 duration = mSearchDropTargetBar.getTransitionInDuration();
3038 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003039 mHotseat.animate().alpha(1f).setDuration(duration);
3040 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003041 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003042 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003043 }
3044 }
3045 }
3046
3047 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003048 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003049 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003050 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003051 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003052 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003053 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003054 int duration = 0;
3055 if (mSearchDropTargetBar != null) {
3056 duration = mSearchDropTargetBar.getTransitionOutDuration();
3057 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003058 mHotseat.animate().alpha(0f).setDuration(duration);
3059 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003060 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003061 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003062 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003063 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003064 }
3065
Patrick Dubroy5f445422011-02-18 14:35:21 -08003066 /**
3067 * Add an item from all apps or customize onto the given workspace screen.
3068 * If layout is null, add to the current screen.
3069 */
3070 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003071 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003072 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003073 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003074 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003075
Winson Chungdff8ebb2011-09-08 17:25:31 -07003076 /** Maps the current orientation to an index for referencing orientation correct global icons */
3077 private int getCurrentOrientationIndexForGlobalIcons() {
3078 // default - 0, landscape - 1
3079 switch (getResources().getConfiguration().orientation) {
3080 case Configuration.ORIENTATION_LANDSCAPE:
3081 return 1;
3082 default:
3083 return 0;
3084 }
3085 }
3086
Michael Jurkaae65ee32012-04-30 11:45:54 -07003087 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003088 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003089 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003090 // Look for the toolbar icon specified in the activity meta-data
3091 Bundle metaData = packageManager.getActivityInfo(
3092 activityName, PackageManager.GET_META_DATA).metaData;
3093 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003094 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003095 if (iconResId != 0) {
3096 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003097 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003098 }
3099 }
3100 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003101 // This can happen if the activity defines an invalid drawable
3102 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3103 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003104 } catch (Resources.NotFoundException nfe) {
3105 // This can happen if the activity defines an invalid drawable
3106 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3107 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003108 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003109 return null;
3110 }
3111
3112 // if successful in getting icon, return it; otherwise, set button to use default drawable
3113 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003114 int buttonId, ComponentName activityName, int fallbackDrawableId,
3115 String toolbarResourceName) {
3116 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003117 Resources r = getResources();
3118 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3119 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003120
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003121 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003122 // If we were unable to find the icon via the meta-data, use a generic one
3123 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003124 toolbarIcon = r.getDrawable(fallbackDrawableId);
3125 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003126 if (button != null) {
3127 button.setCompoundDrawables(toolbarIcon, null, null, null);
3128 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003129 return null;
3130 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003131 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003132 if (button != null) {
3133 button.setCompoundDrawables(toolbarIcon, null, null, null);
3134 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003135 return toolbarIcon.getConstantState();
3136 }
3137 }
3138
3139 // if successful in getting icon, return it; otherwise, set button to use default drawable
3140 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003141 int buttonId, ComponentName activityName, int fallbackDrawableId,
3142 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003143 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003144 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003145
Michael Jurka19e0fc52011-07-22 18:00:21 -07003146 if (button != null) {
3147 // If we were unable to find the icon via the meta-data, use a
3148 // generic one
3149 if (toolbarIcon == null) {
3150 button.setImageResource(fallbackDrawableId);
3151 } else {
3152 button.setImageDrawable(toolbarIcon);
3153 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003154 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003155
3156 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3157
Michael Jurka0423dcf2010-10-05 14:56:18 -07003158 }
3159
Winson Chung1b884092012-06-08 17:13:02 -07003160 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003161 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003162 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003163 }
3164
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003165 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003166 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003167 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003168 }
3169
Winson Chungbb185bd2011-11-21 12:31:42 -08003170 private void invalidatePressedFocusedStates(View container, View button) {
3171 if (container instanceof HolographicLinearLayout) {
3172 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3173 layout.invalidatePressedFocusedStates();
3174 } else if (button instanceof HolographicImageView) {
3175 HolographicImageView view = (HolographicImageView) button;
3176 view.invalidatePressedFocusedStates();
3177 }
3178 }
3179
Winson Chungc51db6a2011-10-05 11:44:49 -07003180 private boolean updateGlobalSearchIcon() {
3181 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003182 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003183 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003184 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003185
Winson Chung1cad91e2011-05-25 17:41:01 -07003186 final SearchManager searchManager =
3187 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3188 ComponentName activityName = searchManager.getGlobalSearchActivity();
3189 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003190 int coi = getCurrentOrientationIndexForGlobalIcons();
3191 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003192 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3193 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3194 if (sGlobalSearchIcon[coi] == null) {
3195 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3196 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3197 TOOLBAR_ICON_METADATA_NAME);
3198 }
3199
Winson Chungc51db6a2011-10-05 11:44:49 -07003200 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3201 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003202 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003203 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003204 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003205 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003206 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3207 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3208 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003209 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003210 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003211 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003212 }
3213 }
3214
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003215 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003216 final View searchButtonContainer = findViewById(R.id.search_button_container);
3217 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003218 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003219 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003220 }
3221
Winson Chungc51db6a2011-10-05 11:44:49 -07003222 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003223 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003224 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003225
Winson Chungc51db6a2011-10-05 11:44:49 -07003226 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003227 final SearchManager searchManager =
3228 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3229 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3230
3231 ComponentName activityName = null;
3232 if (globalSearchActivity != null) {
3233 // Check if the global search activity handles voice search
3234 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3235 intent.setPackage(globalSearchActivity.getPackageName());
3236 activityName = intent.resolveActivity(getPackageManager());
3237 }
3238
3239 if (activityName == null) {
3240 // Fallback: check if an activity other than the global search activity
3241 // resolves this
3242 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3243 activityName = intent.resolveActivity(getPackageManager());
3244 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003245 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003246 int coi = getCurrentOrientationIndexForGlobalIcons();
3247 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003248 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3249 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3250 if (sVoiceSearchIcon[coi] == null) {
3251 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3252 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3253 TOOLBAR_ICON_METADATA_NAME);
3254 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003255 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003256 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003257 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003258 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003259 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003260 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003262 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003263 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003264 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003265 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003266 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003267
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003268 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003269 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3270 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003271 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003272 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003273 }
3274
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003275 public void setVoiceButtonProxyVisible(boolean visible) {
3276 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3277 if (voiceButtonProxy != null) {
3278 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3279 }
3280 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003281 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003282 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003283 */
3284 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003285 final View marketButton = findViewById(R.id.market_button);
3286 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3287 // Find the app market activity by resolving an intent.
3288 // (If multiple app markets are installed, it will return the ResolverActivity.)
3289 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003290 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003291 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003292 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003293 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003294 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3295 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003296 marketButton.setVisibility(View.VISIBLE);
3297 } else {
3298 // We should hide and disable the view so that we don't try and restore the visibility
3299 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3300 marketButton.setVisibility(View.GONE);
3301 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003302 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003303 }
3304
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003305 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003306 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3307 Resources r = getResources();
3308 Drawable marketIconDrawable = d.newDrawable(r);
3309 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3310 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3311 marketIconDrawable.setBounds(0, 0, w, h);
3312
3313 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003314 }
3315
Michael Jurkad7c28052012-04-27 15:43:36 -07003316 @Override
3317 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003318 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003319 final List<CharSequence> text = event.getText();
3320 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003321 // Populate event with a fake title based on the current state.
3322 if (mState == State.APPS_CUSTOMIZE) {
3323 text.add(getString(R.string.all_apps_button_label));
3324 } else {
3325 text.add(getString(R.string.all_apps_home_button_label));
3326 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003327 return result;
3328 }
3329
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003330 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003331 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003332 */
3333 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3334 @Override
3335 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003336 closeSystemDialogs();
3337 }
3338 }
3339
3340 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003341 * Receives notifications whenever the appwidgets are reset.
3342 */
3343 private class AppWidgetResetObserver extends ContentObserver {
3344 public AppWidgetResetObserver() {
3345 super(new Handler());
3346 }
3347
3348 @Override
3349 public void onChange(boolean selfChange) {
3350 onAppWidgetReset();
3351 }
3352 }
3353
3354 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003355 * If the activity is currently paused, signal that we need to run the passed Runnable
3356 * in onResume.
3357 *
3358 * This needs to be called from incoming places where resources might have been loaded
3359 * while we are paused. That is becaues the Configuration might be wrong
3360 * when we're not running, and if it comes back to what it was when we
3361 * were paused, we are not restarted.
3362 *
3363 * Implementation of the method from LauncherModel.Callbacks.
3364 *
3365 * @return true if we are currently paused. The caller might be able to
3366 * skip some work in that case since we will come back again.
3367 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003368 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003369 if (mPaused) {
3370 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003371 if (deletePreviousRunnables) {
3372 while (mOnResumeCallbacks.remove(run)) {
3373 }
3374 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003375 mOnResumeCallbacks.add(run);
3376 return true;
3377 } else {
3378 return false;
3379 }
3380 }
3381
Michael Jurkac402cd92013-05-20 15:49:32 +02003382 private boolean waitUntilResume(Runnable run) {
3383 return waitUntilResume(run, false);
3384 }
3385
Michael Jurka7607c2f2013-04-03 14:33:19 -07003386 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003387 * If the activity is currently paused, signal that we need to re-run the loader
3388 * in onResume.
3389 *
3390 * This needs to be called from incoming places where resources might have been loaded
3391 * while we are paused. That is becaues the Configuration might be wrong
3392 * when we're not running, and if it comes back to what it was when we
3393 * were paused, we are not restarted.
3394 *
3395 * Implementation of the method from LauncherModel.Callbacks.
3396 *
3397 * @return true if we are currently paused. The caller might be able to
3398 * skip some work in that case since we will come back again.
3399 */
3400 public boolean setLoadOnResume() {
3401 if (mPaused) {
3402 Log.i(TAG, "setLoadOnResume");
3403 mOnResumeNeedsLoad = true;
3404 return true;
3405 } else {
3406 return false;
3407 }
3408 }
3409
3410 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003412 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003414 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003415 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003416 } else {
3417 return SCREEN_COUNT / 2;
3418 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003420
Joe Onorato9c1289c2009-08-17 11:03:03 -04003421 /**
3422 * Refreshes the shortcuts shown on the workspace.
3423 *
3424 * Implementation of the method from LauncherModel.Callbacks.
3425 */
3426 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003427 // If we're starting binding all over again, clear any bind calls we'd postponed in
3428 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3429 // from scratch again
3430 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003431
Michael Jurka7607c2f2013-04-03 14:33:19 -07003432 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003433 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003434 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003435 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003436 int count = workspace.getChildCount();
3437 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003438 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003439 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3440 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003441 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003442 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003443 if (mHotseat != null) {
3444 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003445 }
3446 }
3447
Adam Cohendcd297f2013-06-18 13:13:40 -07003448 @Override
3449 public void bindScreens(ArrayList<Long> orderedScreenIds) {
3450 int count = orderedScreenIds.size();
3451 for (int i = 0; i < count; i++) {
3452 mWorkspace.insertNewWorkspaceScreenOnBind(orderedScreenIds.get(i));
3453 }
3454 mWorkspace.addExtraEmptyScreen();
3455 }
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 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003462 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3463 if (waitUntilResume(new Runnable() {
3464 public void run() {
3465 bindItems(shortcuts, start, end);
3466 }
3467 })) {
3468 return;
3469 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003470
Winson Chungf0c6ae02012-03-21 16:10:31 -07003471 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3472 Set<String> newApps = new HashSet<String>();
3473 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3474
3475 Workspace workspace = mWorkspace;
3476 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003478
3479 // Short circuit if we are loading dock items for a configuration which has no dock
3480 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3481 mHotseat == null) {
3482 continue;
3483 }
3484
Joe Onorato9c1289c2009-08-17 11:03:03 -04003485 switch (item.itemType) {
3486 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3487 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003488 ShortcutInfo info = (ShortcutInfo) item;
3489 String uri = info.intent.toUri(0).toString();
3490 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003491
3492 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3493 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003494 boolean animateIconUp = false;
3495 synchronized (newApps) {
3496 if (newApps.contains(uri)) {
3497 animateIconUp = newApps.remove(uri);
3498 }
3499 }
3500 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003501 // Prepare the view to be animated up
3502 shortcut.setAlpha(0f);
3503 shortcut.setScaleX(0f);
3504 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003505 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003506 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3507 mNewShortcutAnimateViews.add(shortcut);
3508 }
3509 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003511 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003512 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003513 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003514 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003515 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3516 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003517 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003518 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003519 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003520
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003522 }
3523
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 /**
3525 * Implementation of the method from LauncherModel.Callbacks.
3526 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003527 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3528 if (waitUntilResume(new Runnable() {
3529 public void run() {
3530 bindFolders(folders);
3531 }
3532 })) {
3533 return;
3534 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003535 sFolders.clear();
3536 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003537 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538
3539 /**
3540 * Add the views for a widget to the workspace.
3541 *
3542 * Implementation of the method from LauncherModel.Callbacks.
3543 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003544 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3545 if (waitUntilResume(new Runnable() {
3546 public void run() {
3547 bindAppWidget(item);
3548 }
3549 })) {
3550 return;
3551 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003552
Daniel Sandler843e8602010-06-07 14:59:01 -04003553 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3554 if (DEBUG_WIDGETS) {
3555 Log.d(TAG, "bindAppWidget: " + item);
3556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 final Workspace workspace = mWorkspace;
3558
3559 final int appWidgetId = item.appWidgetId;
3560 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003561 if (DEBUG_WIDGETS) {
3562 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3563 }
3564
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3566
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003568 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569
Adam Cohendcd297f2013-06-18 13:13:40 -07003570 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003572 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3573
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 workspace.requestLayout();
3575
Daniel Sandler843e8602010-06-07 14:59:01 -04003576 if (DEBUG_WIDGETS) {
3577 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3578 + (SystemClock.uptimeMillis()-start) + "ms");
3579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 }
3581
Adam Cohen1462de32012-07-24 22:34:36 -07003582 public void onPageBoundSynchronously(int page) {
3583 mSynchronouslyBoundPages.add(page);
3584 }
3585
Joe Onorato9c1289c2009-08-17 11:03:03 -04003586 /**
3587 * Callback saying that there aren't any more items to bind.
3588 *
3589 * Implementation of the method from LauncherModel.Callbacks.
3590 */
Adam Cohene25af792013-06-06 23:08:25 -07003591 public void finishBindingItems(final boolean upgradePath) {
Winson Chungc9168342013-06-26 14:54:55 -07003592
Michael Jurka7607c2f2013-04-03 14:33:19 -07003593 if (waitUntilResume(new Runnable() {
3594 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003595 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003596 }
3597 })) {
3598 return;
3599 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 if (mSavedState != null) {
3601 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003602 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 mSavedState = null;
3605 }
3606
Adam Cohen1462de32012-07-24 22:34:36 -07003607 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003608
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003609 // If we received the result of any pending adds while the loader was running (e.g. the
3610 // widget configuration forced an orientation change), process them now.
3611 for (int i = 0; i < sPendingAddList.size(); i++) {
3612 completeAdd(sPendingAddList.get(i));
3613 }
3614 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615
Winson Chungc51db6a2011-10-05 11:44:49 -07003616 // Update the market app icon as necessary (the other icons will be managed in response to
3617 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003618 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003619
Winson Chungf0c6ae02012-03-21 16:10:31 -07003620 // Animate up any icons as necessary
3621 if (mVisible || mWorkspaceLoading) {
3622 Runnable newAppsRunnable = new Runnable() {
3623 @Override
3624 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003625 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003626 }
3627 };
Winson Chunga2413752012-04-03 14:22:34 -07003628
Adam Cohendcd297f2013-06-18 13:13:40 -07003629 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3630 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003631 if (canRunNewAppsAnimation()) {
3632 // If the user has not interacted recently, then either snap to the new page to show
3633 // the new-apps animation or just run them if they are to appear on the current page
3634 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003635 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003636 } else {
3637 runNewAppsAnimation(false);
3638 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003639 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003640 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003641 // are on another page (or just normally if they are added to the current page)
3642 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003643 }
3644 }
3645
3646 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003647 if (upgradePath) {
3648 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003649 mWorkspace.stripDuplicateApps();
3650 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003651 }
Adam Cohen99894d92013-06-14 11:22:59 -07003652
Adam Cohen66a01fd2013-06-11 12:48:00 -07003653 mWorkspace.post(new Runnable() {
3654 @Override
3655 public void run() {
3656 onFinishBindingItems();
3657 }
3658 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003659 }
3660
Winson Chunga2413752012-04-03 14:22:34 -07003661 private boolean canRunNewAppsAnimation() {
3662 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3663 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3664 }
3665
Winson Chungc9168342013-06-26 14:54:55 -07003666 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3667 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3668 PropertyValuesHolder.ofFloat("alpha", 1f),
3669 PropertyValuesHolder.ofFloat("scaleX", 1f),
3670 PropertyValuesHolder.ofFloat("scaleY", 1f));
3671 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3672 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3673 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3674 return bounceAnim;
3675 }
3676
Winson Chungf0c6ae02012-03-21 16:10:31 -07003677 /**
3678 * Runs a new animation that scales up icons that were added while Launcher was in the
3679 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003680 *
3681 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003682 */
Winson Chunga2413752012-04-03 14:22:34 -07003683 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003684 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003685 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003686
3687 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003688 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3689 @Override
3690 public int compare(View a, View b) {
3691 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3692 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3693 int cellCountX = LauncherModel.getCellCountX();
3694 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3695 }
3696 });
Winson Chunga2413752012-04-03 14:22:34 -07003697
3698 // Animate each of the views in place (or show them immediately if requested)
3699 if (immediate) {
3700 for (View v : mNewShortcutAnimateViews) {
3701 v.setAlpha(1f);
3702 v.setScaleX(1f);
3703 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003704 }
Winson Chunga2413752012-04-03 14:22:34 -07003705 } else {
3706 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3707 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003708 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003709 }
3710 anim.playTogether(bounceAnims);
3711 anim.addListener(new AnimatorListenerAdapter() {
3712 @Override
3713 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003714 if (mWorkspace != null) {
3715 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3716 }
Winson Chunga2413752012-04-03 14:22:34 -07003717 }
3718 });
3719 anim.start();
3720 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003721
3722 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003723 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003724 mNewShortcutAnimateViews.clear();
3725 new Thread("clearNewAppsThread") {
3726 public void run() {
3727 mSharedPrefs.edit()
3728 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3729 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3730 .commit();
3731 }
3732 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003733 }
3734
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003735 @Override
3736 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003737 boolean searchVisible = updateGlobalSearchIcon();
3738 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003739 if (mSearchDropTargetBar != null) {
3740 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3741 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003742 }
3743
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003744 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003745 * Add the icons for all apps.
3746 *
3747 * Implementation of the method from LauncherModel.Callbacks.
3748 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003749 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003750 Runnable setAllAppsRunnable = new Runnable() {
3751 public void run() {
3752 if (mAppsCustomizeContent != null) {
3753 mAppsCustomizeContent.setApps(apps);
Adam Cohene25af792013-06-06 23:08:25 -07003754
3755 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3756 getHotseat().addAllAppsFolder(mIconCache, apps,
3757 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3758 mIntentsOnWorkspaceFromUpgradePath = null;
3759 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07003760 }
3761 }
3762 };
3763
Michael Jurkac57b7a82011-08-09 22:02:20 -07003764 // Remove the progress bar entirely; we could also make it GONE
3765 // but better to remove it since we know it's not going to be used
3766 View progressBar = mAppsCustomizeTabHost.
3767 findViewById(R.id.apps_customize_progress_bar);
3768 if (progressBar != null) {
3769 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003770
3771 // We just post the call to setApps so the user sees the progress bar
3772 // disappear-- otherwise, it just looks like the progress bar froze
3773 // which doesn't look great
3774 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3775 } else {
3776 // If we did not initialize the spinner in onCreate, then we can directly set the
3777 // list of applications without waiting for any progress bars views to be hidden.
3778 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003779 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003780 }
3781
3782 /**
3783 * A package was installed.
3784 *
3785 * Implementation of the method from LauncherModel.Callbacks.
3786 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003787 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3788 if (waitUntilResume(new Runnable() {
3789 public void run() {
3790 bindAppsAdded(apps);
3791 }
3792 })) {
3793 return;
3794 }
3795
Winson Chungc9168342013-06-26 14:54:55 -07003796 final Launcher launcher = this;
3797 final Context context = this;
3798 final ArrayList<ApplicationInfo> appsCopy = new ArrayList<ApplicationInfo>();
3799 appsCopy.addAll(apps);
3800
3801 // Process newly added apps
3802 mWorkspace.post(new Runnable() {
3803 @Override
3804 public void run() {
3805 // Process newly added apps
3806 LauncherModel model = getModel();
3807 Iterator<ApplicationInfo> iter = appsCopy.iterator();
3808 ArrayList<View> animatedShortcuts = new ArrayList<View>();
3809
3810 while (iter.hasNext()) {
3811 ApplicationInfo a = iter.next();
3812 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
3813 a.title.toString(), a.intent);
3814 if (coords == null) {
3815 // If we can't find a valid position, then just add a new screen.
3816 // This takes time so we need to re-queue the add until the new
3817 // page is added.
Daniel Sandlere4f98912013-06-25 15:13:26 -04003818 long screenId = LauncherAppState.getInstance().getLauncherProvider()
3819 .generateNewScreenId();
Winson Chungc9168342013-06-26 14:54:55 -07003820 mWorkspace.insertNewWorkspaceScreen(screenId, false);
3821 model.updateWorkspaceScreenOrder(launcher, mWorkspace.getScreenOrder(),
3822 new Runnable() {
3823 @Override
3824 public void run() {
3825 bindAppsAdded(appsCopy);
3826 }
3827 });
3828 return;
3829 } else {
3830 final ShortcutInfo shortcutInfo = a.makeShortcut();
3831 final View shortcut = createShortcut(shortcutInfo);
3832 // Add the view to the screen
3833 mWorkspace.addInScreenFromBind(shortcut,
3834 LauncherSettings.Favorites.CONTAINER_DESKTOP,
3835 coords.first, coords.second[0], coords.second[1], 1, 1);
3836 // Add the shortcut to the db
3837 model.addItemToDatabase(context, shortcutInfo,
3838 LauncherSettings.Favorites.CONTAINER_DESKTOP,
3839 coords.first, coords.second[0], coords.second[1], false);
3840 // Animate the shortcut
3841 animatedShortcuts.add(shortcut);
3842 }
3843 iter.remove();
3844 }
3845
3846 // Animate all the applications up
3847 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3848 Collection<Animator> bounceAnims = new ArrayList<Animator>();
3849 for (int i = 0; i < animatedShortcuts.size(); ++i) {
3850 View shortcut = animatedShortcuts.get(i);
3851 shortcut.setAlpha(0f);
3852 shortcut.setScaleX(0f);
3853 shortcut.setScaleY(0f);
3854 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3855 }
3856 anim.playTogether(bounceAnims);
3857 anim.start();
3858 }
3859 });
Winson Chungf0ea4d32011-06-06 14:27:16 -07003860
Winson Chung785d2eb2011-04-14 16:08:02 -07003861 if (mAppsCustomizeContent != null) {
3862 mAppsCustomizeContent.addApps(apps);
3863 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003864 }
3865
3866 /**
3867 * A package was updated.
3868 *
3869 * Implementation of the method from LauncherModel.Callbacks.
3870 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003871 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3872 if (waitUntilResume(new Runnable() {
3873 public void run() {
3874 bindAppsUpdated(apps);
3875 }
3876 })) {
3877 return;
3878 }
3879
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003880 if (mWorkspace != null) {
3881 mWorkspace.updateShortcuts(apps);
3882 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003883
Winson Chung785d2eb2011-04-14 16:08:02 -07003884 if (mAppsCustomizeContent != null) {
3885 mAppsCustomizeContent.updateApps(apps);
3886 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 }
3888
3889 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003890 * A package was uninstalled. We take both the super set of packageNames
3891 * in addition to specific applications to remove, the reason being that
3892 * this can be called when a package is updated as well. In that scenario,
3893 * we only remove specific components from the workspace, where as
3894 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895 *
3896 * Implementation of the method from LauncherModel.Callbacks.
3897 */
Winson Chung83892cc2013-05-01 16:53:33 -07003898 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3899 final ArrayList<ApplicationInfo> appInfos,
3900 final boolean matchPackageNamesOnly) {
3901 if (waitUntilResume(new Runnable() {
3902 public void run() {
3903 bindComponentsRemoved(packageNames, appInfos, matchPackageNamesOnly);
3904 }
3905 })) {
3906 return;
3907 }
3908
3909 if (matchPackageNamesOnly) {
3910 mWorkspace.removeItemsByPackageName(packageNames);
3911 } else {
3912 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003913 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003914
Winson Chung785d2eb2011-04-14 16:08:02 -07003915 if (mAppsCustomizeContent != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07003916 mAppsCustomizeContent.removeApps(appInfos);
Winson Chung785d2eb2011-04-14 16:08:02 -07003917 }
Winson Chunga1820962011-10-03 16:31:06 -07003918
3919 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003920 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003921 }
Joe Onoratobe386092009-11-17 17:32:16 -08003922
3923 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003924 * A number of packages were updated.
3925 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003926
3927 private ArrayList<Object> mWidgetsAndShortcuts;
3928 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003929 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003930 bindPackagesUpdated(mWidgetsAndShortcuts);
3931 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003932 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003933 };
3934
3935 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3936 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3937 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003938 return;
3939 }
3940
Winson Chung785d2eb2011-04-14 16:08:02 -07003941 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003942 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003943 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003944 }
3945
Winson Chung400438b2011-01-16 17:53:48 -08003946 private int mapConfigurationOriActivityInfoOri(int configOri) {
3947 final Display d = getWindowManager().getDefaultDisplay();
3948 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3949 switch (d.getRotation()) {
3950 case Surface.ROTATION_0:
3951 case Surface.ROTATION_180:
3952 // We are currently in the same basic orientation as the natural orientation
3953 naturalOri = configOri;
3954 break;
3955 case Surface.ROTATION_90:
3956 case Surface.ROTATION_270:
3957 // We are currently in the other basic orientation to the natural orientation
3958 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3959 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3960 break;
3961 }
3962
3963 int[] oriMap = {
3964 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3965 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3966 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3967 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3968 };
3969 // Since the map starts at portrait, we need to offset if this device's natural orientation
3970 // is landscape.
3971 int indexOffset = 0;
3972 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3973 indexOffset = 1;
3974 }
3975 return oriMap[(d.getRotation() + indexOffset) % 4];
3976 }
Adam Cohen446e9402011-09-15 18:21:21 -07003977
Winson Chung4b919f82012-05-01 10:44:08 -07003978 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003979 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003980 getResources().getBoolean(R.bool.allow_rotation);
3981 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003982 }
Winson Chung4b919f82012-05-01 10:44:08 -07003983 public void lockScreenOrientation() {
3984 if (isRotationEnabled()) {
3985 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3986 .getConfiguration().orientation));
3987 }
3988 }
3989 public void unlockScreenOrientation(boolean immediate) {
3990 if (isRotationEnabled()) {
3991 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003992 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003993 } else {
3994 mHandler.postDelayed(new Runnable() {
3995 public void run() {
3996 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3997 }
3998 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003999 }
Winson Chung4b919f82012-05-01 10:44:08 -07004000 }
Winson Chung400438b2011-01-16 17:53:48 -08004001 }
4002
Winson Chung82f55532011-08-09 14:14:23 -07004003 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004004 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004005 if (DISABLE_CLINGS) {
4006 return false;
4007 }
4008
Brett Chabot2a9e2282011-08-23 15:03:13 -07004009 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004010 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004011
Michael Jurkae233a8b2013-03-19 13:49:20 +01004012 // Restricted secondary users (child mode) will potentially have very few apps
4013 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004014// boolean supportsLimitedUsers =
4015// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4016// Account[] accounts = AccountManager.get(this).getAccounts();
4017// if (supportsLimitedUsers && accounts.length == 0) {
4018// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4019// Bundle restrictions = um.getUserRestrictions();
4020// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4021// return false;
4022// }
4023// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004024 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004025 }
Michael Jurka22143132012-10-04 17:42:38 +02004026
Winson Chung7d7541e2011-09-16 20:14:36 -07004027 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004028 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004029 if (cling != null) {
4030 cling.init(this, positionData);
4031 cling.setVisibility(View.VISIBLE);
4032 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4033 if (animate) {
4034 cling.buildLayer();
4035 cling.setAlpha(0f);
4036 cling.animate()
4037 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004038 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004039 .setDuration(SHOW_CLING_DURATION)
4040 .setStartDelay(delay)
4041 .start();
4042 } else {
4043 cling.setAlpha(1f);
4044 }
Michael Jurka22143132012-10-04 17:42:38 +02004045 cling.setFocusableInTouchMode(true);
4046 cling.post(new Runnable() {
4047 public void run() {
4048 cling.setFocusable(true);
4049 cling.requestFocus();
4050 }
4051 });
4052 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4053 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004054 }
4055 return cling;
4056 }
Michael Jurka22143132012-10-04 17:42:38 +02004057
Winson Chung7d7541e2011-09-16 20:14:36 -07004058 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004059 // To catch cases where siblings of top-level views are made invisible, just check whether
4060 // the cling is directly set to GONE before dismissing it.
4061 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004062 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004063 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004064 anim.addListener(new AnimatorListenerAdapter() {
4065 public void onAnimationEnd(Animator animation) {
4066 cling.setVisibility(View.GONE);
4067 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004068 // We should update the shared preferences on a background thread
4069 new Thread("dismissClingThread") {
4070 public void run() {
4071 SharedPreferences.Editor editor = mSharedPrefs.edit();
4072 editor.putBoolean(flag, true);
4073 editor.commit();
4074 }
4075 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004076 };
4077 });
4078 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004079 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004080 }
4081 }
Michael Jurka22143132012-10-04 17:42:38 +02004082
Winson Chung9d9d74f2011-09-19 11:49:12 -07004083 private void removeCling(int id) {
4084 final View cling = findViewById(id);
4085 if (cling != null) {
4086 final ViewGroup parent = (ViewGroup) cling.getParent();
4087 parent.post(new Runnable() {
4088 @Override
4089 public void run() {
4090 parent.removeView(cling);
4091 }
4092 });
Michael Jurka22143132012-10-04 17:42:38 +02004093 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004094 }
4095 }
Michael Jurka974c3862012-05-22 22:00:31 -07004096
4097 private boolean skipCustomClingIfNoAccounts() {
4098 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4099 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4100 if (customCling) {
4101 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004102 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004103 Account[] accounts = am.getAccountsByType("com.google");
4104 return accounts.length == 0;
4105 }
4106 return false;
4107 }
4108
Winson Chung7d7541e2011-09-16 20:14:36 -07004109 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004110 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004111 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004112 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4113 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004114 // If we're not using the default workspace layout, replace workspace cling
4115 // with a custom workspace cling (usually specified in an overlay)
4116 // For now, only do this on tablets
4117 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004118 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004119 // Use a custom cling
4120 View cling = findViewById(R.id.workspace_cling);
4121 ViewGroup clingParent = (ViewGroup) cling.getParent();
4122 int clingIndex = clingParent.indexOfChild(cling);
4123 clingParent.removeViewAt(clingIndex);
4124 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4125 clingParent.addView(customCling, clingIndex);
4126 customCling.setId(R.id.workspace_cling);
4127 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004128 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004129 } else {
4130 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004131 }
4132 }
4133 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004134 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004135 if (isClingsEnabled() &&
4136 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004137 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004138 } else {
4139 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004140 }
4141 }
4142 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004143 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004144 if (isClingsEnabled() &&
4145 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4146 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004147 } else {
4148 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004149 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004150 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004151 }
4152 public boolean isFolderClingVisible() {
4153 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004154 if (cling != null) {
4155 return cling.getVisibility() == View.VISIBLE;
4156 }
4157 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004158 }
Winson Chung82f55532011-08-09 14:14:23 -07004159 public void dismissWorkspaceCling(View v) {
4160 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004161 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004162 }
4163 public void dismissAllAppsCling(View v) {
4164 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004165 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4166 }
4167 public void dismissFolderCling(View v) {
4168 Cling cling = (Cling) findViewById(R.id.folder_cling);
4169 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004170 }
4171
Winson Chung80baf5a2010-08-09 16:03:15 -07004172 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004173 * Prints out out state for debugging.
4174 */
4175 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004176 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004177 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004178 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4179 Log.d(TAG, "mRestoring=" + mRestoring);
4180 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4181 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004182 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004183 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004184
Winson Chung785d2eb2011-04-14 16:08:02 -07004185 if (mAppsCustomizeContent != null) {
4186 mAppsCustomizeContent.dumpState();
4187 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004188 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004189 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004190
4191 @Override
4192 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4193 super.dump(prefix, fd, writer, args);
4194 writer.println(" ");
4195 writer.println("Debug logs: ");
4196 for (int i = 0; i < sDumpLogs.size(); i++) {
4197 writer.println(" " + sDumpLogs.get(i));
4198 }
4199 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004200
4201 public static void dumpDebugLogsToConsole() {
4202 Log.d(TAG, "");
4203 Log.d(TAG, "*********************");
4204 Log.d(TAG, "Launcher debug logs: ");
4205 for (int i = 0; i < sDumpLogs.size(); i++) {
4206 Log.d(TAG, " " + sDumpLogs.get(i));
4207 }
4208 Log.d(TAG, "*********************");
4209 Log.d(TAG, "");
4210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004211}
Michael Jurka2763be32011-02-24 11:19:57 -08004212
Michael Jurkaabded662011-03-04 12:06:57 -08004213interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004214 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004215 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004216 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004217 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004218 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004219}