blob: 47fa66b779cd9b3c57eaaf8dafc6b5a232605fa5 [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 Sandlere060b0b2013-06-27 21:47:55 -0400351
352 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
353 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400354 LauncherAppState app = LauncherAppState.getInstance();
355 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700356 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800357 mModel = app.setLauncher(this);
358 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400359 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700361
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700362 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700363 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
364 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700365
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700366 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
367 // this also ensures that any synchronous binding below doesn't re-trigger another
368 // LauncherModel load.
369 mPaused = false;
370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200372 android.os.Debug.startMethodTracing(
373 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 }
375
376 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 setContentView(R.layout.launcher);
378 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700379 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800381 registerContentObservers();
382
Joe Onorato7c312c12009-08-13 21:36:53 -0700383 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 mSavedState = savedInstanceState;
386 restoreState(mSavedState);
387
Winson Chunga12a2502010-12-20 14:41:35 -0800388 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700389 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200390 mAppsCustomizeContent.onPackagesUpdated(
391 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700392 }
Winson Chunga12a2502010-12-20 14:41:35 -0800393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 if (PROFILE_STARTUP) {
395 android.os.Debug.stopMethodTracing();
396 }
397
398 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700399 if (sPausedFromUserAction) {
400 // If the user leaves launcher, then we should just load items asynchronously when
401 // they return.
402 mModel.startLoader(true, -1);
403 } else {
404 // We only load the page synchronously if the user rotates (or triggers a
405 // configuration change) while launcher is in the foreground
406 mModel.startLoader(true, mWorkspace.getCurrentPage());
407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 }
409
Michael Jurkac57b7a82011-08-09 22:02:20 -0700410 if (!mModel.isAllAppsLoaded()) {
411 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
412 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
413 }
414
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 // For handling default keys
416 mDefaultKeySsb = new SpannableStringBuilder();
417 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800418
419 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
420 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800421
Adam Cohenf9426d52012-06-04 17:26:21 -0700422 updateGlobalIcons();
423
424 // On large interfaces, we want the screen to auto-rotate based on the current orientation
425 unlockScreenOrientation(true);
426 }
427
Winson Chung4a2afa32012-07-19 14:53:05 -0700428 protected void onUserLeaveHint() {
429 super.onUserLeaveHint();
430 sPausedFromUserAction = true;
431 }
432
Adam Cohenf9426d52012-06-04 17:26:21 -0700433 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700434 boolean searchVisible = false;
435 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800436 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700437 int coi = getCurrentOrientationIndexForGlobalIcons();
438 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
439 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700440 updateAppMarketIcon();
441 searchVisible = updateGlobalSearchIcon();
442 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700443 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700444 if (sGlobalSearchIcon[coi] != null) {
445 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700446 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700447 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700448 if (sVoiceSearchIcon[coi] != null) {
449 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700450 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700451 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700452 if (sAppMarketIcon[coi] != null) {
453 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800454 }
Winson Chungadf0c182012-08-23 14:59:07 -0700455 if (mSearchDropTargetBar != null) {
456 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
Romain Guycbb89e42009-06-08 15:52:54 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700461 if (sLocaleConfiguration == null) {
462 new AsyncTask<Void, Void, LocaleConfiguration>() {
463 @Override
464 protected LocaleConfiguration doInBackground(Void... unused) {
465 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
466 readConfiguration(Launcher.this, localeConfiguration);
467 return localeConfiguration;
468 }
469
470 @Override
471 protected void onPostExecute(LocaleConfiguration result) {
472 sLocaleConfiguration = result;
473 checkForLocaleChange(); // recursive, but now with a locale configuration
474 }
475 }.execute();
476 return;
477 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 final Configuration configuration = getResources().getConfiguration();
480
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700481 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 final String locale = configuration.locale.toString();
483
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 final int mcc = configuration.mcc;
486
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700487 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 final int mnc = configuration.mnc;
489
Romain Guy84f296c2009-11-04 15:00:44 -0800490 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491
Romain Guy84f296c2009-11-04 15:00:44 -0800492 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700493 sLocaleConfiguration.locale = locale;
494 sLocaleConfiguration.mcc = mcc;
495 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700496
Joe Onorato0589f0f2010-02-08 13:44:00 -0800497 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700498
499 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
500 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700501 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700502 public void run() {
503 writeConfiguration(Launcher.this, localeConfiguration);
504 }
505 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700506 }
507 }
508
509 private static class LocaleConfiguration {
510 public String locale;
511 public int mcc = -1;
512 public int mnc = -1;
513 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700514
Romain Guy98d01652009-06-30 16:21:04 -0700515 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
516 DataInputStream in = null;
517 try {
518 in = new DataInputStream(context.openFileInput(PREFERENCES));
519 configuration.locale = in.readUTF();
520 configuration.mcc = in.readInt();
521 configuration.mnc = in.readInt();
522 } catch (FileNotFoundException e) {
523 // Ignore
524 } catch (IOException e) {
525 // Ignore
526 } finally {
527 if (in != null) {
528 try {
529 in.close();
530 } catch (IOException e) {
531 // Ignore
532 }
533 }
534 }
535 }
536
537 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
538 DataOutputStream out = null;
539 try {
540 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
541 out.writeUTF(configuration.locale);
542 out.writeInt(configuration.mcc);
543 out.writeInt(configuration.mnc);
544 out.flush();
545 } catch (FileNotFoundException e) {
546 // Ignore
547 } catch (IOException e) {
548 //noinspection ResultOfMethodCallIgnored
549 context.getFileStreamPath(PREFERENCES).delete();
550 } finally {
551 if (out != null) {
552 try {
553 out.close();
554 } catch (IOException e) {
555 // Ignore
556 }
557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 }
559 }
560
Bjorn Bringertc459e522013-06-07 19:36:01 +0100561 public LayoutInflater getInflater() {
562 return mInflater;
563 }
564
Adam Cohen716b51e2011-06-30 12:09:54 -0700565 public DragLayer getDragLayer() {
566 return mDragLayer;
567 }
568
Winson Chung36a62fe2012-05-06 18:04:42 -0700569 boolean isDraggingEnabled() {
570 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
571 // that is subsequently removed from the workspace in startBinding().
572 return !mModel.isLoadingWorkspace();
573 }
574
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 static int getScreen() {
576 synchronized (sLock) {
577 return sScreen;
578 }
579 }
580
581 static void setScreen(int screen) {
582 synchronized (sLock) {
583 sScreen = screen;
584 }
585 }
586
Winson Chung557d6ed2011-07-08 15:34:52 -0700587 /**
588 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
589 * a configuration step, this allows the proper animations to run after other transitions.
590 */
591 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700592 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800593 switch (args.requestCode) {
594 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700595 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700596 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800597 break;
598 case REQUEST_PICK_SHORTCUT:
599 processShortcut(args.intent);
600 break;
601 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700602 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700603 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700604 result = true;
605 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800606 case REQUEST_CREATE_APPWIDGET:
607 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700608 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700609 result = true;
610 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800611 case REQUEST_PICK_WALLPAPER:
612 // We just wanted the activity result here so we can clear mWaitingForResult
613 break;
614 }
Michael Jurka27614d22012-04-02 06:40:22 -0700615 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
616 // if you turned the screen off and then back while in All Apps, Launcher would not
617 // return to the workspace. Clearing mAddInfo.container here fixes this issue
618 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700619 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800620 }
621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700623 protected void onActivityResult(
624 final int requestCode, final int resultCode, final Intent data) {
625 if (requestCode == REQUEST_BIND_APPWIDGET) {
626 int appWidgetId = data != null ?
627 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
628 if (resultCode == RESULT_CANCELED) {
629 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
630 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700631 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700632 }
633 return;
634 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700635 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
637 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700638 mWaitingForResult = false;
639
Adam Cohened66b2b2012-01-23 17:28:51 -0800640 // We have special handling for widgets
641 if (isWidgetDrop) {
642 int appWidgetId = data != null ?
643 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700644 if (appWidgetId < 0) {
645 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
646 "widget configuration activity.");
647 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
648 } else {
649 completeTwoStageWidgetDrop(resultCode, appWidgetId);
650 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800651 return;
652 }
653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 // The pattern used here is that a user PICKs a specific application,
655 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
658 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700659 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800660 final PendingAddArguments args = new PendingAddArguments();
661 args.requestCode = requestCode;
662 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700663 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700664 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700665 args.cellX = mPendingAddInfo.cellX;
666 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800667 if (isWorkspaceLocked()) {
668 sPendingAddList.add(args);
669 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700670 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700674 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800675 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
676 null);
677 }
678
679 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700680 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 Runnable onCompleteRunnable = null;
683 int animationType = 0;
684
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700685 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 if (resultCode == RESULT_OK) {
687 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
688 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700689 mPendingAddWidgetInfo);
690 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800691 onCompleteRunnable = new Runnable() {
692 @Override
693 public void run() {
694 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700695 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800696 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
697 null);
698 }
699 };
700 } else if (resultCode == RESULT_CANCELED) {
701 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
702 onCompleteRunnable = new Runnable() {
703 @Override
704 public void run() {
705 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
706 null);
707 }
708 };
709 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700710 if (mDragLayer.getAnimatedView() != null) {
711 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
712 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
713 animationType, boundWidget, true);
714 } else {
715 // The animated view may be null in the case of a rotation during widget configuration
716 onCompleteRunnable.run();
717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 }
719
720 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700721 protected void onStop() {
722 super.onStop();
723 FirstFrameAnimatorHelper.setIsVisible(false);
724 }
725
726 @Override
727 protected void onStart() {
728 super.onStart();
729 FirstFrameAnimatorHelper.setIsVisible(true);
730 }
731
732 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200734 long startTime = 0;
735 if (DEBUG_RESUME_TIME) {
736 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400737 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700740
Winson Chung4a2afa32012-07-19 14:53:05 -0700741 // Restore the previous launcher state
742 if (mOnResumeState == State.WORKSPACE) {
743 showWorkspace(false);
744 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
745 showAllApps(false);
746 }
747 mOnResumeState = State.NONE;
748
Craig Mautner360310b2012-10-26 15:13:08 -0700749 // Background was set to gradient in onPause(), restore to black if in all apps.
750 setWorkspaceBackground(mState == State.WORKSPACE);
751
Winson Chungde0fb8f2012-05-08 14:37:08 -0700752 // Process any items that were added while Launcher was away
753 InstallShortcutReceiver.flushInstallQueue(this);
754
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800755 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700756 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700757 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700759 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700761 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200763 if (mOnResumeCallbacks.size() > 0) {
764 // We might have postponed some bind calls until onResume (see waitUntilResume) --
765 // execute them here
766 long startTimeCallbacks = 0;
767 if (DEBUG_RESUME_TIME) {
768 startTimeCallbacks = System.currentTimeMillis();
769 }
770
771 if (mAppsCustomizeContent != null) {
772 mAppsCustomizeContent.setBulkBind(true);
773 }
774 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
775 mOnResumeCallbacks.get(i).run();
776 }
777 if (mAppsCustomizeContent != null) {
778 mAppsCustomizeContent.setBulkBind(false);
779 }
780 mOnResumeCallbacks.clear();
781 if (DEBUG_RESUME_TIME) {
782 Log.d(TAG, "Time spent processing callbacks in onResume: " +
783 (System.currentTimeMillis() - startTimeCallbacks));
784 }
Michael Jurka447bf842013-05-15 14:52:15 +0200785 }
Winson Chunge4e50662012-01-23 14:45:13 -0800786
787 // Reset the pressed state of icons that were locked in the press state while activities
788 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800789 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800790 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800791 mWaitingForResume.setStayPressed(false);
792 }
Winson Chunge4e50662012-01-23 14:45:13 -0800793 if (mAppsCustomizeContent != null) {
794 // Resets the previous all apps icon press state
795 mAppsCustomizeContent.resetDrawableState();
796 }
Adam Cohen06dff352012-06-01 17:17:08 -0700797 // It is possible that widgets can receive updates while launcher is not in the foreground.
798 // Consequently, the widgets will be inflated in the orientation of the foreground activity
799 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
800 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700801 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700802
803 // Again, as with the above scenario, it's possible that one or more of the global icons
804 // were updated in the wrong orientation.
805 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200806 if (DEBUG_RESUME_TIME) {
807 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
808 }
Adam Cohen06dff352012-06-01 17:17:08 -0700809 }
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700812 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700813 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
814 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
815 // when Launcher resumes and we are still in AllApps.
816 updateWallpaperVisibility(true);
817
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700818 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700819 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800820 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700821 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700822 }
Romain Guycbb89e42009-06-08 15:52:54 -0700823
Adam Cohen66a01fd2013-06-11 12:48:00 -0700824 protected void onFinishBindingItems() {
825 }
826
827 // Add a fullscreen unpadded view to the workspace to the left all other screens.
828 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700829 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700830 }
831
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700832 @Override
833 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400834 // Flag the loader to stop early before switching
835 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700836 if (mAppsCustomizeContent != null) {
837 mAppsCustomizeContent.surrender();
838 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800839 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700840 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700841
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800842 // We can't hide the IME if it was forced open. So don't bother
843 /*
844 @Override
845 public void onWindowFocusChanged(boolean hasFocus) {
846 super.onWindowFocusChanged(hasFocus);
847
848 if (hasFocus) {
849 final InputMethodManager inputManager = (InputMethodManager)
850 getSystemService(Context.INPUT_METHOD_SERVICE);
851 WindowManager.LayoutParams lp = getWindow().getAttributes();
852 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
853 android.os.Handler()) {
854 protected void onReceiveResult(int resultCode, Bundle resultData) {
855 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
856 }
857 });
858 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
859 }
860 }
861 */
862
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 private boolean acceptFilter() {
864 final InputMethodManager inputManager = (InputMethodManager)
865 getSystemService(Context.INPUT_METHOD_SERVICE);
866 return !inputManager.isFullscreenMode();
867 }
868
869 @Override
870 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700871 final int uniChar = event.getUnicodeChar();
872 final boolean handled = super.onKeyDown(keyCode, event);
873 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
874 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
876 keyCode, event);
877 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700878 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700879 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700880 // showSearchDialog()
881 // If there are multiple keystrokes before the search dialog takes focus,
882 // onSearchRequested() will be called for every keystroke,
883 // but it is idempotent, so it's fine.
884 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886 }
887
Joe Onorato8a9625e2010-01-28 15:55:35 -0800888 // Eat the long press event so the keyboard doesn't come up.
889 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
890 return true;
891 }
892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 return handled;
894 }
895
Karl Rosaen138a0412009-04-23 19:00:21 -0700896 private String getTypedText() {
897 return mDefaultKeySsb.toString();
898 }
899
900 private void clearTypedText() {
901 mDefaultKeySsb.clear();
902 mDefaultKeySsb.clearSpans();
903 Selection.setSelection(mDefaultKeySsb, 0);
904 }
905
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700907 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
908 * State
909 */
910 private static State intToState(int stateOrdinal) {
911 State state = State.WORKSPACE;
912 final State[] stateValues = State.values();
913 for (int i = 0; i < stateValues.length; i++) {
914 if (stateValues[i].ordinal() == stateOrdinal) {
915 state = stateValues[i];
916 break;
917 }
918 }
919 return state;
920 }
921
922 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 * Restores the previous state, if it exists.
924 *
925 * @param savedState The previous state.
926 */
927 private void restoreState(Bundle savedState) {
928 if (savedState == null) {
929 return;
930 }
931
Michael Jurka883f55b2010-10-21 15:47:14 -0700932 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700933 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700934 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800935 }
936
Winson Chungf0c6ae02012-03-21 16:10:31 -0700937 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700939 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700944
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
946 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700947 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700948 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
949 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
951 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
952 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700953 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 mRestoring = true;
955 }
956
957 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
958 if (renameFolder) {
959 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700960 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 mRestoring = true;
962 }
Winson Chunga12a2502010-12-20 14:41:35 -0800963
Winson Chung785d2eb2011-04-14 16:08:02 -0700964 // Restore the AppsCustomize tab
965 if (mAppsCustomizeTabHost != null) {
966 String curTab = savedState.getString("apps_customize_currentTab");
967 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700968 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700969 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700970 mAppsCustomizeContent.loadAssociatedPages(
971 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700972 }
973
Winson Chung5afbf7b2011-07-25 11:53:08 -0700974 int currentIndex = savedState.getInt("apps_customize_currentIndex");
975 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
978
979 /**
980 * Finds all the views we need and configure them properly.
981 */
982 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700983 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400984
Craig Mautner360310b2012-10-26 15:13:08 -0700985 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700986 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
987 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700988 mQsbDivider = findViewById(R.id.qsb_divider);
989 mDockDivider = findViewById(R.id.dock_divider);
990
991 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
992 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993
Winson Chung4c98d922011-05-31 16:50:48 -0700994 // Setup the drag layer
995 mDragLayer.setup(this, dragController);
996
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 // Setup the hotseat
998 mHotseat = (Hotseat) findViewById(R.id.hotseat);
999 if (mHotseat != null) {
1000 mHotseat.setup(this);
1001 }
1002
Winson Chung4c98d922011-05-31 16:50:48 -07001003 // Setup the workspace
1004 mWorkspace.setHapticFeedbackEnabled(false);
1005 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001006 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001007 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001008
Winson Chungf0ea4d32011-06-06 14:27:16 -07001009 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001010 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001011
Winson Chungf0ea4d32011-06-06 14:27:16 -07001012 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001013 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001014 mAppsCustomizeContent = (AppsCustomizePagedView)
1015 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1016 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001017
Winson Chung3d503fb2011-07-13 17:25:49 -07001018 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001019 dragController.setDragScoller(mWorkspace);
1020 dragController.setScrollView(mDragLayer);
1021 dragController.setMoveTarget(mWorkspace);
1022 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001023 if (mSearchDropTargetBar != null) {
1024 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001025 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001026
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001027 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001028 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001029 final View ww = new WeightWatcher(this);
1030 ww.setAlpha(0.5f);
1031 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001032 new FrameLayout.LayoutParams(
1033 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001034 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001035 Gravity.BOTTOM)
1036 );
1037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
1039
1040 /**
1041 * Creates a view representing a shortcut.
1042 *
1043 * @param info The data structure describing the shortcut.
1044 *
1045 * @return A View inflated from R.layout.application.
1046 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001047 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001049 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
1051
1052 /**
1053 * Creates a view representing a shortcut inflated from the specified resource.
1054 *
1055 * @param layoutResId The id of the XML layout used to create the shortcut.
1056 * @param parent The group the shortcut belongs to.
1057 * @param info The data structure describing the shortcut.
1058 *
1059 * @return A View inflated from layoutResId.
1060 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001061 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001062 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1063 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 return favorite;
1066 }
1067
1068 /**
1069 * Add an application shortcut to the workspace.
1070 *
1071 * @param data The intent describing the application.
1072 * @param cellInfo The position on screen where to create the shortcut.
1073 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001074 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001075 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001076 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001077
Adam Cohenfbba09b2011-07-18 21:43:05 -07001078 // First we check if we already know the exact location where we want to add this item.
1079 if (cellX >= 0 && cellY >= 0) {
1080 cellXY[0] = cellX;
1081 cellXY[1] = cellY;
1082 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001083 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001084 return;
1085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001087 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001088
Romain Guy73b979d2009-06-09 12:57:21 -07001089 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001090 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001092 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001093 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001094 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001095 } else {
1096 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
1098 }
Romain Guycbb89e42009-06-08 15:52:54 -07001099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 /**
1101 * Add a shortcut to the workspace.
1102 *
1103 * @param data The intent describing the shortcut.
1104 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001106 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001107 int cellY) {
1108 int[] cellXY = mTmpAddItemCellCoordinates;
1109 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001110 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001111
Michael Jurkad3ef3062010-11-23 16:23:58 -08001112 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001113
Adam Cohen558baaf2011-08-15 15:22:57 -07001114 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001115 if (info == null) {
1116 return;
1117 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001118 final View view = createShortcut(info);
1119
Adam Cohenfbba09b2011-07-18 21:43:05 -07001120 // First we check if we already know the exact location where we want to add this item.
1121 if (cellX >= 0 && cellY >= 0) {
1122 cellXY[0] = cellX;
1123 cellXY[1] = cellY;
1124 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001125
1126 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001127 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001128 true, null,null)) {
1129 return;
1130 }
1131 DragObject dragObject = new DragObject();
1132 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001133 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1134 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001135 return;
1136 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001137 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001138 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001139 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001140 foundCellSpan = (result != null);
1141 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001142 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001143 }
1144
1145 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001146 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001147 return;
1148 }
1149
Adam Cohendcd297f2013-06-18 13:13:40 -07001150 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151
1152 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001153 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001154 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
1156 }
1157
Adam Cohen2f093b62012-04-30 18:59:53 -07001158 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1159 int minHeight) {
1160 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001161 // We want to account for the extra amount of padding that we are adding to the widget
1162 // to ensure that it gets the full amount of space that it has requested
1163 int requiredWidth = minWidth + padding.left + padding.right;
1164 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001165 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001166 }
1167
Adam Cohen2f093b62012-04-30 18:59:53 -07001168 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1169 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001170 }
1171
Adam Cohen2f093b62012-04-30 18:59:53 -07001172 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1173 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001174 }
1175
Adam Cohen2f093b62012-04-30 18:59:53 -07001176 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1177 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001178 }
1179
Adam Cohen2f093b62012-04-30 18:59:53 -07001180 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1181 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001182 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001183 }
1184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001186 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001188 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001189 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001191 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001192 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1193 if (appWidgetInfo == null) {
1194 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1195 }
Romain Guycbb89e42009-06-08 15:52:54 -07001196
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001197 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001198 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001199
Adam Cohen2f093b62012-04-30 18:59:53 -07001200 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1201 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001204 // We have saved the position to which the widget was dragged-- this really only matters
1205 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001206 int[] cellXY = mTmpAddItemCellCoordinates;
1207 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001208 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001209 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001210 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1211 cellXY[0] = mPendingAddInfo.cellX;
1212 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001213 spanXY[0] = mPendingAddInfo.spanX;
1214 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001215 foundCellSpan = true;
1216 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001217 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001218 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001219 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1220 spanXY[1], cellXY, finalSpan);
1221 spanXY[0] = finalSpan[0];
1222 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001223 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001224 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001225 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001226 }
1227
Michael Jurka0280c3b2010-09-17 15:00:07 -07001228 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001229 if (appWidgetId != -1) {
1230 // Deleting an app widget ID is a void call but writes to disk before returning
1231 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001232 new Thread("deleteAppWidgetId") {
1233 public void run() {
1234 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1235 }
1236 }.start();
1237 }
Winson Chung93eef082012-03-23 15:59:27 -07001238 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001239 return;
1240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001242 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001243 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1244 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001245 launcherInfo.spanX = spanXY[0];
1246 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001247 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1248 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001251 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252
1253 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001254 if (hostView == null) {
1255 // Perform actual inflation because we're live
1256 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1257 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1258 } else {
1259 // The AppWidgetHostView has already been inflated and instantiated
1260 launcherInfo.hostView = hostView;
1261 }
Romain Guycbb89e42009-06-08 15:52:54 -07001262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001264 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001265 launcherInfo.notifyWidgetSizeChanged(this);
1266
Adam Cohendcd297f2013-06-18 13:13:40 -07001267 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001268 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001269
1270 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001272 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
Romain Guycbb89e42009-06-08 15:52:54 -07001274
Adam Cohended9f8d2010-11-03 13:25:16 -07001275 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1276 @Override
1277 public void onReceive(Context context, Intent intent) {
1278 final String action = intent.getAction();
1279 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1280 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001281 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001282 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001283
Winson Chungc100e8e2011-08-09 16:02:43 -07001284 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001285 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001286 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1287 mAppsCustomizeTabHost.reset();
1288 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001289 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001290 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1291 mUserPresent = true;
1292 updateRunning();
1293 }
1294 }
1295 };
1296
1297 @Override
1298 public void onAttachedToWindow() {
1299 super.onAttachedToWindow();
1300
1301 // Listen for broadcasts related to user-presence
1302 final IntentFilter filter = new IntentFilter();
1303 filter.addAction(Intent.ACTION_SCREEN_OFF);
1304 filter.addAction(Intent.ACTION_USER_PRESENT);
1305 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001306 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001307 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001308 mVisible = true;
1309 }
1310
1311 @Override
1312 public void onDetachedFromWindow() {
1313 super.onDetachedFromWindow();
1314 mVisible = false;
1315
Adam Cohend113e0c2010-11-11 10:48:05 -08001316 if (mAttached) {
1317 unregisterReceiver(mReceiver);
1318 mAttached = false;
1319 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001320 updateRunning();
1321 }
1322
1323 public void onWindowVisibilityChanged(int visibility) {
1324 mVisible = visibility == View.VISIBLE;
1325 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001326 // The following code used to be in onResume, but it turns out onResume is called when
1327 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1328 // is a more appropriate event to handle
1329 if (mVisible) {
1330 mAppsCustomizeTabHost.onWindowVisible();
1331 if (!mWorkspaceLoading) {
1332 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001333 // We want to let Launcher draw itself at least once before we force it to build
1334 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001335 // apps is nice and speedy.
1336 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001337 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001338 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001339 if (mStarted) return;
1340 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001341 // We delay the layer building a bit in order to give
1342 // other message processing a time to run. In particular
1343 // this avoids a delay in hiding the IME if it was
1344 // currently shown, because doing that may involve
1345 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001346 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001347 final ViewTreeObserver.OnDrawListener listener = this;
1348 mWorkspace.post(new Runnable() {
1349 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001350 if (mWorkspace != null &&
1351 mWorkspace.getViewTreeObserver() != null) {
1352 mWorkspace.getViewTreeObserver().
1353 removeOnDrawListener(listener);
1354 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001355 }
1356 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001357 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001358 }
1359 });
1360 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001361 // When Launcher comes back to foreground, a different Activity might be responsible for
1362 // the app market intent, so refresh the icon
1363 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001364 clearTypedText();
1365 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001366 }
1367
1368 private void sendAdvanceMessage(long delay) {
1369 mHandler.removeMessages(ADVANCE_MSG);
1370 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1371 mHandler.sendMessageDelayed(msg, delay);
1372 mAutoAdvanceSentTime = System.currentTimeMillis();
1373 }
1374
1375 private void updateRunning() {
1376 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1377 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1378 mAutoAdvanceRunning = autoAdvanceRunning;
1379 if (autoAdvanceRunning) {
1380 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1381 sendAdvanceMessage(delay);
1382 } else {
1383 if (!mWidgetsToAdvance.isEmpty()) {
1384 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1385 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1386 }
1387 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001388 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001389 }
1390 }
1391 }
1392
1393 private final Handler mHandler = new Handler() {
1394 @Override
1395 public void handleMessage(Message msg) {
1396 if (msg.what == ADVANCE_MSG) {
1397 int i = 0;
1398 for (View key: mWidgetsToAdvance.keySet()) {
1399 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1400 final int delay = mAdvanceStagger * i;
1401 if (v instanceof Advanceable) {
1402 postDelayed(new Runnable() {
1403 public void run() {
1404 ((Advanceable) v).advance();
1405 }
1406 }, delay);
1407 }
1408 i++;
1409 }
1410 sendAdvanceMessage(mAdvanceInterval);
1411 }
1412 }
1413 };
1414
1415 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001416 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001417 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1418 if (v instanceof Advanceable) {
1419 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001420 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001421 updateRunning();
1422 }
1423 }
1424
1425 void removeWidgetToAutoAdvance(View hostView) {
1426 if (mWidgetsToAdvance.containsKey(hostView)) {
1427 mWidgetsToAdvance.remove(hostView);
1428 updateRunning();
1429 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001430 }
1431
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001433 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 launcherInfo.hostView = null;
1435 }
1436
Winson Chung93eef082012-03-23 15:59:27 -07001437 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1438 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1439 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001440 }
1441
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001442 public LauncherAppWidgetHost getAppWidgetHost() {
1443 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
Romain Guycbb89e42009-06-08 15:52:54 -07001445
Winson Chunga9abd0e2010-10-27 17:18:37 -07001446 public LauncherModel getModel() {
1447 return mModel;
1448 }
1449
Bjorn Bringertc459e522013-06-07 19:36:01 +01001450 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001451 getWindow().closeAllPanels();
1452
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001453 // Whatever we were doing is hereby canceled.
1454 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001455 }
1456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 @Override
1458 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001459 long startTime = 0;
1460 if (DEBUG_RESUME_TIME) {
1461 startTime = System.currentTimeMillis();
1462 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 super.onNewIntent(intent);
1464
1465 // Close the menu
1466 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001467 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001468 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001469
Jamie Genniscb222e82012-10-23 15:44:26 -07001470 final boolean alreadyOnHome =
1471 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001472 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001473
Jamie Genniscb222e82012-10-23 15:44:26 -07001474 Runnable processIntent = new Runnable() {
1475 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001476 if (mWorkspace == null) {
1477 // Can be cases where mWorkspace is null, this prevents a NPE
1478 return;
1479 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001480 Folder openFolder = mWorkspace.getOpenFolder();
1481 // In all these cases, only animate if we're already on home
1482 mWorkspace.exitWidgetResizeMode();
1483 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1484 openFolder == null) {
1485 mWorkspace.moveToDefaultScreen(true);
1486 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001487
Jamie Genniscb222e82012-10-23 15:44:26 -07001488 closeFolder();
1489 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001490
Jamie Genniscb222e82012-10-23 15:44:26 -07001491 // If we are already on home, then just animate back to the workspace,
1492 // otherwise, just wait until onResume to set the state back to Workspace
1493 if (alreadyOnHome) {
1494 showWorkspace(true);
1495 } else {
1496 mOnResumeState = State.WORKSPACE;
1497 }
1498
1499 final View v = getWindow().peekDecorView();
1500 if (v != null && v.getWindowToken() != null) {
1501 InputMethodManager imm = (InputMethodManager)getSystemService(
1502 INPUT_METHOD_SERVICE);
1503 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1504 }
1505
1506 // Reset AllApps to its initial state
1507 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1508 mAppsCustomizeTabHost.reset();
1509 }
1510 }
1511 };
1512
1513 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1514 // Delay processing of the intent to allow the status bar animation to finish
1515 // first in order to avoid janky animations.
1516 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001517 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001518 // Process the intent immediately.
1519 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001520 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 }
Michael Jurka447bf842013-05-15 14:52:15 +02001523 if (DEBUG_RESUME_TIME) {
1524 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
1527
1528 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001529 public void onRestoreInstanceState(Bundle state) {
1530 super.onRestoreInstanceState(state);
1531 for (int page: mSynchronouslyBoundPages) {
1532 mWorkspace.restoreInstanceStateForChild(page);
1533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
1535
1536 @Override
1537 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001538 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001539 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540
Michael Jurka883f55b2010-10-21 15:47:14 -07001541 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001542 // We close any open folder since it will not be re-opened, and we need to make sure
1543 // this state is reflected.
1544 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 mWaitingForResult) {
1548 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001549 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001550 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1551 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001552 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1553 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1554 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556
1557 if (mFolderInfo != null && mWaitingForResult) {
1558 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1559 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1560 }
Michael Jurka946ad472010-07-09 18:05:18 -07001561
Winson Chung785d2eb2011-04-14 16:08:02 -07001562 // Save the current AppsCustomize tab
1563 if (mAppsCustomizeTabHost != null) {
1564 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1565 if (currentTabTag != null) {
1566 outState.putString("apps_customize_currentTab", currentTabTag);
1567 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001568 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1569 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
1572
1573 @Override
1574 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001576
Winson Chunge7a03942011-08-05 15:05:12 -07001577 // Remove all pending runnables
1578 mHandler.removeMessages(ADVANCE_MSG);
1579 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001580 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001581
Winson Chungcd2b0142011-06-08 16:02:26 -07001582 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001583 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001584 mModel.stopLoader();
1585 app.setLauncher(null);
1586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001588 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001590 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001592 mAppWidgetHost = null;
1593
1594 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595
1596 TextKeyListener.getInstance().release();
1597
Winson Chung81b52252012-08-27 15:34:29 -07001598 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1599 // to prevent leaking Launcher activities on orientation change.
1600 if (mModel != null) {
1601 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1602 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001603
1604 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001605 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001606
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001607 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001608 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1609 mWorkspace.removeAllViews();
1610 mWorkspace = null;
1611 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001612
Michael Jurka2ecf9952012-06-18 12:52:28 -07001613 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 }
1615
Adam Cohena9cf38f2011-05-02 15:36:58 -07001616 public DragController getDragController() {
1617 return mDragController;
1618 }
1619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 @Override
1621 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001622 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 super.startActivityForResult(intent, requestCode);
1624 }
1625
Winson Chung70d72102011-08-12 11:24:35 -07001626 /**
1627 * Indicates that we want global search for this activity by setting the globalSearch
1628 * argument for {@link #startSearch} to true.
1629 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001631 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001633
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001634 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001635
Karl Rosaen138a0412009-04-23 19:00:21 -07001636 if (initialQuery == null) {
1637 // Use any text typed in the launcher as the initial query
1638 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 if (appSearchData == null) {
1641 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001642 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 }
Winson Chungadf0c182012-08-23 14:59:07 -07001644 Rect sourceBounds = new Rect();
1645 if (mSearchDropTargetBar != null) {
1646 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1647 }
Romain Guycbb89e42009-06-08 15:52:54 -07001648
Bjorn Bringertc459e522013-06-07 19:36:01 +01001649 startSearch(initialQuery, selectInitialQuery,
1650 appSearchData, sourceBounds);
1651 }
1652
1653 public void startSearch(String initialQuery,
1654 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001655 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001656 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001657 }
1658
1659 /**
1660 * Starts the global search activity. This code is a copied from SearchManager
1661 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001662 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001663 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001664 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001665 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1666 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1667 if (globalSearchActivity == null) {
1668 Log.w(TAG, "No global search activity found.");
1669 return;
1670 }
1671 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1672 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1673 intent.setComponent(globalSearchActivity);
1674 // Make sure that we have a Bundle to put source in
1675 if (appSearchData == null) {
1676 appSearchData = new Bundle();
1677 } else {
1678 appSearchData = new Bundle(appSearchData);
1679 }
1680 // Set source to package name of app that starts global search, if not set already.
1681 if (!appSearchData.containsKey("source")) {
1682 appSearchData.putString("source", getPackageName());
1683 }
1684 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1685 if (!TextUtils.isEmpty(initialQuery)) {
1686 intent.putExtra(SearchManager.QUERY, initialQuery);
1687 }
1688 if (selectInitialQuery) {
1689 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1690 }
1691 intent.setSourceBounds(sourceBounds);
1692 try {
1693 startActivity(intent);
1694 } catch (ActivityNotFoundException ex) {
1695 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 }
1698
Winson Chung70d72102011-08-12 11:24:35 -07001699 @Override
1700 public boolean onCreateOptionsMenu(Menu menu) {
1701 if (isWorkspaceLocked()) {
1702 return false;
1703 }
1704
1705 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001706
1707 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1708 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1709 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001710 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1711 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1712 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001713 String helpUrl = getString(R.string.help_url);
1714 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001715 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1716 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1717
Winson Chung70d72102011-08-12 11:24:35 -07001718 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1719 .setIcon(android.R.drawable.ic_menu_gallery)
1720 .setAlphabeticShortcut('W');
1721 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1722 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001723 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001724 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001725 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1726 .setIcon(android.R.drawable.ic_menu_preferences)
1727 .setIntent(settings)
1728 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001729 if (!helpUrl.isEmpty()) {
1730 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1731 .setIcon(android.R.drawable.ic_menu_help)
1732 .setIntent(help)
1733 .setAlphabeticShortcut('H');
1734 }
Winson Chung70d72102011-08-12 11:24:35 -07001735 return true;
1736 }
1737
1738 @Override
1739 public boolean onPrepareOptionsMenu(Menu menu) {
1740 super.onPrepareOptionsMenu(menu);
1741
1742 if (mAppsCustomizeTabHost.isTransitioning()) {
1743 return false;
1744 }
1745 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1746 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1747
1748 return true;
1749 }
1750
1751 @Override
1752 public boolean onOptionsItemSelected(MenuItem item) {
1753 switch (item.getItemId()) {
1754 case MENU_WALLPAPER_SETTINGS:
1755 startWallpaper();
1756 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001757 }
1758
1759 return super.onOptionsItemSelected(item);
1760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001762 @Override
1763 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001764 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001765 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001766 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001767 }
1768
Joe Onorato9c1289c2009-08-17 11:03:03 -04001769 public boolean isWorkspaceLocked() {
1770 return mWorkspaceLoading || mWaitingForResult;
1771 }
1772
Michael Jurka0280c3b2010-09-17 15:00:07 -07001773 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001774 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001775 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001776 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1777 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001778 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001779 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001780 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001781
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001782 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1783 AppWidgetProviderInfo appWidgetInfo) {
1784 if (appWidgetInfo.configure != null) {
1785 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001786
Bjorn Bringert7984c942009-12-09 15:38:25 +00001787 // Launch over to configure widget, if needed
1788 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001789 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001790 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001791 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001793 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001794 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001795 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001796 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001797 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
1799 }
Romain Guycbb89e42009-06-08 15:52:54 -07001800
Adam Cohenfbba09b2011-07-18 21:43:05 -07001801 /**
1802 * Process a shortcut drop.
1803 *
1804 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001805 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001806 * @param cell The cell it should be added to, optional
1807 * @param position The location on the screen where it was dropped, optional
1808 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001809 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001810 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001811 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001812 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001813 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001814 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001815
1816 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001817 mPendingAddInfo.cellX = cell[0];
1818 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001819 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001820
1821 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1822 createShortcutIntent.setComponent(componentName);
1823 processShortcut(createShortcutIntent);
1824 }
1825
Adam Cohenfbba09b2011-07-18 21:43:05 -07001826 /**
1827 * Process a widget drop.
1828 *
1829 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001830 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001831 * @param cell The cell it should be added to, optional
1832 * @param position The location on the screen where it was dropped, optional
1833 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001834 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001835 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001836 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001837 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001838 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001839 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001840 mPendingAddInfo.minSpanX = info.minSpanX;
1841 mPendingAddInfo.minSpanY = info.minSpanY;
1842
Adam Cohenfbba09b2011-07-18 21:43:05 -07001843 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001844 mPendingAddInfo.cellX = cell[0];
1845 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001846 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001847 if (span != null) {
1848 mPendingAddInfo.spanX = span[0];
1849 mPendingAddInfo.spanY = span[1];
1850 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001851
Adam Cohened66b2b2012-01-23 17:28:51 -08001852 AppWidgetHostView hostView = info.boundWidget;
1853 int appWidgetId;
1854 if (hostView != null) {
1855 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001856 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001857 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001858 // In this case, we either need to start an activity to get permission to bind
1859 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001860 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001861 Bundle options = info.bindOptions;
1862
1863 boolean success = false;
1864 if (options != null) {
1865 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1866 info.componentName, options);
1867 } else {
1868 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1869 info.componentName);
1870 }
1871 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001872 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001873 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001874 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001875 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1876 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1877 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001878 // TODO: we need to make sure that this accounts for the options bundle.
1879 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001880 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1881 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001882 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001883 }
1884
Joe Onoratodeb98af2010-02-19 14:59:39 -08001885 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001886 // Handle case where user selected "Applications"
1887 String applicationName = getResources().getString(R.string.group_applications);
1888 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001889
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001890 if (applicationName != null && applicationName.equals(shortcutName)) {
1891 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1892 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001893
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001894 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1895 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001896 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001897 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001898 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001899 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
1902
Winson Chung24ab2f12010-09-16 14:10:47 -07001903 void processWallpaper(Intent intent) {
1904 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1905 }
1906
Adam Cohendcd297f2013-06-18 13:13:40 -07001907 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001908 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001909 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 folderInfo.title = getText(R.string.folder_name);
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001913 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001914 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001915 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916
1917 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001918 FolderIcon newFolder =
1919 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001920 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001922 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 }
Romain Guycbb89e42009-06-08 15:52:54 -07001924
Joe Onorato9c1289c2009-08-17 11:03:03 -04001925 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001926 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927 }
1928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001930 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001932 Intent chooser = Intent.createChooser(pickWallpaper,
1933 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001934 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1935 // Removed in Eclair MR1
1936// WallpaperManager wm = (WallpaperManager)
1937// getSystemService(Context.WALLPAPER_SERVICE);
1938// WallpaperInfo wi = wm.getWallpaperInfo();
1939// if (wi != null && wi.getSettingsActivity() != null) {
1940// LabeledIntent li = new LabeledIntent(getPackageName(),
1941// R.string.configure_wallpaper, 0);
1942// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1943// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1944// }
Mike Clerona0618e42009-10-22 13:55:21 -07001945 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001948 /**
1949 * Registers various content observers. The current implementation registers
1950 * only a favorites observer to keep track of the favorites applications.
1951 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001952 private void registerContentObservers() {
1953 ContentResolver resolver = getContentResolver();
1954 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1955 true, mWidgetObserver);
1956 }
1957
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 @Override
1959 public boolean dispatchKeyEvent(KeyEvent event) {
1960 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1961 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001963 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001964 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001965 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001966 dumpState();
1967 return true;
1968 }
1969 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001970 }
1971 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1972 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001973 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 return true;
1975 }
1976 }
1977
1978 return super.dispatchKeyEvent(event);
1979 }
1980
Joe Onorato88ec0992009-11-19 13:16:06 -08001981 @Override
1982 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001983 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001984 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001985 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001986 Folder openFolder = mWorkspace.getOpenFolder();
1987 if (openFolder.isEditingName()) {
1988 openFolder.dismissEditingName();
1989 } else {
1990 closeFolder();
1991 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001992 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001993 mWorkspace.exitWidgetResizeMode();
1994
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001995 // Back button is a no-op here, but give at least some feedback for the button press
1996 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001997 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001998 }
1999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002001 * Re-listen when widgets are reset.
2002 */
2003 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002004 if (mAppWidgetHost != null) {
2005 mAppWidgetHost.startListening();
2006 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002007 }
2008
2009 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 * Launches the intent referred by the clicked shortcut.
2011 *
2012 * @param v The view representing the clicked shortcut.
2013 */
2014 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002015 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2016 // view has detached (it's possible for this to happen if the view is removed mid touch).
2017 if (v.getWindowToken() == null) {
2018 return;
2019 }
2020
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002021 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002022 return;
2023 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002026 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002028 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002029
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002030 // Check for special shortcuts
2031 if (intent.getComponent() != null) {
2032 final String shortcutClass = intent.getComponent().getClassName();
2033
2034 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2035 showAllApps(true);
2036 return;
2037 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2038 MemoryDumpActivity.startDump(this);
2039 return;
2040 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002041 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002042
2043 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002044 int[] pos = new int[2];
2045 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002046 intent.setSourceBounds(new Rect(pos[0], pos[1],
2047 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002048
2049 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002050
2051 if (success && v instanceof BubbleTextView) {
2052 mWaitingForResume = (BubbleTextView) v;
2053 mWaitingForResume.setStayPressed(true);
2054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002056 if (v instanceof FolderIcon) {
2057 FolderIcon fi = (FolderIcon) v;
2058 handleFolderClick(fi);
2059 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002060 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002061 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002062 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002063 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002064 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
2067 }
2068
Michael Jurka0e260592010-06-30 17:07:39 -07002069 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002070 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002071 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002072 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2073 showWorkspace(true);
2074 }
Michael Jurka0e260592010-06-30 17:07:39 -07002075 return false;
2076 }
2077
Michael Jurkaaf442092010-06-10 17:01:57 -07002078 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002079 * Event handler for the search button
2080 *
2081 * @param v The view that was clicked.
2082 */
2083 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002084 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2085
Amith Yamasania135ba82011-08-09 17:42:01 -07002086 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002087 }
2088
2089 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002090 * Event handler for the voice button
2091 *
2092 * @param v The view that was clicked.
2093 */
2094 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002095 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002096
Bjorn Bringertc459e522013-06-07 19:36:01 +01002097 startVoice();
2098 }
2099
2100 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002101 try {
2102 final SearchManager searchManager =
2103 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2104 ComponentName activityName = searchManager.getGlobalSearchActivity();
2105 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002106 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002107 if (activityName != null) {
2108 intent.setPackage(activityName.getPackageName());
2109 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002110 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002111 } catch (ActivityNotFoundException e) {
2112 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002113 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002114 startActivitySafely(null, intent, "onClickVoiceButton");
2115 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002116 }
2117
2118 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002119 * Event handler for the "grid" button that appears on the home screen, which
2120 * enters all apps mode.
2121 *
2122 * @param v The view that was clicked.
2123 */
2124 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002125 showAllApps(true);
2126 }
2127
2128 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002129 // Provide the same haptic feedback that the system offers for virtual keys.
2130 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002131 }
2132
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002133 public void onClickAppMarketButton(View v) {
2134 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002135 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002136 } else {
2137 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002138 }
2139 }
2140
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002141 void startApplicationDetailsActivity(ComponentName componentName) {
2142 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002143 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2144 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002145 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002146 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002147 }
2148
Patrick Dubroy5539af72010-09-07 15:22:01 -07002149 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2150 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2151 // System applications cannot be installed. For now, show a toast explaining that.
2152 // We may give them the option of disabling apps this way.
2153 int messageId = R.string.uninstall_system_app_text;
2154 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2155 } else {
2156 String packageName = appInfo.componentName.getPackageName();
2157 String className = appInfo.componentName.getClassName();
2158 Intent intent = new Intent(
2159 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002160 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2161 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002162 startActivity(intent);
2163 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002164 }
2165
Michael Jurka86a720e2012-05-09 11:23:23 -07002166 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002170 // Only launch using the new animation if the shortcut has not opted out (this is a
2171 // private contract between launcher and may be ignored in the future).
2172 boolean useLaunchAnimation = (v != null) &&
2173 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2174 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002175 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2176 v.getMeasuredWidth(), v.getMeasuredHeight());
2177
2178 startActivity(intent, opts.toBundle());
2179 } else {
2180 startActivity(intent);
2181 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002182 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 } catch (SecurityException e) {
2184 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002185 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002187 "or use the exported attribute for this activity. "
2188 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002190 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002192
Michael Jurka86a720e2012-05-09 11:23:23 -07002193 boolean startActivitySafely(View v, Intent intent, Object tag) {
2194 boolean success = false;
2195 try {
2196 success = startActivity(v, intent, tag);
2197 } catch (ActivityNotFoundException e) {
2198 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2199 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2200 }
2201 return success;
2202 }
2203
Romain Guy8e633c52010-03-04 12:51:36 -08002204 void startActivityForResultSafely(Intent intent, int requestCode) {
2205 try {
2206 startActivityForResult(intent, requestCode);
2207 } catch (ActivityNotFoundException e) {
2208 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2209 } catch (SecurityException e) {
2210 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2211 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2212 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2213 "or use the exported attribute for this activity.", e);
2214 }
2215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216
Adam Cohena9cf38f2011-05-02 15:36:58 -07002217 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002218 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002219 Folder openFolder = mWorkspace.getFolderForTag(info);
2220
2221 // If the folder info reports that the associated folder is open, then verify that
2222 // it is actually opened. There have been a few instances where this gets out of sync.
2223 if (info.opened && openFolder == null) {
2224 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002225 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002226 info.opened = false;
2227 }
2228
Adam Cohenfb91f302012-06-11 15:45:18 -07002229 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 // Close any open folder
2231 closeFolder();
2232 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002233 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 } else {
2235 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 int folderScreen;
2237 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002238 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 // .. and close it
2240 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002241 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 // Close any folder open on the current screen
2243 closeFolder();
2244 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002245 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 }
2247 }
2248 }
2249 }
2250
Adam Cohen268c4752012-06-06 17:47:33 -07002251 /**
2252 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2253 * in the DragLayer in the exact absolute location of the original FolderIcon.
2254 */
2255 private void copyFolderIconToImage(FolderIcon fi) {
2256 final int width = fi.getMeasuredWidth();
2257 final int height = fi.getMeasuredHeight();
2258
2259 // Lazy load ImageView, Bitmap and Canvas
2260 if (mFolderIconImageView == null) {
2261 mFolderIconImageView = new ImageView(this);
2262 }
2263 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2264 mFolderIconBitmap.getHeight() != height) {
2265 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2266 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2267 }
2268
2269 DragLayer.LayoutParams lp;
2270 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2271 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2272 } else {
2273 lp = new DragLayer.LayoutParams(width, height);
2274 }
2275
Adam Cohen307fe232012-08-16 17:55:58 -07002276 // The layout from which the folder is being opened may be scaled, adjust the starting
2277 // view size by this scale factor.
2278 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002279 lp.customPosition = true;
2280 lp.x = mRectForFolderAnimation.left;
2281 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002282 lp.width = (int) (scale * width);
2283 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002284
2285 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2286 fi.draw(mFolderIconCanvas);
2287 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002288 if (fi.getFolder() != null) {
2289 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2290 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002291 }
Adam Cohen268c4752012-06-06 17:47:33 -07002292 // Just in case this image view is still in the drag layer from a previous animation,
2293 // we remove it and re-add it.
2294 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2295 mDragLayer.removeView(mFolderIconImageView);
2296 }
2297 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002298 if (fi.getFolder() != null) {
2299 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002300 }
Adam Cohen268c4752012-06-06 17:47:33 -07002301 }
2302
Adam Cohen2801caf2011-05-13 20:57:39 -07002303 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002304 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002305 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2306 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2307 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2308
Adam Cohenc51934b2011-07-26 21:07:43 -07002309 FolderInfo info = (FolderInfo) fi.getTag();
2310 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2311 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002312 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2313 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002314 }
2315
Adam Cohen268c4752012-06-06 17:47:33 -07002316 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2317 copyFolderIconToImage(fi);
2318 fi.setVisibility(View.INVISIBLE);
2319
Michael Jurka2ecf9952012-06-18 12:52:28 -07002320 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002321 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002322 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2323 oa.start();
2324 }
2325
Adam Cohen268c4752012-06-06 17:47:33 -07002326 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002327 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002328 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2329 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2330 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2331
Adam Cohen268c4752012-06-06 17:47:33 -07002332 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002333
Adam Cohen268c4752012-06-06 17:47:33 -07002334 // We remove and re-draw the FolderIcon in-case it has changed
2335 mDragLayer.removeView(mFolderIconImageView);
2336 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002337 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002338 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002339 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002340 oa.addListener(new AnimatorListenerAdapter() {
2341 @Override
2342 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002343 if (cl != null) {
2344 cl.clearFolderLeaveBehind();
2345 // Remove the ImageView copy of the FolderIcon and make the original visible.
2346 mDragLayer.removeView(mFolderIconImageView);
2347 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002348 }
2349 }
2350 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002351 oa.start();
2352 }
2353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002355 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 * is animated relative to the specified View. If the View is null, no animation
2357 * is played.
2358 *
2359 * @param folderInfo The FolderInfo describing the folder to open.
2360 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002361 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002362 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002363 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364
Adam Cohena9cf38f2011-05-02 15:36:58 -07002365 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366
Adam Cohen4554ee12011-08-03 16:13:21 -07002367 // Just verify that the folder hasn't already been added to the DragLayer.
2368 // There was a one-off crash where the folder had a parent already.
2369 if (folder.getParent() == null) {
2370 mDragLayer.addView(folder);
2371 mDragController.addDropTarget((DropTarget) folder);
2372 } else {
2373 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2374 folder.getParent() + ").");
2375 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002376 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002377 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002378
2379 // Notify the accessibility manager that this folder "window" has appeared and occluded
2380 // the workspace items
2381 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2382 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002383 }
2384
2385 public void closeFolder() {
2386 Folder folder = mWorkspace.getOpenFolder();
2387 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002388 if (folder.isEditingName()) {
2389 folder.dismissEditingName();
2390 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002391 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002392
2393 // Dismiss the folder cling
2394 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002395 }
2396 }
2397
2398 void closeFolder(Folder folder) {
2399 folder.getInfo().opened = false;
2400
2401 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2402 if (parent != null) {
2403 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2404 shrinkAndFadeInFolderIcon(fi);
2405 }
2406 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002407
2408 // Notify the accessibility manager that this folder "window" has disappeard and no
2409 // longer occludeds the workspace items
2410 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 }
2412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002414 if (!isDraggingEnabled()) return false;
2415 if (isWorkspaceLocked()) return false;
2416 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417
2418 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002419 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 }
2421
Michael Jurka0280c3b2010-09-17 15:00:07 -07002422 resetAddInfo();
2423 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002425 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 return true;
2427 }
2428
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 // The hotseat touch handling does not go through Workspace, and we always allow long press
2430 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002431 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002432 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2433 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002434 if (itemUnderLongClick == null) {
2435 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002436 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2437 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002438 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002440 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002442 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 }
2444 }
2445 }
2446 return true;
2447 }
2448
Winson Chung3d503fb2011-07-13 17:25:49 -07002449 boolean isHotseatLayout(View layout) {
2450 return mHotseat != null && layout != null &&
2451 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2452 }
2453 Hotseat getHotseat() {
2454 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002455 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002456 SearchDropTargetBar getSearchBar() {
2457 return mSearchDropTargetBar;
2458 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002459
Winson Chung3d503fb2011-07-13 17:25:49 -07002460 /**
2461 * Returns the CellLayout of the specified container at the specified screen.
2462 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002463 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002464 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2465 if (mHotseat != null) {
2466 return mHotseat.getLayout();
2467 } else {
2468 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002469 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002470 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002471 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002472 }
2473 }
2474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 Workspace getWorkspace() {
2476 return mWorkspace;
2477 }
2478
Daniel Sandler843e8602010-06-07 14:59:01 -04002479 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002480 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002481 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002482 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002483 }
2484
Craig Mautner360310b2012-10-26 15:13:08 -07002485 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002486 public boolean isAllAppsButtonRank(int rank) {
2487 return mHotseat.isAllAppsButtonRank(rank);
2488 }
2489
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002490 /**
2491 * Helper method for the cameraZoomIn/cameraZoomOut animations
2492 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002493 * @param scaleFactor The scale factor used for the zoom
2494 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002495 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002496 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002497 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002498 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002499
Winson Chung18f41f82012-05-09 13:28:10 -07002500 void disableWallpaperIfInAllApps() {
2501 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002502 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002503 if (mAppsCustomizeTabHost != null &&
2504 !mAppsCustomizeTabHost.isTransitioning()) {
2505 updateWallpaperVisibility(false);
2506 }
2507 }
2508 }
2509
Craig Mautner360310b2012-10-26 15:13:08 -07002510 private void setWorkspaceBackground(boolean workspace) {
2511 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002512 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002513 }
2514
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002515 void updateWallpaperVisibility(boolean visible) {
2516 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2517 int curflags = getWindow().getAttributes().flags
2518 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2519 if (wpflags != curflags) {
2520 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2521 }
Craig Mautner360310b2012-10-26 15:13:08 -07002522 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002523 }
2524
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002525 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2526 if (v instanceof LauncherTransitionable) {
2527 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2528 }
2529 }
2530
Michael Jurkabed61d22012-02-14 22:51:29 -08002531 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2532 if (v instanceof LauncherTransitionable) {
2533 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2534 }
Winson Chung70442722012-02-10 15:43:22 -08002535
2536 // Update the workspace transition step as well
2537 dispatchOnLauncherTransitionStep(v, 0f);
2538 }
2539
2540 private void dispatchOnLauncherTransitionStep(View v, float t) {
2541 if (v instanceof LauncherTransitionable) {
2542 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2543 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002544 }
2545
2546 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2547 if (v instanceof LauncherTransitionable) {
2548 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2549 }
Winson Chung70442722012-02-10 15:43:22 -08002550
2551 // Update the workspace transition step as well
2552 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002553 }
2554
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002555 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002556 * Things to test when changing the following seven functions.
2557 * - Home from workspace
2558 * - from center screen
2559 * - from other screens
2560 * - Home from all apps
2561 * - from center screen
2562 * - from other screens
2563 * - Back from all apps
2564 * - from center screen
2565 * - from other screens
2566 * - Launch app from workspace and quit
2567 * - with back
2568 * - with home
2569 * - Launch app from all apps and quit
2570 * - with back
2571 * - with home
2572 * - Go to a screen that's not the default, then all
2573 * apps, and launch and app, and go back
2574 * - with back
2575 * -with home
2576 * - On workspace, long press power and go back
2577 * - with back
2578 * - with home
2579 * - On all apps, long press power and go back
2580 * - with back
2581 * - with home
2582 * - On workspace, power off
2583 * - On all apps, power off
2584 * - Launch an app and turn off the screen while in that app
2585 * - Go back with home key
2586 * - Go back with back key TODO: make this not go to workspace
2587 * - From all apps
2588 * - From workspace
2589 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2590 * - From all apps
2591 * - From the center workspace
2592 * - From another workspace
2593 */
2594
2595 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002596 * Zoom the camera out from the workspace to reveal 'toView'.
2597 * Assumes that the view to show is anchored at either the very top or very bottom
2598 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002599 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002600 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002601 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002602 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002603 mStateAnimation.cancel();
2604 mStateAnimation = null;
2605 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002606 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002607
Winson Chungf0ea4d32011-06-06 14:27:16 -07002608 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2609 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2610 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002611 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002612 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002613 final int startDelay =
2614 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002615
Michael Jurkab3e22d92011-10-31 15:58:33 -07002616 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617
Michael Jurkad74c9842011-07-10 12:44:21 -07002618 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002619 Animator workspaceAnim =
2620 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002621
2622 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002623 toView.setScaleX(scale);
2624 toView.setScaleY(scale);
2625 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2626 scaleAnim.
2627 scaleX(1f).scaleY(1f).
2628 setDuration(duration).
2629 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002630
Winson Chungf0ea4d32011-06-06 14:27:16 -07002631 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002632 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002633 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002634 .ofFloat(toView, "alpha", 0f, 1f)
2635 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002636 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002637 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2638 @Override
2639 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002640 if (animation == null) {
2641 throw new RuntimeException("animation is null");
2642 }
Winson Chung70442722012-02-10 15:43:22 -08002643 float t = (Float) animation.getAnimatedValue();
2644 dispatchOnLauncherTransitionStep(fromView, t);
2645 dispatchOnLauncherTransitionStep(toView, t);
2646 }
2647 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002648
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002649 // toView should appear right at the end of the workspace shrink
2650 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002651 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002652 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002653 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002654
2655 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002656 boolean animationCancelled = false;
2657
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002658 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002659 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002660 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002661 // Prepare the position
2662 toView.setTranslationX(0.0f);
2663 toView.setTranslationY(0.0f);
2664 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002665 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002666 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002667 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002668 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002669 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2670 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002671
Daniel Sandlere4f98912013-06-25 15:13:26 -04002672 if (mWorkspace != null
2673 && !springLoaded
2674 && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002675 // Hide the workspace scrollbar
2676 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002677 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002678 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002679 if (!animationCancelled) {
2680 updateWallpaperVisibility(false);
2681 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002682
2683 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002684 if (mSearchDropTargetBar != null) {
2685 mSearchDropTargetBar.hideSearchBar(false);
2686 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002687 }
2688
Adam Cohencff6af82011-09-13 14:51:53 -07002689 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002690 public void onAnimationCancel(Animator animation) {
2691 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002692 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002693 });
2694
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002695 if (workspaceAnim != null) {
2696 mStateAnimation.play(workspaceAnim);
2697 }
Michael Jurka1899a362011-11-03 13:50:45 -07002698
2699 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002700
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002701 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2702 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002703
2704 // If any of the objects being animated haven't been measured/laid out
2705 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002706 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002707 (mWorkspace.getMeasuredWidth() == 0) ||
2708 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002709 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002710 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002711
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002712 final AnimatorSet stateAnimation = mStateAnimation;
2713 final Runnable startAnimRunnable = new Runnable() {
2714 public void run() {
2715 // Check that mStateAnimation hasn't changed while
2716 // we waited for a layout/draw pass
2717 if (mStateAnimation != stateAnimation)
2718 return;
2719 setPivotsForZoom(toView, scale);
2720 dispatchOnLauncherTransitionStart(fromView, animated, false);
2721 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002722 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002723 }
2724 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002725 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002726 final ViewTreeObserver observer = toView.getViewTreeObserver();
2727 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2728 public void onGlobalLayout() {
2729 startAnimRunnable.run();
2730 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2731 }
2732 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002733 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002734 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002735 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002736 } else {
2737 toView.setTranslationX(0.0f);
2738 toView.setTranslationY(0.0f);
2739 toView.setScaleX(1.0f);
2740 toView.setScaleY(1.0f);
2741 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002742 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002743
Daniel Sandlere4f98912013-06-25 15:13:26 -04002744 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002745 // Hide the workspace scrollbar
2746 mWorkspace.hideScrollingIndicator(true);
2747 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002748
2749 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002750 if (mSearchDropTargetBar != null) {
2751 mSearchDropTargetBar.hideSearchBar(false);
2752 }
Michael Jurkaabded662011-03-04 12:06:57 -08002753 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002754 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002755 dispatchOnLauncherTransitionStart(fromView, animated, false);
2756 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002757 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002758 dispatchOnLauncherTransitionStart(toView, animated, false);
2759 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002760 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002761 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002762 }
2763
2764 /**
2765 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002766 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002767 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002768 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002769 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2770 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002771
Michael Jurkab3e22d92011-10-31 15:58:33 -07002772 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002773 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002774 mStateAnimation.cancel();
2775 mStateAnimation = null;
2776 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002777 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002778
Winson Chungf0ea4d32011-06-06 14:27:16 -07002779 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002780 final int fadeOutDuration =
2781 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002782 final float scaleFactor = (float)
2783 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2784 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002785 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002786 Animator workspaceAnim = null;
2787
2788 if (toState == State.WORKSPACE) {
2789 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2790 workspaceAnim = mWorkspace.getChangeStateAnimation(
2791 Workspace.State.NORMAL, animated, stagger);
2792 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2793 workspaceAnim = mWorkspace.getChangeStateAnimation(
2794 Workspace.State.SPRING_LOADED, animated);
2795 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002796
Michael Jurkab3e22d92011-10-31 15:58:33 -07002797 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002798 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002799 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002800 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002801 final LauncherViewPropertyAnimator scaleAnim =
2802 new LauncherViewPropertyAnimator(fromView);
2803 scaleAnim.
2804 scaleX(scaleFactor).scaleY(scaleFactor).
2805 setDuration(duration).
2806 setInterpolator(new Workspace.ZoomInInterpolator());
2807
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002808 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002809 .ofFloat(fromView, "alpha", 1f, 0f)
2810 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002811 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002812 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2813 @Override
2814 public void onAnimationUpdate(ValueAnimator animation) {
2815 float t = 1f - (Float) animation.getAnimatedValue();
2816 dispatchOnLauncherTransitionStep(fromView, t);
2817 dispatchOnLauncherTransitionStep(toView, t);
2818 }
2819 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002820
Michael Jurka2ecf9952012-06-18 12:52:28 -07002821 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002822
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002823 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2824 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002825 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002826
2827 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002828 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002829 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002830 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002831 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002832 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2833 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002834 if (mWorkspace != null) {
2835 mWorkspace.hideScrollingIndicator(false);
2836 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002837 if (onCompleteRunnable != null) {
2838 onCompleteRunnable.run();
2839 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002840 mAppsCustomizeContent.updateCurrentPageScroll();
2841 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002842 }
2843 });
2844
Winson Chungf0ea4d32011-06-06 14:27:16 -07002845 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002846 if (workspaceAnim != null) {
2847 mStateAnimation.play(workspaceAnim);
2848 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002849 dispatchOnLauncherTransitionStart(fromView, animated, true);
2850 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002851 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002852 } else {
2853 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002854 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002855 dispatchOnLauncherTransitionStart(fromView, animated, true);
2856 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002857 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002858 dispatchOnLauncherTransitionStart(toView, animated, true);
2859 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002860 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002861 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002862 }
2863
Michael Jurkae326f182011-11-21 14:05:46 -08002864 @Override
2865 public void onTrimMemory(int level) {
2866 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002867 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002868 mAppsCustomizeTabHost.onTrimMemory();
2869 }
2870 }
2871
Winson Chung18f41f82012-05-09 13:28:10 -07002872 @Override
2873 public void onWindowFocusChanged(boolean hasFocus) {
2874 if (!hasFocus) {
2875 // When another window occludes launcher (like the notification shade, or recents),
2876 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2877 updateWallpaperVisibility(true);
2878 } else {
2879 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002880 mWorkspace.postDelayed(new Runnable() {
2881 @Override
2882 public void run() {
2883 disableWallpaperIfInAllApps();
2884 }
2885 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002886 }
2887 }
2888
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002889 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002890 showWorkspace(animated, null);
2891 }
2892
2893 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002894 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002895 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002896 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002897 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002898
Winson Chungc7d2b602012-05-16 17:02:20 -07002899 // Show the search bar (only animate if we were showing the drop target bar in spring
2900 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002901 if (mSearchDropTargetBar != null) {
2902 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2903 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002904
Michael Jurkab737ee62011-11-15 15:57:22 -08002905 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002906 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002907
2908 // Set focus to the AppsCustomize button
2909 if (mAllAppsButton != null) {
2910 mAllAppsButton.requestFocus();
2911 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002912 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002913
Michael Jurkab737ee62011-11-15 15:57:22 -08002914 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002915
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002916 // Change the state *after* we've called all the transition code
2917 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002918
Winson Chung5fb63472011-02-02 17:03:37 -08002919 // Resume the auto-advance of widgets
2920 mUserPresent = true;
2921 updateRunning();
2922
alanv1d4fde62012-10-17 13:15:47 -07002923 // Send an accessibility event to announce the context change
2924 getWindow().getDecorView()
2925 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002926
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002927 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002928 }
2929
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002930 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002931 }
2932
Michael Jurkab3e22d92011-10-31 15:58:33 -07002933 void showAllApps(boolean animated) {
2934 if (mState != State.WORKSPACE) return;
2935
2936 showAppsCustomizeHelper(animated, false);
2937 mAppsCustomizeTabHost.requestFocus();
2938
Michael Jurkab3e22d92011-10-31 15:58:33 -07002939 // Change the state *after* we've called all the transition code
2940 mState = State.APPS_CUSTOMIZE;
2941
2942 // Pause the auto-advance of widgets until we are out of AllApps
2943 mUserPresent = false;
2944 updateRunning();
2945 closeFolder();
2946
2947 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002948 getWindow().getDecorView()
2949 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002950 }
2951
Adam Cohen7777d962011-08-18 18:58:38 -07002952 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002953 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002954 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002955 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002956 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002957 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002958 }
Adam Cohen7777d962011-08-18 18:58:38 -07002959
Adam Cohened66b2b2012-01-23 17:28:51 -08002960 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2961 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002962 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2963
Winson Chunge7a03942011-08-05 15:05:12 -07002964 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002965 @Override
2966 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002967 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002968 // Before we show workspace, hide all apps again because
2969 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2970 // clean up our state transition functions
2971 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002972 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002973 } else {
2974 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002975 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002976 }
2977 }, (extendedDelay ?
2978 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2979 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2980 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002981
Michael Jurkad3ef3062010-11-23 16:23:58 -08002982 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002983 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002984 final boolean animated = true;
2985 final boolean springLoaded = true;
2986 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002987 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002988 }
2989 // Otherwise, we are not in spring loaded mode, so don't do anything.
2990 }
2991
Michael Jurkab737ee62011-11-15 15:57:22 -08002992 void hideDockDivider() {
2993 if (mQsbDivider != null && mDockDivider != null) {
2994 mQsbDivider.setVisibility(View.INVISIBLE);
2995 mDockDivider.setVisibility(View.INVISIBLE);
2996 }
2997 }
2998
2999 void showDockDivider(boolean animated) {
3000 if (mQsbDivider != null && mDockDivider != null) {
3001 mQsbDivider.setVisibility(View.VISIBLE);
3002 mDockDivider.setVisibility(View.VISIBLE);
3003 if (mDividerAnimator != null) {
3004 mDividerAnimator.cancel();
3005 mQsbDivider.setAlpha(1f);
3006 mDockDivider.setAlpha(1f);
3007 mDividerAnimator = null;
3008 }
3009 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003010 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3011 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3012 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003013 int duration = 0;
3014 if (mSearchDropTargetBar != null) {
3015 duration = mSearchDropTargetBar.getTransitionInDuration();
3016 }
3017 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003018 mDividerAnimator.start();
3019 }
3020 }
3021 }
3022
Michael Jurkab3e22d92011-10-31 15:58:33 -07003023 void lockAllApps() {
3024 // TODO
3025 }
3026
3027 void unlockAllApps() {
3028 // TODO
3029 }
3030
Winson Chungf0ea4d32011-06-06 14:27:16 -07003031 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003032 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003033 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003034 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003035 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003036 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003037 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003038 int duration = 0;
3039 if (mSearchDropTargetBar != null) {
3040 duration = mSearchDropTargetBar.getTransitionInDuration();
3041 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003042 mHotseat.animate().alpha(1f).setDuration(duration);
3043 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003044 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003045 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003046 }
3047 }
3048 }
3049
3050 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003051 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003052 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003053 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003054 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003055 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003056 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003057 int duration = 0;
3058 if (mSearchDropTargetBar != null) {
3059 duration = mSearchDropTargetBar.getTransitionOutDuration();
3060 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003061 mHotseat.animate().alpha(0f).setDuration(duration);
3062 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003063 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003064 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003065 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003066 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003067 }
3068
Patrick Dubroy5f445422011-02-18 14:35:21 -08003069 /**
3070 * Add an item from all apps or customize onto the given workspace screen.
3071 * If layout is null, add to the current screen.
3072 */
3073 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003074 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003075 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003076 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003077 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003078
Winson Chungdff8ebb2011-09-08 17:25:31 -07003079 /** Maps the current orientation to an index for referencing orientation correct global icons */
3080 private int getCurrentOrientationIndexForGlobalIcons() {
3081 // default - 0, landscape - 1
3082 switch (getResources().getConfiguration().orientation) {
3083 case Configuration.ORIENTATION_LANDSCAPE:
3084 return 1;
3085 default:
3086 return 0;
3087 }
3088 }
3089
Michael Jurkaae65ee32012-04-30 11:45:54 -07003090 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003091 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003092 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003093 // Look for the toolbar icon specified in the activity meta-data
3094 Bundle metaData = packageManager.getActivityInfo(
3095 activityName, PackageManager.GET_META_DATA).metaData;
3096 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003097 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003098 if (iconResId != 0) {
3099 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003100 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003101 }
3102 }
3103 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003104 // This can happen if the activity defines an invalid drawable
3105 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3106 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003107 } catch (Resources.NotFoundException nfe) {
3108 // This can happen if the activity defines an invalid drawable
3109 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3110 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003111 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003112 return null;
3113 }
3114
3115 // if successful in getting icon, return it; otherwise, set button to use default drawable
3116 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003117 int buttonId, ComponentName activityName, int fallbackDrawableId,
3118 String toolbarResourceName) {
3119 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003120 Resources r = getResources();
3121 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3122 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003123
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003124 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003125 // If we were unable to find the icon via the meta-data, use a generic one
3126 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003127 toolbarIcon = r.getDrawable(fallbackDrawableId);
3128 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003129 if (button != null) {
3130 button.setCompoundDrawables(toolbarIcon, null, null, null);
3131 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003132 return null;
3133 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003134 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003135 if (button != null) {
3136 button.setCompoundDrawables(toolbarIcon, null, null, null);
3137 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003138 return toolbarIcon.getConstantState();
3139 }
3140 }
3141
3142 // if successful in getting icon, return it; otherwise, set button to use default drawable
3143 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003144 int buttonId, ComponentName activityName, int fallbackDrawableId,
3145 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003146 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003147 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003148
Michael Jurka19e0fc52011-07-22 18:00:21 -07003149 if (button != null) {
3150 // If we were unable to find the icon via the meta-data, use a
3151 // generic one
3152 if (toolbarIcon == null) {
3153 button.setImageResource(fallbackDrawableId);
3154 } else {
3155 button.setImageDrawable(toolbarIcon);
3156 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003157 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003158
3159 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3160
Michael Jurka0423dcf2010-10-05 14:56:18 -07003161 }
3162
Winson Chung1b884092012-06-08 17:13:02 -07003163 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003164 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003165 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003166 }
3167
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003168 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003169 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003170 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003171 }
3172
Winson Chungbb185bd2011-11-21 12:31:42 -08003173 private void invalidatePressedFocusedStates(View container, View button) {
3174 if (container instanceof HolographicLinearLayout) {
3175 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3176 layout.invalidatePressedFocusedStates();
3177 } else if (button instanceof HolographicImageView) {
3178 HolographicImageView view = (HolographicImageView) button;
3179 view.invalidatePressedFocusedStates();
3180 }
3181 }
3182
Winson Chungc51db6a2011-10-05 11:44:49 -07003183 private boolean updateGlobalSearchIcon() {
3184 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003185 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003186 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003187 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003188
Winson Chung1cad91e2011-05-25 17:41:01 -07003189 final SearchManager searchManager =
3190 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3191 ComponentName activityName = searchManager.getGlobalSearchActivity();
3192 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003193 int coi = getCurrentOrientationIndexForGlobalIcons();
3194 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003195 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3196 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3197 if (sGlobalSearchIcon[coi] == null) {
3198 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3199 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3200 TOOLBAR_ICON_METADATA_NAME);
3201 }
3202
Winson Chungc51db6a2011-10-05 11:44:49 -07003203 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3204 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003205 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003206 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003207 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003208 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003209 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3210 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3211 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003212 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003213 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003214 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003215 }
3216 }
3217
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003218 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003219 final View searchButtonContainer = findViewById(R.id.search_button_container);
3220 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003221 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003222 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003223 }
3224
Winson Chungc51db6a2011-10-05 11:44:49 -07003225 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003226 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003227 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003228
Winson Chungc51db6a2011-10-05 11:44:49 -07003229 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003230 final SearchManager searchManager =
3231 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3232 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3233
3234 ComponentName activityName = null;
3235 if (globalSearchActivity != null) {
3236 // Check if the global search activity handles voice search
3237 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3238 intent.setPackage(globalSearchActivity.getPackageName());
3239 activityName = intent.resolveActivity(getPackageManager());
3240 }
3241
3242 if (activityName == null) {
3243 // Fallback: check if an activity other than the global search activity
3244 // resolves this
3245 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3246 activityName = intent.resolveActivity(getPackageManager());
3247 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003248 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003249 int coi = getCurrentOrientationIndexForGlobalIcons();
3250 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003251 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3252 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3253 if (sVoiceSearchIcon[coi] == null) {
3254 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3255 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3256 TOOLBAR_ICON_METADATA_NAME);
3257 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003258 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003259 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003260 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003261 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003262 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003263 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003264 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003265 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003266 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003267 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003268 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003269 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003270
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003271 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003272 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3273 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003274 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003275 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003276 }
3277
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003278 public void setVoiceButtonProxyVisible(boolean visible) {
3279 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3280 if (voiceButtonProxy != null) {
3281 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3282 }
3283 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003284 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003285 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003286 */
3287 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003288 final View marketButton = findViewById(R.id.market_button);
3289 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3290 // Find the app market activity by resolving an intent.
3291 // (If multiple app markets are installed, it will return the ResolverActivity.)
3292 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003293 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003294 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003295 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003296 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003297 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3298 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003299 marketButton.setVisibility(View.VISIBLE);
3300 } else {
3301 // We should hide and disable the view so that we don't try and restore the visibility
3302 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3303 marketButton.setVisibility(View.GONE);
3304 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003305 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003306 }
3307
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003308 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003309 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3310 Resources r = getResources();
3311 Drawable marketIconDrawable = d.newDrawable(r);
3312 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3313 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3314 marketIconDrawable.setBounds(0, 0, w, h);
3315
3316 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003317 }
3318
Michael Jurkad7c28052012-04-27 15:43:36 -07003319 @Override
3320 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003321 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003322 final List<CharSequence> text = event.getText();
3323 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003324 // Populate event with a fake title based on the current state.
3325 if (mState == State.APPS_CUSTOMIZE) {
3326 text.add(getString(R.string.all_apps_button_label));
3327 } else {
3328 text.add(getString(R.string.all_apps_home_button_label));
3329 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003330 return result;
3331 }
3332
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003333 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003334 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003335 */
3336 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3337 @Override
3338 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003339 closeSystemDialogs();
3340 }
3341 }
3342
3343 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003344 * Receives notifications whenever the appwidgets are reset.
3345 */
3346 private class AppWidgetResetObserver extends ContentObserver {
3347 public AppWidgetResetObserver() {
3348 super(new Handler());
3349 }
3350
3351 @Override
3352 public void onChange(boolean selfChange) {
3353 onAppWidgetReset();
3354 }
3355 }
3356
3357 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003358 * If the activity is currently paused, signal that we need to run the passed Runnable
3359 * in onResume.
3360 *
3361 * This needs to be called from incoming places where resources might have been loaded
3362 * while we are paused. That is becaues the Configuration might be wrong
3363 * when we're not running, and if it comes back to what it was when we
3364 * were paused, we are not restarted.
3365 *
3366 * Implementation of the method from LauncherModel.Callbacks.
3367 *
3368 * @return true if we are currently paused. The caller might be able to
3369 * skip some work in that case since we will come back again.
3370 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003371 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003372 if (mPaused) {
3373 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003374 if (deletePreviousRunnables) {
3375 while (mOnResumeCallbacks.remove(run)) {
3376 }
3377 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003378 mOnResumeCallbacks.add(run);
3379 return true;
3380 } else {
3381 return false;
3382 }
3383 }
3384
Michael Jurkac402cd92013-05-20 15:49:32 +02003385 private boolean waitUntilResume(Runnable run) {
3386 return waitUntilResume(run, false);
3387 }
3388
Michael Jurka7607c2f2013-04-03 14:33:19 -07003389 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003390 * If the activity is currently paused, signal that we need to re-run the loader
3391 * in onResume.
3392 *
3393 * This needs to be called from incoming places where resources might have been loaded
3394 * while we are paused. That is becaues the Configuration might be wrong
3395 * when we're not running, and if it comes back to what it was when we
3396 * were paused, we are not restarted.
3397 *
3398 * Implementation of the method from LauncherModel.Callbacks.
3399 *
3400 * @return true if we are currently paused. The caller might be able to
3401 * skip some work in that case since we will come back again.
3402 */
3403 public boolean setLoadOnResume() {
3404 if (mPaused) {
3405 Log.i(TAG, "setLoadOnResume");
3406 mOnResumeNeedsLoad = true;
3407 return true;
3408 } else {
3409 return false;
3410 }
3411 }
3412
3413 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003415 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003417 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003418 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003419 } else {
3420 return SCREEN_COUNT / 2;
3421 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003423
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424 /**
3425 * Refreshes the shortcuts shown on the workspace.
3426 *
3427 * Implementation of the method from LauncherModel.Callbacks.
3428 */
3429 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003430 // If we're starting binding all over again, clear any bind calls we'd postponed in
3431 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3432 // from scratch again
3433 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003434
Michael Jurka7607c2f2013-04-03 14:33:19 -07003435 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003436 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003437 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003438 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003439 int count = workspace.getChildCount();
3440 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003441 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003442 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3443 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003445 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003446 if (mHotseat != null) {
3447 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003448 }
3449 }
3450
Adam Cohendcd297f2013-06-18 13:13:40 -07003451 @Override
3452 public void bindScreens(ArrayList<Long> orderedScreenIds) {
3453 int count = orderedScreenIds.size();
3454 for (int i = 0; i < count; i++) {
3455 mWorkspace.insertNewWorkspaceScreenOnBind(orderedScreenIds.get(i));
3456 }
3457 mWorkspace.addExtraEmptyScreen();
3458 }
3459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003460 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003461 * Bind the items start-end from the list.
3462 *
3463 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003464 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003465 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3466 if (waitUntilResume(new Runnable() {
3467 public void run() {
3468 bindItems(shortcuts, start, end);
3469 }
3470 })) {
3471 return;
3472 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003473
Winson Chungf0c6ae02012-03-21 16:10:31 -07003474 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3475 Set<String> newApps = new HashSet<String>();
3476 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3477
3478 Workspace workspace = mWorkspace;
3479 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003481
3482 // Short circuit if we are loading dock items for a configuration which has no dock
3483 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3484 mHotseat == null) {
3485 continue;
3486 }
3487
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 switch (item.itemType) {
3489 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3490 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003491 ShortcutInfo info = (ShortcutInfo) item;
3492 String uri = info.intent.toUri(0).toString();
3493 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003494
3495 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3496 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003497 boolean animateIconUp = false;
3498 synchronized (newApps) {
3499 if (newApps.contains(uri)) {
3500 animateIconUp = newApps.remove(uri);
3501 }
3502 }
3503 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003504 // Prepare the view to be animated up
3505 shortcut.setAlpha(0f);
3506 shortcut.setScaleX(0f);
3507 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003508 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003509 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3510 mNewShortcutAnimateViews.add(shortcut);
3511 }
3512 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003513 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003514 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003515 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003516 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003517 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003518 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3519 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003520 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003522 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003523
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003525 }
3526
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 /**
3528 * Implementation of the method from LauncherModel.Callbacks.
3529 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003530 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3531 if (waitUntilResume(new Runnable() {
3532 public void run() {
3533 bindFolders(folders);
3534 }
3535 })) {
3536 return;
3537 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003538 sFolders.clear();
3539 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003540 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003541
3542 /**
3543 * Add the views for a widget to the workspace.
3544 *
3545 * Implementation of the method from LauncherModel.Callbacks.
3546 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003547 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3548 if (waitUntilResume(new Runnable() {
3549 public void run() {
3550 bindAppWidget(item);
3551 }
3552 })) {
3553 return;
3554 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003555
Daniel Sandler843e8602010-06-07 14:59:01 -04003556 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3557 if (DEBUG_WIDGETS) {
3558 Log.d(TAG, "bindAppWidget: " + item);
3559 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 final Workspace workspace = mWorkspace;
3561
3562 final int appWidgetId = item.appWidgetId;
3563 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003564 if (DEBUG_WIDGETS) {
3565 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3566 }
3567
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3569
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003571 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572
Adam Cohendcd297f2013-06-18 13:13:40 -07003573 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003575 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3576
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 workspace.requestLayout();
3578
Daniel Sandler843e8602010-06-07 14:59:01 -04003579 if (DEBUG_WIDGETS) {
3580 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3581 + (SystemClock.uptimeMillis()-start) + "ms");
3582 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583 }
3584
Adam Cohen1462de32012-07-24 22:34:36 -07003585 public void onPageBoundSynchronously(int page) {
3586 mSynchronouslyBoundPages.add(page);
3587 }
3588
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 /**
3590 * Callback saying that there aren't any more items to bind.
3591 *
3592 * Implementation of the method from LauncherModel.Callbacks.
3593 */
Adam Cohene25af792013-06-06 23:08:25 -07003594 public void finishBindingItems(final boolean upgradePath) {
Winson Chungc9168342013-06-26 14:54:55 -07003595
Michael Jurka7607c2f2013-04-03 14:33:19 -07003596 if (waitUntilResume(new Runnable() {
3597 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003598 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003599 }
3600 })) {
3601 return;
3602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 if (mSavedState != null) {
3604 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003605 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 mSavedState = null;
3608 }
3609
Adam Cohen1462de32012-07-24 22:34:36 -07003610 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003612 // If we received the result of any pending adds while the loader was running (e.g. the
3613 // widget configuration forced an orientation change), process them now.
3614 for (int i = 0; i < sPendingAddList.size(); i++) {
3615 completeAdd(sPendingAddList.get(i));
3616 }
3617 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618
Winson Chungc51db6a2011-10-05 11:44:49 -07003619 // Update the market app icon as necessary (the other icons will be managed in response to
3620 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003621 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003622
Winson Chungf0c6ae02012-03-21 16:10:31 -07003623 // Animate up any icons as necessary
3624 if (mVisible || mWorkspaceLoading) {
3625 Runnable newAppsRunnable = new Runnable() {
3626 @Override
3627 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003628 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003629 }
3630 };
Winson Chunga2413752012-04-03 14:22:34 -07003631
Adam Cohendcd297f2013-06-18 13:13:40 -07003632 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3633 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003634 if (canRunNewAppsAnimation()) {
3635 // If the user has not interacted recently, then either snap to the new page to show
3636 // the new-apps animation or just run them if they are to appear on the current page
3637 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003638 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003639 } else {
3640 runNewAppsAnimation(false);
3641 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003642 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003643 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003644 // are on another page (or just normally if they are added to the current page)
3645 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003646 }
3647 }
3648
3649 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003650 if (upgradePath) {
3651 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003652 mWorkspace.stripDuplicateApps();
3653 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003654 }
Adam Cohen99894d92013-06-14 11:22:59 -07003655
Adam Cohen66a01fd2013-06-11 12:48:00 -07003656 mWorkspace.post(new Runnable() {
3657 @Override
3658 public void run() {
3659 onFinishBindingItems();
3660 }
3661 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003662 }
3663
Winson Chunga2413752012-04-03 14:22:34 -07003664 private boolean canRunNewAppsAnimation() {
3665 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3666 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3667 }
3668
Winson Chungc9168342013-06-26 14:54:55 -07003669 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3670 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3671 PropertyValuesHolder.ofFloat("alpha", 1f),
3672 PropertyValuesHolder.ofFloat("scaleX", 1f),
3673 PropertyValuesHolder.ofFloat("scaleY", 1f));
3674 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3675 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3676 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3677 return bounceAnim;
3678 }
3679
Winson Chungf0c6ae02012-03-21 16:10:31 -07003680 /**
3681 * Runs a new animation that scales up icons that were added while Launcher was in the
3682 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003683 *
3684 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003685 */
Winson Chunga2413752012-04-03 14:22:34 -07003686 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003687 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003688 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003689
3690 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003691 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3692 @Override
3693 public int compare(View a, View b) {
3694 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3695 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3696 int cellCountX = LauncherModel.getCellCountX();
3697 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3698 }
3699 });
Winson Chunga2413752012-04-03 14:22:34 -07003700
3701 // Animate each of the views in place (or show them immediately if requested)
3702 if (immediate) {
3703 for (View v : mNewShortcutAnimateViews) {
3704 v.setAlpha(1f);
3705 v.setScaleX(1f);
3706 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003707 }
Winson Chunga2413752012-04-03 14:22:34 -07003708 } else {
3709 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3710 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003711 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003712 }
3713 anim.playTogether(bounceAnims);
3714 anim.addListener(new AnimatorListenerAdapter() {
3715 @Override
3716 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003717 if (mWorkspace != null) {
3718 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3719 }
Winson Chunga2413752012-04-03 14:22:34 -07003720 }
3721 });
3722 anim.start();
3723 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003724
3725 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003726 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003727 mNewShortcutAnimateViews.clear();
3728 new Thread("clearNewAppsThread") {
3729 public void run() {
3730 mSharedPrefs.edit()
3731 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3732 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3733 .commit();
3734 }
3735 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003736 }
3737
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003738 @Override
3739 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003740 boolean searchVisible = updateGlobalSearchIcon();
3741 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003742 if (mSearchDropTargetBar != null) {
3743 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3744 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003745 }
3746
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003747 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003748 * Add the icons for all apps.
3749 *
3750 * Implementation of the method from LauncherModel.Callbacks.
3751 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003752 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003753 Runnable setAllAppsRunnable = new Runnable() {
3754 public void run() {
3755 if (mAppsCustomizeContent != null) {
3756 mAppsCustomizeContent.setApps(apps);
Adam Cohene25af792013-06-06 23:08:25 -07003757
3758 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3759 getHotseat().addAllAppsFolder(mIconCache, apps,
3760 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3761 mIntentsOnWorkspaceFromUpgradePath = null;
3762 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07003763 }
3764 }
3765 };
3766
Michael Jurkac57b7a82011-08-09 22:02:20 -07003767 // Remove the progress bar entirely; we could also make it GONE
3768 // but better to remove it since we know it's not going to be used
3769 View progressBar = mAppsCustomizeTabHost.
3770 findViewById(R.id.apps_customize_progress_bar);
3771 if (progressBar != null) {
3772 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003773
3774 // We just post the call to setApps so the user sees the progress bar
3775 // disappear-- otherwise, it just looks like the progress bar froze
3776 // which doesn't look great
3777 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3778 } else {
3779 // If we did not initialize the spinner in onCreate, then we can directly set the
3780 // list of applications without waiting for any progress bars views to be hidden.
3781 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003782 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003783 }
3784
3785 /**
3786 * A package was installed.
3787 *
3788 * Implementation of the method from LauncherModel.Callbacks.
3789 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003790 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3791 if (waitUntilResume(new Runnable() {
3792 public void run() {
3793 bindAppsAdded(apps);
3794 }
3795 })) {
3796 return;
3797 }
3798
Winson Chungc9168342013-06-26 14:54:55 -07003799 final Launcher launcher = this;
3800 final Context context = this;
3801 final ArrayList<ApplicationInfo> appsCopy = new ArrayList<ApplicationInfo>();
3802 appsCopy.addAll(apps);
3803
3804 // Process newly added apps
3805 mWorkspace.post(new Runnable() {
3806 @Override
3807 public void run() {
3808 // Process newly added apps
3809 LauncherModel model = getModel();
3810 Iterator<ApplicationInfo> iter = appsCopy.iterator();
3811 ArrayList<View> animatedShortcuts = new ArrayList<View>();
3812
3813 while (iter.hasNext()) {
3814 ApplicationInfo a = iter.next();
3815 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
3816 a.title.toString(), a.intent);
3817 if (coords == null) {
3818 // If we can't find a valid position, then just add a new screen.
3819 // This takes time so we need to re-queue the add until the new
3820 // page is added.
Daniel Sandlere4f98912013-06-25 15:13:26 -04003821 long screenId = LauncherAppState.getInstance().getLauncherProvider()
3822 .generateNewScreenId();
Winson Chungc9168342013-06-26 14:54:55 -07003823 mWorkspace.insertNewWorkspaceScreen(screenId, false);
3824 model.updateWorkspaceScreenOrder(launcher, mWorkspace.getScreenOrder(),
3825 new Runnable() {
3826 @Override
3827 public void run() {
3828 bindAppsAdded(appsCopy);
3829 }
3830 });
3831 return;
3832 } else {
3833 final ShortcutInfo shortcutInfo = a.makeShortcut();
3834 final View shortcut = createShortcut(shortcutInfo);
3835 // Add the view to the screen
3836 mWorkspace.addInScreenFromBind(shortcut,
3837 LauncherSettings.Favorites.CONTAINER_DESKTOP,
3838 coords.first, coords.second[0], coords.second[1], 1, 1);
3839 // Add the shortcut to the db
3840 model.addItemToDatabase(context, shortcutInfo,
3841 LauncherSettings.Favorites.CONTAINER_DESKTOP,
3842 coords.first, coords.second[0], coords.second[1], false);
3843 // Animate the shortcut
3844 animatedShortcuts.add(shortcut);
3845 }
3846 iter.remove();
3847 }
3848
3849 // Animate all the applications up
3850 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3851 Collection<Animator> bounceAnims = new ArrayList<Animator>();
3852 for (int i = 0; i < animatedShortcuts.size(); ++i) {
3853 View shortcut = animatedShortcuts.get(i);
3854 shortcut.setAlpha(0f);
3855 shortcut.setScaleX(0f);
3856 shortcut.setScaleY(0f);
3857 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3858 }
3859 anim.playTogether(bounceAnims);
3860 anim.start();
3861 }
3862 });
Winson Chungf0ea4d32011-06-06 14:27:16 -07003863
Winson Chung785d2eb2011-04-14 16:08:02 -07003864 if (mAppsCustomizeContent != null) {
3865 mAppsCustomizeContent.addApps(apps);
3866 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003867 }
3868
3869 /**
3870 * A package was updated.
3871 *
3872 * Implementation of the method from LauncherModel.Callbacks.
3873 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003874 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3875 if (waitUntilResume(new Runnable() {
3876 public void run() {
3877 bindAppsUpdated(apps);
3878 }
3879 })) {
3880 return;
3881 }
3882
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003883 if (mWorkspace != null) {
3884 mWorkspace.updateShortcuts(apps);
3885 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003886
Winson Chung785d2eb2011-04-14 16:08:02 -07003887 if (mAppsCustomizeContent != null) {
3888 mAppsCustomizeContent.updateApps(apps);
3889 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003890 }
3891
3892 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003893 * A package was uninstalled. We take both the super set of packageNames
3894 * in addition to specific applications to remove, the reason being that
3895 * this can be called when a package is updated as well. In that scenario,
3896 * we only remove specific components from the workspace, where as
3897 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003898 *
3899 * Implementation of the method from LauncherModel.Callbacks.
3900 */
Winson Chung83892cc2013-05-01 16:53:33 -07003901 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3902 final ArrayList<ApplicationInfo> appInfos,
3903 final boolean matchPackageNamesOnly) {
3904 if (waitUntilResume(new Runnable() {
3905 public void run() {
3906 bindComponentsRemoved(packageNames, appInfos, matchPackageNamesOnly);
3907 }
3908 })) {
3909 return;
3910 }
3911
3912 if (matchPackageNamesOnly) {
3913 mWorkspace.removeItemsByPackageName(packageNames);
3914 } else {
3915 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003916 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003917
Winson Chung785d2eb2011-04-14 16:08:02 -07003918 if (mAppsCustomizeContent != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07003919 mAppsCustomizeContent.removeApps(appInfos);
Winson Chung785d2eb2011-04-14 16:08:02 -07003920 }
Winson Chunga1820962011-10-03 16:31:06 -07003921
3922 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003923 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003924 }
Joe Onoratobe386092009-11-17 17:32:16 -08003925
3926 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003927 * A number of packages were updated.
3928 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003929
3930 private ArrayList<Object> mWidgetsAndShortcuts;
3931 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003932 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003933 bindPackagesUpdated(mWidgetsAndShortcuts);
3934 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003935 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003936 };
3937
3938 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3939 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3940 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003941 return;
3942 }
3943
Winson Chung785d2eb2011-04-14 16:08:02 -07003944 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003945 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003946 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003947 }
3948
Winson Chung400438b2011-01-16 17:53:48 -08003949 private int mapConfigurationOriActivityInfoOri(int configOri) {
3950 final Display d = getWindowManager().getDefaultDisplay();
3951 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3952 switch (d.getRotation()) {
3953 case Surface.ROTATION_0:
3954 case Surface.ROTATION_180:
3955 // We are currently in the same basic orientation as the natural orientation
3956 naturalOri = configOri;
3957 break;
3958 case Surface.ROTATION_90:
3959 case Surface.ROTATION_270:
3960 // We are currently in the other basic orientation to the natural orientation
3961 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3962 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3963 break;
3964 }
3965
3966 int[] oriMap = {
3967 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3968 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3969 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3970 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3971 };
3972 // Since the map starts at portrait, we need to offset if this device's natural orientation
3973 // is landscape.
3974 int indexOffset = 0;
3975 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3976 indexOffset = 1;
3977 }
3978 return oriMap[(d.getRotation() + indexOffset) % 4];
3979 }
Adam Cohen446e9402011-09-15 18:21:21 -07003980
Winson Chung4b919f82012-05-01 10:44:08 -07003981 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003982 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003983 getResources().getBoolean(R.bool.allow_rotation);
3984 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003985 }
Winson Chung4b919f82012-05-01 10:44:08 -07003986 public void lockScreenOrientation() {
3987 if (isRotationEnabled()) {
3988 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3989 .getConfiguration().orientation));
3990 }
3991 }
3992 public void unlockScreenOrientation(boolean immediate) {
3993 if (isRotationEnabled()) {
3994 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003995 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003996 } else {
3997 mHandler.postDelayed(new Runnable() {
3998 public void run() {
3999 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4000 }
4001 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004002 }
Winson Chung4b919f82012-05-01 10:44:08 -07004003 }
Winson Chung400438b2011-01-16 17:53:48 -08004004 }
4005
Winson Chung82f55532011-08-09 14:14:23 -07004006 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004007 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004008 if (DISABLE_CLINGS) {
4009 return false;
4010 }
4011
Brett Chabot2a9e2282011-08-23 15:03:13 -07004012 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004013 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004014
Michael Jurkae233a8b2013-03-19 13:49:20 +01004015 // Restricted secondary users (child mode) will potentially have very few apps
4016 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004017// boolean supportsLimitedUsers =
4018// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4019// Account[] accounts = AccountManager.get(this).getAccounts();
4020// if (supportsLimitedUsers && accounts.length == 0) {
4021// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4022// Bundle restrictions = um.getUserRestrictions();
4023// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4024// return false;
4025// }
4026// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004027 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004028 }
Michael Jurka22143132012-10-04 17:42:38 +02004029
Winson Chung7d7541e2011-09-16 20:14:36 -07004030 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004031 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004032 if (cling != null) {
4033 cling.init(this, positionData);
4034 cling.setVisibility(View.VISIBLE);
4035 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4036 if (animate) {
4037 cling.buildLayer();
4038 cling.setAlpha(0f);
4039 cling.animate()
4040 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004041 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004042 .setDuration(SHOW_CLING_DURATION)
4043 .setStartDelay(delay)
4044 .start();
4045 } else {
4046 cling.setAlpha(1f);
4047 }
Michael Jurka22143132012-10-04 17:42:38 +02004048 cling.setFocusableInTouchMode(true);
4049 cling.post(new Runnable() {
4050 public void run() {
4051 cling.setFocusable(true);
4052 cling.requestFocus();
4053 }
4054 });
4055 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4056 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004057 }
4058 return cling;
4059 }
Michael Jurka22143132012-10-04 17:42:38 +02004060
Winson Chung7d7541e2011-09-16 20:14:36 -07004061 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004062 // To catch cases where siblings of top-level views are made invisible, just check whether
4063 // the cling is directly set to GONE before dismissing it.
4064 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004065 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004066 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004067 anim.addListener(new AnimatorListenerAdapter() {
4068 public void onAnimationEnd(Animator animation) {
4069 cling.setVisibility(View.GONE);
4070 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004071 // We should update the shared preferences on a background thread
4072 new Thread("dismissClingThread") {
4073 public void run() {
4074 SharedPreferences.Editor editor = mSharedPrefs.edit();
4075 editor.putBoolean(flag, true);
4076 editor.commit();
4077 }
4078 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004079 };
4080 });
4081 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004082 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004083 }
4084 }
Michael Jurka22143132012-10-04 17:42:38 +02004085
Winson Chung9d9d74f2011-09-19 11:49:12 -07004086 private void removeCling(int id) {
4087 final View cling = findViewById(id);
4088 if (cling != null) {
4089 final ViewGroup parent = (ViewGroup) cling.getParent();
4090 parent.post(new Runnable() {
4091 @Override
4092 public void run() {
4093 parent.removeView(cling);
4094 }
4095 });
Michael Jurka22143132012-10-04 17:42:38 +02004096 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004097 }
4098 }
Michael Jurka974c3862012-05-22 22:00:31 -07004099
4100 private boolean skipCustomClingIfNoAccounts() {
4101 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4102 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4103 if (customCling) {
4104 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004105 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004106 Account[] accounts = am.getAccountsByType("com.google");
4107 return accounts.length == 0;
4108 }
4109 return false;
4110 }
4111
Winson Chung7d7541e2011-09-16 20:14:36 -07004112 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004113 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004114 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004115 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4116 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004117 // If we're not using the default workspace layout, replace workspace cling
4118 // with a custom workspace cling (usually specified in an overlay)
4119 // For now, only do this on tablets
4120 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004121 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004122 // Use a custom cling
4123 View cling = findViewById(R.id.workspace_cling);
4124 ViewGroup clingParent = (ViewGroup) cling.getParent();
4125 int clingIndex = clingParent.indexOfChild(cling);
4126 clingParent.removeViewAt(clingIndex);
4127 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4128 clingParent.addView(customCling, clingIndex);
4129 customCling.setId(R.id.workspace_cling);
4130 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004131 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004132 } else {
4133 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004134 }
4135 }
4136 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004137 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004138 if (isClingsEnabled() &&
4139 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004140 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004141 } else {
4142 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004143 }
4144 }
4145 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004146 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004147 if (isClingsEnabled() &&
4148 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4149 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004150 } else {
4151 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004152 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004153 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004154 }
4155 public boolean isFolderClingVisible() {
4156 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004157 if (cling != null) {
4158 return cling.getVisibility() == View.VISIBLE;
4159 }
4160 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004161 }
Winson Chung82f55532011-08-09 14:14:23 -07004162 public void dismissWorkspaceCling(View v) {
4163 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004164 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004165 }
4166 public void dismissAllAppsCling(View v) {
4167 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004168 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4169 }
4170 public void dismissFolderCling(View v) {
4171 Cling cling = (Cling) findViewById(R.id.folder_cling);
4172 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004173 }
4174
Winson Chung80baf5a2010-08-09 16:03:15 -07004175 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004176 * Prints out out state for debugging.
4177 */
4178 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004179 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004180 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004181 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4182 Log.d(TAG, "mRestoring=" + mRestoring);
4183 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4184 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004185 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004186 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004187
Winson Chung785d2eb2011-04-14 16:08:02 -07004188 if (mAppsCustomizeContent != null) {
4189 mAppsCustomizeContent.dumpState();
4190 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004191 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004192 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004193
4194 @Override
4195 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4196 super.dump(prefix, fd, writer, args);
4197 writer.println(" ");
4198 writer.println("Debug logs: ");
4199 for (int i = 0; i < sDumpLogs.size(); i++) {
4200 writer.println(" " + sDumpLogs.get(i));
4201 }
4202 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004203
4204 public static void dumpDebugLogsToConsole() {
4205 Log.d(TAG, "");
4206 Log.d(TAG, "*********************");
4207 Log.d(TAG, "Launcher debug logs: ");
4208 for (int i = 0; i < sDumpLogs.size(); i++) {
4209 Log.d(TAG, " " + sDumpLogs.get(i));
4210 }
4211 Log.d(TAG, "*********************");
4212 Log.d(TAG, "");
4213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004214}
Michael Jurka2763be32011-02-24 11:19:57 -08004215
Michael Jurkaabded662011-03-04 12:06:57 -08004216interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004217 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004218 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004219 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004220 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004221 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004222}