blob: 40e44d1c9870ddad754cdc11f4f18d4e9b873450 [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 Sandler924b9932013-06-12 00:38:25 -0400109 static final boolean PROFILE_STARTUP = true;
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 Sandler924b9932013-06-12 00:38:25 -0400112 static final boolean DEBUG_RESUME_TIME = true;
113 static final boolean DEBUG_MEMORY = true;
Romain Guy6fefcf12009-06-11 13:07:43 -0700114
Winson Chung70d72102011-08-12 11:24:35 -0700115 private static final int MENU_GROUP_WALLPAPER = 1;
116 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
117 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700118 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
119 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
121 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700122 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int REQUEST_PICK_APPLICATION = 6;
124 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700125 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700126 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
Michael Jurka8b805b12012-04-18 14:23:14 -0700128 private static final int REQUEST_BIND_APPWIDGET = 11;
129
Mathew Inwood876a8462013-06-14 14:12:41 +0100130 /**
131 * IntentStarter uses request codes starting with this. This must be greater than all activity
132 * request codes used internally.
133 */
134 protected static final int REQUEST_LAST = 100;
135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
137
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800138 static final int SCREEN_COUNT = 5;
139 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Romain Guy98d01652009-06-30 16:21:04 -0700141 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800142 // To turn on these properties, type
143 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
144 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
145 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Winson Chung2672ff92012-05-04 16:22:30 -0700147 // The Intent extra that defines whether to ignore the launch animation
148 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400149 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
152 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700153 // Type: int
154 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
157 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
159 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: boolean
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
165 // Type: long
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
171 // Type: parcelable
172 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100174 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700175 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100176 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700177 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100178 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700179
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700180 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700181 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700182 private State mState = State.WORKSPACE;
183 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800184 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700187 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
188 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700189 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700190 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800193 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Winson Chunga2413752012-04-03 14:22:34 -0700195 // How long to wait before the new-shortcut animation automatically pans the workspace
196 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
197
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800198 private final BroadcastReceiver mCloseSystemDialogsReceiver
199 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800200 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private LayoutInflater mInflater;
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800205 private View mQsbDivider;
206 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700207 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800208 private DragLayer mDragLayer;
209 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700210
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700211 private AppWidgetManager mAppWidgetManager;
212 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700213
Michael Jurkac9d95c52011-08-29 14:03:34 -0700214 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700215 private AppWidgetProviderInfo mPendingAddWidgetInfo;
216
Michael Jurka0280c3b2010-09-17 15:00:07 -0700217 private int[] mTmpAddItemCellCoordinates = new int[2];
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private FolderInfo mFolderInfo;
220
Winson Chung3d503fb2011-07-13 17:25:49 -0700221 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800222 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700223
Winson Chungc51db6a2011-10-05 11:44:49 -0700224 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700225 private AppsCustomizeTabHost mAppsCustomizeTabHost;
226 private AppsCustomizePagedView mAppsCustomizeContent;
227 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700230 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
231 // scroll issues (because the workspace may not have been measured yet) and extra work.
232 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
233 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
235 private SpannableStringBuilder mDefaultKeySsb = null;
236
Joe Onorato9c1289c2009-08-17 11:03:03 -0400237 private boolean mWorkspaceLoading = true;
238
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800239 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 private boolean mRestoring;
241 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700242 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243
Michael Jurka7607c2f2013-04-03 14:33:19 -0700244 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
245
Winson Chung4a2afa32012-07-19 14:53:05 -0700246 // Keep track of whether the user has left launcher
247 private static boolean sPausedFromUserAction = false;
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private Bundle mSavedInstanceState;
250
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800252 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800253 private boolean mUserPresent = true;
254 private boolean mVisible = false;
255 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700256 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700258 private static LocaleConfiguration sLocaleConfiguration = null;
259
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700260 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700261
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700262 private Intent mAppMarketIntent = null;
263
Adam Cohended9f8d2010-11-03 13:25:16 -0700264 // Related to the auto-advancing of widgets
265 private final int ADVANCE_MSG = 1;
266 private final int mAdvanceInterval = 20000;
267 private final int mAdvanceStagger = 250;
268 private long mAutoAdvanceSentTime;
269 private long mAutoAdvanceTimeLeft = -1;
270 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
271 new HashMap<View, AppWidgetProviderInfo>();
272
Winson Chung400438b2011-01-16 17:53:48 -0800273 // Determines how long to wait after a rotation before restoring the screen orientation to
274 // match the sensor state.
275 private final int mRestoreScreenOrientationDelay = 500;
276
Michael Jurka4ef207b2010-11-29 17:05:45 -0800277 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700278 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
279 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
280 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800281
Craig Mautner360310b2012-10-26 15:13:08 -0700282 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700283
Adam Cohen1462de32012-07-24 22:34:36 -0700284 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
285
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700286 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
287
Winson Chung46353de2012-02-16 14:05:10 -0800288 // We only want to get the SharedPreferences once since it does an FS stat each time we get
289 // it from the context.
290 private SharedPreferences mSharedPrefs;
291
Adam Cohene25af792013-06-06 23:08:25 -0700292 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
293
Winson Chungf0c6ae02012-03-21 16:10:31 -0700294 // Holds the page that we need to animate to, and the icon views that we need to animate up
295 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700296 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700297 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700298 private ImageView mFolderIconImageView;
299 private Bitmap mFolderIconBitmap;
300 private Canvas mFolderIconCanvas;
301 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700302
Michael Jurkaddd62e92011-02-16 17:49:14 -0800303 private BubbleTextView mWaitingForResume;
304
Michael Jurka22143132012-10-04 17:42:38 +0200305 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
306 = new HideFromAccessibilityHelper();
307
Michael Jurkac1f5d262011-09-30 19:32:27 -0700308 private Runnable mBuildLayersRunnable = new Runnable() {
309 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700310 if (mWorkspace != null) {
311 mWorkspace.buildPageHardwareLayers();
312 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700313 }
314 };
315
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800316 private static ArrayList<PendingAddArguments> sPendingAddList
317 = new ArrayList<PendingAddArguments>();
318
Michael Jurka0a457bf2012-11-19 14:05:05 -0800319 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
320
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800321 private static class PendingAddArguments {
322 int requestCode;
323 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700324 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700325 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800326 int cellX;
327 int cellY;
328 }
329
Michael Jurka0a457bf2012-11-19 14:05:05 -0800330 private static boolean isPropertyEnabled(String propertyName) {
331 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700332 }
333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 @Override
335 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700336 if (DEBUG_STRICT_MODE) {
337 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
338 .detectDiskReads()
339 .detectDiskWrites()
340 .detectNetwork() // or .detectAll() for all detectable problems
341 .penaltyLog()
342 .build());
343 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
344 .detectLeakedSqlLiteObjects()
345 .detectLeakedClosableObjects()
346 .penaltyLog()
347 .penaltyDeath()
348 .build());
349 }
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 super.onCreate(savedInstanceState);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400352 LauncherAppState app = LauncherAppState.getInstance();
353 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700354 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800355 mModel = app.setLauncher(this);
356 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400357 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700359
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700360 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700361 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
362 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700363
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700364 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
365 // this also ensures that any synchronous binding below doesn't re-trigger another
366 // LauncherModel load.
367 mPaused = false;
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200370 android.os.Debug.startMethodTracing(
371 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 }
373
374 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 setContentView(R.layout.launcher);
376 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700377 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800379 registerContentObservers();
380
Joe Onorato7c312c12009-08-13 21:36:53 -0700381 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 mSavedState = savedInstanceState;
384 restoreState(mSavedState);
385
Winson Chunga12a2502010-12-20 14:41:35 -0800386 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700387 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200388 mAppsCustomizeContent.onPackagesUpdated(
389 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700390 }
Winson Chunga12a2502010-12-20 14:41:35 -0800391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 if (PROFILE_STARTUP) {
393 android.os.Debug.stopMethodTracing();
394 }
395
396 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700397 if (sPausedFromUserAction) {
398 // If the user leaves launcher, then we should just load items asynchronously when
399 // they return.
400 mModel.startLoader(true, -1);
401 } else {
402 // We only load the page synchronously if the user rotates (or triggers a
403 // configuration change) while launcher is in the foreground
404 mModel.startLoader(true, mWorkspace.getCurrentPage());
405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
407
Michael Jurkac57b7a82011-08-09 22:02:20 -0700408 if (!mModel.isAllAppsLoaded()) {
409 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
410 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
411 }
412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 // For handling default keys
414 mDefaultKeySsb = new SpannableStringBuilder();
415 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800416
417 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
418 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800419
Adam Cohenf9426d52012-06-04 17:26:21 -0700420 updateGlobalIcons();
421
422 // On large interfaces, we want the screen to auto-rotate based on the current orientation
423 unlockScreenOrientation(true);
424 }
425
Winson Chung4a2afa32012-07-19 14:53:05 -0700426 protected void onUserLeaveHint() {
427 super.onUserLeaveHint();
428 sPausedFromUserAction = true;
429 }
430
Adam Cohenf9426d52012-06-04 17:26:21 -0700431 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700432 boolean searchVisible = false;
433 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800434 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700435 int coi = getCurrentOrientationIndexForGlobalIcons();
436 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
437 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700438 updateAppMarketIcon();
439 searchVisible = updateGlobalSearchIcon();
440 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700441 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700442 if (sGlobalSearchIcon[coi] != null) {
443 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700444 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700445 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700446 if (sVoiceSearchIcon[coi] != null) {
447 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700448 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700449 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700450 if (sAppMarketIcon[coi] != null) {
451 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800452 }
Winson Chungadf0c182012-08-23 14:59:07 -0700453 if (mSearchDropTargetBar != null) {
454 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
Romain Guycbb89e42009-06-08 15:52:54 -0700457
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700459 if (sLocaleConfiguration == null) {
460 new AsyncTask<Void, Void, LocaleConfiguration>() {
461 @Override
462 protected LocaleConfiguration doInBackground(Void... unused) {
463 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
464 readConfiguration(Launcher.this, localeConfiguration);
465 return localeConfiguration;
466 }
467
468 @Override
469 protected void onPostExecute(LocaleConfiguration result) {
470 sLocaleConfiguration = result;
471 checkForLocaleChange(); // recursive, but now with a locale configuration
472 }
473 }.execute();
474 return;
475 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700476
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 final Configuration configuration = getResources().getConfiguration();
478
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700479 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 final String locale = configuration.locale.toString();
481
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700482 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 final int mcc = configuration.mcc;
484
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700485 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 final int mnc = configuration.mnc;
487
Romain Guy84f296c2009-11-04 15:00:44 -0800488 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489
Romain Guy84f296c2009-11-04 15:00:44 -0800490 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700491 sLocaleConfiguration.locale = locale;
492 sLocaleConfiguration.mcc = mcc;
493 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700494
Joe Onorato0589f0f2010-02-08 13:44:00 -0800495 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700496
497 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
498 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700499 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700500 public void run() {
501 writeConfiguration(Launcher.this, localeConfiguration);
502 }
503 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700504 }
505 }
506
507 private static class LocaleConfiguration {
508 public String locale;
509 public int mcc = -1;
510 public int mnc = -1;
511 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700512
Romain Guy98d01652009-06-30 16:21:04 -0700513 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
514 DataInputStream in = null;
515 try {
516 in = new DataInputStream(context.openFileInput(PREFERENCES));
517 configuration.locale = in.readUTF();
518 configuration.mcc = in.readInt();
519 configuration.mnc = in.readInt();
520 } catch (FileNotFoundException e) {
521 // Ignore
522 } catch (IOException e) {
523 // Ignore
524 } finally {
525 if (in != null) {
526 try {
527 in.close();
528 } catch (IOException e) {
529 // Ignore
530 }
531 }
532 }
533 }
534
535 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
536 DataOutputStream out = null;
537 try {
538 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
539 out.writeUTF(configuration.locale);
540 out.writeInt(configuration.mcc);
541 out.writeInt(configuration.mnc);
542 out.flush();
543 } catch (FileNotFoundException e) {
544 // Ignore
545 } catch (IOException e) {
546 //noinspection ResultOfMethodCallIgnored
547 context.getFileStreamPath(PREFERENCES).delete();
548 } finally {
549 if (out != null) {
550 try {
551 out.close();
552 } catch (IOException e) {
553 // Ignore
554 }
555 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 }
557 }
558
Bjorn Bringertc459e522013-06-07 19:36:01 +0100559 public LayoutInflater getInflater() {
560 return mInflater;
561 }
562
Adam Cohen716b51e2011-06-30 12:09:54 -0700563 public DragLayer getDragLayer() {
564 return mDragLayer;
565 }
566
Winson Chung36a62fe2012-05-06 18:04:42 -0700567 boolean isDraggingEnabled() {
568 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
569 // that is subsequently removed from the workspace in startBinding().
570 return !mModel.isLoadingWorkspace();
571 }
572
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 static int getScreen() {
574 synchronized (sLock) {
575 return sScreen;
576 }
577 }
578
579 static void setScreen(int screen) {
580 synchronized (sLock) {
581 sScreen = screen;
582 }
583 }
584
Winson Chung557d6ed2011-07-08 15:34:52 -0700585 /**
586 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
587 * a configuration step, this allows the proper animations to run after other transitions.
588 */
589 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700590 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800591 switch (args.requestCode) {
592 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700593 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700594 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800595 break;
596 case REQUEST_PICK_SHORTCUT:
597 processShortcut(args.intent);
598 break;
599 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700600 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700601 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700602 result = true;
603 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800604 case REQUEST_CREATE_APPWIDGET:
605 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700606 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700607 result = true;
608 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800609 case REQUEST_PICK_WALLPAPER:
610 // We just wanted the activity result here so we can clear mWaitingForResult
611 break;
612 }
Michael Jurka27614d22012-04-02 06:40:22 -0700613 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
614 // if you turned the screen off and then back while in All Apps, Launcher would not
615 // return to the workspace. Clearing mAddInfo.container here fixes this issue
616 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700617 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 }
619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700621 protected void onActivityResult(
622 final int requestCode, final int resultCode, final Intent data) {
623 if (requestCode == REQUEST_BIND_APPWIDGET) {
624 int appWidgetId = data != null ?
625 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
626 if (resultCode == RESULT_CANCELED) {
627 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
628 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700629 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700630 }
631 return;
632 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700633 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800634 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
635 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700636 mWaitingForResult = false;
637
Adam Cohened66b2b2012-01-23 17:28:51 -0800638 // We have special handling for widgets
639 if (isWidgetDrop) {
640 int appWidgetId = data != null ?
641 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700642 if (appWidgetId < 0) {
643 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
644 "widget configuration activity.");
645 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
646 } else {
647 completeTwoStageWidgetDrop(resultCode, appWidgetId);
648 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800649 return;
650 }
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 // The pattern used here is that a user PICKs a specific application,
653 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
656 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700657 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 final PendingAddArguments args = new PendingAddArguments();
659 args.requestCode = requestCode;
660 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700661 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700662 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700663 args.cellX = mPendingAddInfo.cellX;
664 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800665 if (isWorkspaceLocked()) {
666 sPendingAddList.add(args);
667 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700668 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700672 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
674 null);
675 }
676
677 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700678 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700679 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800680 Runnable onCompleteRunnable = null;
681 int animationType = 0;
682
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700683 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 if (resultCode == RESULT_OK) {
685 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
686 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700687 mPendingAddWidgetInfo);
688 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800689 onCompleteRunnable = new Runnable() {
690 @Override
691 public void run() {
692 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700693 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800694 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
695 null);
696 }
697 };
698 } else if (resultCode == RESULT_CANCELED) {
699 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
700 onCompleteRunnable = new Runnable() {
701 @Override
702 public void run() {
703 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
704 null);
705 }
706 };
707 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700708 if (mDragLayer.getAnimatedView() != null) {
709 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
710 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
711 animationType, boundWidget, true);
712 } else {
713 // The animated view may be null in the case of a rotation during widget configuration
714 onCompleteRunnable.run();
715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717
718 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700719 protected void onStop() {
720 super.onStop();
721 FirstFrameAnimatorHelper.setIsVisible(false);
722 }
723
724 @Override
725 protected void onStart() {
726 super.onStart();
727 FirstFrameAnimatorHelper.setIsVisible(true);
728 }
729
730 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200732 long startTime = 0;
733 if (DEBUG_RESUME_TIME) {
734 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400735 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700738
Winson Chung4a2afa32012-07-19 14:53:05 -0700739 // Restore the previous launcher state
740 if (mOnResumeState == State.WORKSPACE) {
741 showWorkspace(false);
742 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
743 showAllApps(false);
744 }
745 mOnResumeState = State.NONE;
746
Craig Mautner360310b2012-10-26 15:13:08 -0700747 // Background was set to gradient in onPause(), restore to black if in all apps.
748 setWorkspaceBackground(mState == State.WORKSPACE);
749
Winson Chungde0fb8f2012-05-08 14:37:08 -0700750 // Process any items that were added while Launcher was away
751 InstallShortcutReceiver.flushInstallQueue(this);
752
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800753 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700754 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700755 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400756 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700757 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700759 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200761 if (mOnResumeCallbacks.size() > 0) {
762 // We might have postponed some bind calls until onResume (see waitUntilResume) --
763 // execute them here
764 long startTimeCallbacks = 0;
765 if (DEBUG_RESUME_TIME) {
766 startTimeCallbacks = System.currentTimeMillis();
767 }
768
769 if (mAppsCustomizeContent != null) {
770 mAppsCustomizeContent.setBulkBind(true);
771 }
772 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
773 mOnResumeCallbacks.get(i).run();
774 }
775 if (mAppsCustomizeContent != null) {
776 mAppsCustomizeContent.setBulkBind(false);
777 }
778 mOnResumeCallbacks.clear();
779 if (DEBUG_RESUME_TIME) {
780 Log.d(TAG, "Time spent processing callbacks in onResume: " +
781 (System.currentTimeMillis() - startTimeCallbacks));
782 }
Michael Jurka447bf842013-05-15 14:52:15 +0200783 }
Winson Chunge4e50662012-01-23 14:45:13 -0800784
785 // Reset the pressed state of icons that were locked in the press state while activities
786 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800787 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800788 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800789 mWaitingForResume.setStayPressed(false);
790 }
Winson Chunge4e50662012-01-23 14:45:13 -0800791 if (mAppsCustomizeContent != null) {
792 // Resets the previous all apps icon press state
793 mAppsCustomizeContent.resetDrawableState();
794 }
Adam Cohen06dff352012-06-01 17:17:08 -0700795 // It is possible that widgets can receive updates while launcher is not in the foreground.
796 // Consequently, the widgets will be inflated in the orientation of the foreground activity
797 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
798 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700799 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700800
801 // Again, as with the above scenario, it's possible that one or more of the global icons
802 // were updated in the wrong orientation.
803 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200804 if (DEBUG_RESUME_TIME) {
805 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
806 }
Adam Cohen06dff352012-06-01 17:17:08 -0700807 }
808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700810 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700811 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
812 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
813 // when Launcher resumes and we are still in AllApps.
814 updateWallpaperVisibility(true);
815
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700816 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700817 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800818 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700819 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700820 }
Romain Guycbb89e42009-06-08 15:52:54 -0700821
Adam Cohen66a01fd2013-06-11 12:48:00 -0700822 protected void onFinishBindingItems() {
823 }
824
825 // Add a fullscreen unpadded view to the workspace to the left all other screens.
826 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700827 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700828 }
829
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700830 @Override
831 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400832 // Flag the loader to stop early before switching
833 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700834 if (mAppsCustomizeContent != null) {
835 mAppsCustomizeContent.surrender();
836 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800837 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700838 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700839
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800840 // We can't hide the IME if it was forced open. So don't bother
841 /*
842 @Override
843 public void onWindowFocusChanged(boolean hasFocus) {
844 super.onWindowFocusChanged(hasFocus);
845
846 if (hasFocus) {
847 final InputMethodManager inputManager = (InputMethodManager)
848 getSystemService(Context.INPUT_METHOD_SERVICE);
849 WindowManager.LayoutParams lp = getWindow().getAttributes();
850 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
851 android.os.Handler()) {
852 protected void onReceiveResult(int resultCode, Bundle resultData) {
853 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
854 }
855 });
856 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
857 }
858 }
859 */
860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 private boolean acceptFilter() {
862 final InputMethodManager inputManager = (InputMethodManager)
863 getSystemService(Context.INPUT_METHOD_SERVICE);
864 return !inputManager.isFullscreenMode();
865 }
866
867 @Override
868 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700869 final int uniChar = event.getUnicodeChar();
870 final boolean handled = super.onKeyDown(keyCode, event);
871 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
872 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
874 keyCode, event);
875 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700876 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700877 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700878 // showSearchDialog()
879 // If there are multiple keystrokes before the search dialog takes focus,
880 // onSearchRequested() will be called for every keystroke,
881 // but it is idempotent, so it's fine.
882 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 }
884 }
885
Joe Onorato8a9625e2010-01-28 15:55:35 -0800886 // Eat the long press event so the keyboard doesn't come up.
887 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
888 return true;
889 }
890
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 return handled;
892 }
893
Karl Rosaen138a0412009-04-23 19:00:21 -0700894 private String getTypedText() {
895 return mDefaultKeySsb.toString();
896 }
897
898 private void clearTypedText() {
899 mDefaultKeySsb.clear();
900 mDefaultKeySsb.clearSpans();
901 Selection.setSelection(mDefaultKeySsb, 0);
902 }
903
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700905 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
906 * State
907 */
908 private static State intToState(int stateOrdinal) {
909 State state = State.WORKSPACE;
910 final State[] stateValues = State.values();
911 for (int i = 0; i < stateValues.length; i++) {
912 if (stateValues[i].ordinal() == stateOrdinal) {
913 state = stateValues[i];
914 break;
915 }
916 }
917 return state;
918 }
919
920 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 * Restores the previous state, if it exists.
922 *
923 * @param savedState The previous state.
924 */
925 private void restoreState(Bundle savedState) {
926 if (savedState == null) {
927 return;
928 }
929
Michael Jurka883f55b2010-10-21 15:47:14 -0700930 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700931 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700932 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800933 }
934
Winson Chungf0c6ae02012-03-21 16:10:31 -0700935 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700937 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700941 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700942
Winson Chung3d503fb2011-07-13 17:25:49 -0700943 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
944 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700945 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
947 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700948 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
949 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
950 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700951 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 mRestoring = true;
953 }
954
955 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
956 if (renameFolder) {
957 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700958 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 mRestoring = true;
960 }
Winson Chunga12a2502010-12-20 14:41:35 -0800961
Winson Chung785d2eb2011-04-14 16:08:02 -0700962 // Restore the AppsCustomize tab
963 if (mAppsCustomizeTabHost != null) {
964 String curTab = savedState.getString("apps_customize_currentTab");
965 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700966 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700967 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700968 mAppsCustomizeContent.loadAssociatedPages(
969 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700970 }
971
Winson Chung5afbf7b2011-07-25 11:53:08 -0700972 int currentIndex = savedState.getInt("apps_customize_currentIndex");
973 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976
977 /**
978 * Finds all the views we need and configure them properly.
979 */
980 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700981 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400982
Craig Mautner360310b2012-10-26 15:13:08 -0700983 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700984 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
985 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700986 mQsbDivider = findViewById(R.id.qsb_divider);
987 mDockDivider = findViewById(R.id.dock_divider);
988
989 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
990 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991
Winson Chung4c98d922011-05-31 16:50:48 -0700992 // Setup the drag layer
993 mDragLayer.setup(this, dragController);
994
Winson Chung3d503fb2011-07-13 17:25:49 -0700995 // Setup the hotseat
996 mHotseat = (Hotseat) findViewById(R.id.hotseat);
997 if (mHotseat != null) {
998 mHotseat.setup(this);
999 }
1000
Winson Chung4c98d922011-05-31 16:50:48 -07001001 // Setup the workspace
1002 mWorkspace.setHapticFeedbackEnabled(false);
1003 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001004 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001005 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001006
Winson Chungf0ea4d32011-06-06 14:27:16 -07001007 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001008 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001009
Winson Chungf0ea4d32011-06-06 14:27:16 -07001010 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001011 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001012 mAppsCustomizeContent = (AppsCustomizePagedView)
1013 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1014 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001015
Winson Chung3d503fb2011-07-13 17:25:49 -07001016 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001017 dragController.setDragScoller(mWorkspace);
1018 dragController.setScrollView(mDragLayer);
1019 dragController.setMoveTarget(mWorkspace);
1020 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001021 if (mSearchDropTargetBar != null) {
1022 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001023 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001024
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001025 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001026 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001027 final View ww = new WeightWatcher(this);
1028 ww.setAlpha(0.5f);
1029 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001030 new FrameLayout.LayoutParams(
1031 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001032 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001033 Gravity.BOTTOM)
1034 );
1035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037
1038 /**
1039 * Creates a view representing a shortcut.
1040 *
1041 * @param info The data structure describing the shortcut.
1042 *
1043 * @return A View inflated from R.layout.application.
1044 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001045 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001047 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 }
1049
1050 /**
1051 * Creates a view representing a shortcut inflated from the specified resource.
1052 *
1053 * @param layoutResId The id of the XML layout used to create the shortcut.
1054 * @param parent The group the shortcut belongs to.
1055 * @param info The data structure describing the shortcut.
1056 *
1057 * @return A View inflated from layoutResId.
1058 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001059 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001060 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1061 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 return favorite;
1064 }
1065
1066 /**
1067 * Add an application shortcut to the workspace.
1068 *
1069 * @param data The intent describing the application.
1070 * @param cellInfo The position on screen where to create the shortcut.
1071 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001072 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001074 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001075
Adam Cohenfbba09b2011-07-18 21:43:05 -07001076 // First we check if we already know the exact location where we want to add this item.
1077 if (cellX >= 0 && cellY >= 0) {
1078 cellXY[0] = cellX;
1079 cellXY[1] = cellY;
1080 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001081 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001082 return;
1083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001085 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001086
Romain Guy73b979d2009-06-09 12:57:21 -07001087 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001088 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001090 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001091 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001092 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001093 } else {
1094 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
1096 }
Romain Guycbb89e42009-06-08 15:52:54 -07001097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 /**
1099 * Add a shortcut to the workspace.
1100 *
1101 * @param data The intent describing the shortcut.
1102 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001104 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001105 int cellY) {
1106 int[] cellXY = mTmpAddItemCellCoordinates;
1107 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001108 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001109
Michael Jurkad3ef3062010-11-23 16:23:58 -08001110 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001111
Adam Cohen558baaf2011-08-15 15:22:57 -07001112 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001113 if (info == null) {
1114 return;
1115 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001116 final View view = createShortcut(info);
1117
Adam Cohenfbba09b2011-07-18 21:43:05 -07001118 // First we check if we already know the exact location where we want to add this item.
1119 if (cellX >= 0 && cellY >= 0) {
1120 cellXY[0] = cellX;
1121 cellXY[1] = cellY;
1122 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001123
1124 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001125 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001126 true, null,null)) {
1127 return;
1128 }
1129 DragObject dragObject = new DragObject();
1130 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001131 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1132 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001133 return;
1134 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001135 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001136 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001137 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001138 foundCellSpan = (result != null);
1139 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001140 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001141 }
1142
1143 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001144 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001145 return;
1146 }
1147
Adam Cohendcd297f2013-06-18 13:13:40 -07001148 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149
1150 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001151 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001152 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
1154 }
1155
Adam Cohen2f093b62012-04-30 18:59:53 -07001156 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1157 int minHeight) {
1158 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001159 // We want to account for the extra amount of padding that we are adding to the widget
1160 // to ensure that it gets the full amount of space that it has requested
1161 int requiredWidth = minWidth + padding.left + padding.right;
1162 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001163 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001164 }
1165
Adam Cohen2f093b62012-04-30 18:59:53 -07001166 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1167 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001168 }
1169
Adam Cohen2f093b62012-04-30 18:59:53 -07001170 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1171 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001172 }
1173
Adam Cohen2f093b62012-04-30 18:59:53 -07001174 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1175 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001176 }
1177
Adam Cohen2f093b62012-04-30 18:59:53 -07001178 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1179 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001180 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001181 }
1182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001184 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001186 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001187 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001189 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001190 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1191 if (appWidgetInfo == null) {
1192 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1193 }
Romain Guycbb89e42009-06-08 15:52:54 -07001194
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001195 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001196 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001197
Adam Cohen2f093b62012-04-30 18:59:53 -07001198 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1199 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001202 // We have saved the position to which the widget was dragged-- this really only matters
1203 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001204 int[] cellXY = mTmpAddItemCellCoordinates;
1205 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001206 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001208 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1209 cellXY[0] = mPendingAddInfo.cellX;
1210 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001211 spanXY[0] = mPendingAddInfo.spanX;
1212 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001213 foundCellSpan = true;
1214 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001215 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001216 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001217 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1218 spanXY[1], cellXY, finalSpan);
1219 spanXY[0] = finalSpan[0];
1220 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001221 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001223 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001224 }
1225
Michael Jurka0280c3b2010-09-17 15:00:07 -07001226 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001227 if (appWidgetId != -1) {
1228 // Deleting an app widget ID is a void call but writes to disk before returning
1229 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001230 new Thread("deleteAppWidgetId") {
1231 public void run() {
1232 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1233 }
1234 }.start();
1235 }
Winson Chung93eef082012-03-23 15:59:27 -07001236 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001237 return;
1238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001240 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001241 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1242 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001243 launcherInfo.spanX = spanXY[0];
1244 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1246 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001249 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250
1251 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001252 if (hostView == null) {
1253 // Perform actual inflation because we're live
1254 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1255 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1256 } else {
1257 // The AppWidgetHostView has already been inflated and instantiated
1258 launcherInfo.hostView = hostView;
1259 }
Romain Guycbb89e42009-06-08 15:52:54 -07001260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001262 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001263 launcherInfo.notifyWidgetSizeChanged(this);
1264
Adam Cohendcd297f2013-06-18 13:13:40 -07001265 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001266 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001267
1268 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001270 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
Romain Guycbb89e42009-06-08 15:52:54 -07001272
Adam Cohended9f8d2010-11-03 13:25:16 -07001273 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1274 @Override
1275 public void onReceive(Context context, Intent intent) {
1276 final String action = intent.getAction();
1277 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1278 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001279 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001280 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001281
Winson Chungc100e8e2011-08-09 16:02:43 -07001282 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001283 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001284 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1285 mAppsCustomizeTabHost.reset();
1286 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001287 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1289 mUserPresent = true;
1290 updateRunning();
1291 }
1292 }
1293 };
1294
1295 @Override
1296 public void onAttachedToWindow() {
1297 super.onAttachedToWindow();
1298
1299 // Listen for broadcasts related to user-presence
1300 final IntentFilter filter = new IntentFilter();
1301 filter.addAction(Intent.ACTION_SCREEN_OFF);
1302 filter.addAction(Intent.ACTION_USER_PRESENT);
1303 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001304 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001305 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001306 mVisible = true;
1307 }
1308
1309 @Override
1310 public void onDetachedFromWindow() {
1311 super.onDetachedFromWindow();
1312 mVisible = false;
1313
Adam Cohend113e0c2010-11-11 10:48:05 -08001314 if (mAttached) {
1315 unregisterReceiver(mReceiver);
1316 mAttached = false;
1317 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001318 updateRunning();
1319 }
1320
1321 public void onWindowVisibilityChanged(int visibility) {
1322 mVisible = visibility == View.VISIBLE;
1323 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001324 // The following code used to be in onResume, but it turns out onResume is called when
1325 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1326 // is a more appropriate event to handle
1327 if (mVisible) {
1328 mAppsCustomizeTabHost.onWindowVisible();
1329 if (!mWorkspaceLoading) {
1330 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001331 // We want to let Launcher draw itself at least once before we force it to build
1332 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001333 // apps is nice and speedy.
1334 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001335 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001336 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001337 if (mStarted) return;
1338 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001339 // We delay the layer building a bit in order to give
1340 // other message processing a time to run. In particular
1341 // this avoids a delay in hiding the IME if it was
1342 // currently shown, because doing that may involve
1343 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001344 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001345 final ViewTreeObserver.OnDrawListener listener = this;
1346 mWorkspace.post(new Runnable() {
1347 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001348 if (mWorkspace != null &&
1349 mWorkspace.getViewTreeObserver() != null) {
1350 mWorkspace.getViewTreeObserver().
1351 removeOnDrawListener(listener);
1352 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001353 }
1354 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001355 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001356 }
1357 });
1358 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001359 // When Launcher comes back to foreground, a different Activity might be responsible for
1360 // the app market intent, so refresh the icon
1361 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001362 clearTypedText();
1363 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001364 }
1365
1366 private void sendAdvanceMessage(long delay) {
1367 mHandler.removeMessages(ADVANCE_MSG);
1368 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1369 mHandler.sendMessageDelayed(msg, delay);
1370 mAutoAdvanceSentTime = System.currentTimeMillis();
1371 }
1372
1373 private void updateRunning() {
1374 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1375 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1376 mAutoAdvanceRunning = autoAdvanceRunning;
1377 if (autoAdvanceRunning) {
1378 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1379 sendAdvanceMessage(delay);
1380 } else {
1381 if (!mWidgetsToAdvance.isEmpty()) {
1382 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1383 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1384 }
1385 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001386 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001387 }
1388 }
1389 }
1390
1391 private final Handler mHandler = new Handler() {
1392 @Override
1393 public void handleMessage(Message msg) {
1394 if (msg.what == ADVANCE_MSG) {
1395 int i = 0;
1396 for (View key: mWidgetsToAdvance.keySet()) {
1397 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1398 final int delay = mAdvanceStagger * i;
1399 if (v instanceof Advanceable) {
1400 postDelayed(new Runnable() {
1401 public void run() {
1402 ((Advanceable) v).advance();
1403 }
1404 }, delay);
1405 }
1406 i++;
1407 }
1408 sendAdvanceMessage(mAdvanceInterval);
1409 }
1410 }
1411 };
1412
1413 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001414 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001415 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1416 if (v instanceof Advanceable) {
1417 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001418 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001419 updateRunning();
1420 }
1421 }
1422
1423 void removeWidgetToAutoAdvance(View hostView) {
1424 if (mWidgetsToAdvance.containsKey(hostView)) {
1425 mWidgetsToAdvance.remove(hostView);
1426 updateRunning();
1427 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001428 }
1429
Joe Onorato9c1289c2009-08-17 11:03:03 -04001430 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001431 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 launcherInfo.hostView = null;
1433 }
1434
Winson Chung93eef082012-03-23 15:59:27 -07001435 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1436 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1437 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001438 }
1439
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001440 public LauncherAppWidgetHost getAppWidgetHost() {
1441 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
Romain Guycbb89e42009-06-08 15:52:54 -07001443
Winson Chunga9abd0e2010-10-27 17:18:37 -07001444 public LauncherModel getModel() {
1445 return mModel;
1446 }
1447
Bjorn Bringertc459e522013-06-07 19:36:01 +01001448 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001449 getWindow().closeAllPanels();
1450
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001451 // Whatever we were doing is hereby canceled.
1452 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001453 }
1454
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 @Override
1456 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001457 long startTime = 0;
1458 if (DEBUG_RESUME_TIME) {
1459 startTime = System.currentTimeMillis();
1460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 super.onNewIntent(intent);
1462
1463 // Close the menu
1464 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001465 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001466 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001467
Jamie Genniscb222e82012-10-23 15:44:26 -07001468 final boolean alreadyOnHome =
1469 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001470 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001471
Jamie Genniscb222e82012-10-23 15:44:26 -07001472 Runnable processIntent = new Runnable() {
1473 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001474 if (mWorkspace == null) {
1475 // Can be cases where mWorkspace is null, this prevents a NPE
1476 return;
1477 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001478 Folder openFolder = mWorkspace.getOpenFolder();
1479 // In all these cases, only animate if we're already on home
1480 mWorkspace.exitWidgetResizeMode();
1481 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1482 openFolder == null) {
1483 mWorkspace.moveToDefaultScreen(true);
1484 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001485
Jamie Genniscb222e82012-10-23 15:44:26 -07001486 closeFolder();
1487 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001488
Jamie Genniscb222e82012-10-23 15:44:26 -07001489 // If we are already on home, then just animate back to the workspace,
1490 // otherwise, just wait until onResume to set the state back to Workspace
1491 if (alreadyOnHome) {
1492 showWorkspace(true);
1493 } else {
1494 mOnResumeState = State.WORKSPACE;
1495 }
1496
1497 final View v = getWindow().peekDecorView();
1498 if (v != null && v.getWindowToken() != null) {
1499 InputMethodManager imm = (InputMethodManager)getSystemService(
1500 INPUT_METHOD_SERVICE);
1501 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1502 }
1503
1504 // Reset AllApps to its initial state
1505 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1506 mAppsCustomizeTabHost.reset();
1507 }
1508 }
1509 };
1510
1511 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1512 // Delay processing of the intent to allow the status bar animation to finish
1513 // first in order to avoid janky animations.
1514 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001515 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001516 // Process the intent immediately.
1517 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001518 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 }
Michael Jurka447bf842013-05-15 14:52:15 +02001521 if (DEBUG_RESUME_TIME) {
1522 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 }
1525
1526 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001527 public void onRestoreInstanceState(Bundle state) {
1528 super.onRestoreInstanceState(state);
1529 for (int page: mSynchronouslyBoundPages) {
1530 mWorkspace.restoreInstanceStateForChild(page);
1531 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
1533
1534 @Override
1535 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001536 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001537 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
Michael Jurka883f55b2010-10-21 15:47:14 -07001539 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001540 // We close any open folder since it will not be re-opened, and we need to make sure
1541 // this state is reflected.
1542 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 mWaitingForResult) {
1546 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001548 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1549 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001550 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1551 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1552 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554
1555 if (mFolderInfo != null && mWaitingForResult) {
1556 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1557 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1558 }
Michael Jurka946ad472010-07-09 18:05:18 -07001559
Winson Chung785d2eb2011-04-14 16:08:02 -07001560 // Save the current AppsCustomize tab
1561 if (mAppsCustomizeTabHost != null) {
1562 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1563 if (currentTabTag != null) {
1564 outState.putString("apps_customize_currentTab", currentTabTag);
1565 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001566 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1567 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001568 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570
1571 @Override
1572 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001574
Winson Chunge7a03942011-08-05 15:05:12 -07001575 // Remove all pending runnables
1576 mHandler.removeMessages(ADVANCE_MSG);
1577 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001578 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001579
Winson Chungcd2b0142011-06-08 16:02:26 -07001580 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001581 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001582 mModel.stopLoader();
1583 app.setLauncher(null);
1584
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001586 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001588 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001590 mAppWidgetHost = null;
1591
1592 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593
1594 TextKeyListener.getInstance().release();
1595
Winson Chung81b52252012-08-27 15:34:29 -07001596 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1597 // to prevent leaking Launcher activities on orientation change.
1598 if (mModel != null) {
1599 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1600 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001601
1602 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001603 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001604
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001605 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001606 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1607 mWorkspace.removeAllViews();
1608 mWorkspace = null;
1609 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001610
Michael Jurka2ecf9952012-06-18 12:52:28 -07001611 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 }
1613
Adam Cohena9cf38f2011-05-02 15:36:58 -07001614 public DragController getDragController() {
1615 return mDragController;
1616 }
1617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 @Override
1619 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001620 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 super.startActivityForResult(intent, requestCode);
1622 }
1623
Winson Chung70d72102011-08-12 11:24:35 -07001624 /**
1625 * Indicates that we want global search for this activity by setting the globalSearch
1626 * argument for {@link #startSearch} to true.
1627 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001629 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001631
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001632 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001633
Karl Rosaen138a0412009-04-23 19:00:21 -07001634 if (initialQuery == null) {
1635 // Use any text typed in the launcher as the initial query
1636 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 if (appSearchData == null) {
1639 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001640 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 }
Winson Chungadf0c182012-08-23 14:59:07 -07001642 Rect sourceBounds = new Rect();
1643 if (mSearchDropTargetBar != null) {
1644 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1645 }
Romain Guycbb89e42009-06-08 15:52:54 -07001646
Bjorn Bringertc459e522013-06-07 19:36:01 +01001647 startSearch(initialQuery, selectInitialQuery,
1648 appSearchData, sourceBounds);
1649 }
1650
1651 public void startSearch(String initialQuery,
1652 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001653 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001654 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001655 }
1656
1657 /**
1658 * Starts the global search activity. This code is a copied from SearchManager
1659 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001660 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001661 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001662 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001663 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1664 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1665 if (globalSearchActivity == null) {
1666 Log.w(TAG, "No global search activity found.");
1667 return;
1668 }
1669 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1670 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1671 intent.setComponent(globalSearchActivity);
1672 // Make sure that we have a Bundle to put source in
1673 if (appSearchData == null) {
1674 appSearchData = new Bundle();
1675 } else {
1676 appSearchData = new Bundle(appSearchData);
1677 }
1678 // Set source to package name of app that starts global search, if not set already.
1679 if (!appSearchData.containsKey("source")) {
1680 appSearchData.putString("source", getPackageName());
1681 }
1682 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1683 if (!TextUtils.isEmpty(initialQuery)) {
1684 intent.putExtra(SearchManager.QUERY, initialQuery);
1685 }
1686 if (selectInitialQuery) {
1687 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1688 }
1689 intent.setSourceBounds(sourceBounds);
1690 try {
1691 startActivity(intent);
1692 } catch (ActivityNotFoundException ex) {
1693 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 }
1696
Winson Chung70d72102011-08-12 11:24:35 -07001697 @Override
1698 public boolean onCreateOptionsMenu(Menu menu) {
1699 if (isWorkspaceLocked()) {
1700 return false;
1701 }
1702
1703 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001704
1705 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1706 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1707 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001708 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1709 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1710 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001711 String helpUrl = getString(R.string.help_url);
1712 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001713 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1714 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1715
Winson Chung70d72102011-08-12 11:24:35 -07001716 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1717 .setIcon(android.R.drawable.ic_menu_gallery)
1718 .setAlphabeticShortcut('W');
1719 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1720 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001721 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001722 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001723 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1724 .setIcon(android.R.drawable.ic_menu_preferences)
1725 .setIntent(settings)
1726 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001727 if (!helpUrl.isEmpty()) {
1728 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1729 .setIcon(android.R.drawable.ic_menu_help)
1730 .setIntent(help)
1731 .setAlphabeticShortcut('H');
1732 }
Winson Chung70d72102011-08-12 11:24:35 -07001733 return true;
1734 }
1735
1736 @Override
1737 public boolean onPrepareOptionsMenu(Menu menu) {
1738 super.onPrepareOptionsMenu(menu);
1739
1740 if (mAppsCustomizeTabHost.isTransitioning()) {
1741 return false;
1742 }
1743 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1744 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1745
1746 return true;
1747 }
1748
1749 @Override
1750 public boolean onOptionsItemSelected(MenuItem item) {
1751 switch (item.getItemId()) {
1752 case MENU_WALLPAPER_SETTINGS:
1753 startWallpaper();
1754 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001755 }
1756
1757 return super.onOptionsItemSelected(item);
1758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001760 @Override
1761 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001762 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001763 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001764 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001765 }
1766
Joe Onorato9c1289c2009-08-17 11:03:03 -04001767 public boolean isWorkspaceLocked() {
1768 return mWorkspaceLoading || mWaitingForResult;
1769 }
1770
Michael Jurka0280c3b2010-09-17 15:00:07 -07001771 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001772 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001773 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001774 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1775 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001776 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001777 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001778 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001779
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001780 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1781 AppWidgetProviderInfo appWidgetInfo) {
1782 if (appWidgetInfo.configure != null) {
1783 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001784
Bjorn Bringert7984c942009-12-09 15:38:25 +00001785 // Launch over to configure widget, if needed
1786 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001787 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001788 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001789 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001791 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001792 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001793 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001794 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001795 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797 }
Romain Guycbb89e42009-06-08 15:52:54 -07001798
Adam Cohenfbba09b2011-07-18 21:43:05 -07001799 /**
1800 * Process a shortcut drop.
1801 *
1802 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001803 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001804 * @param cell The cell it should be added to, optional
1805 * @param position The location on the screen where it was dropped, optional
1806 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001807 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001808 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001809 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001810 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001811 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001812 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001813
1814 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001815 mPendingAddInfo.cellX = cell[0];
1816 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001817 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001818
1819 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1820 createShortcutIntent.setComponent(componentName);
1821 processShortcut(createShortcutIntent);
1822 }
1823
Adam Cohenfbba09b2011-07-18 21:43:05 -07001824 /**
1825 * Process a widget drop.
1826 *
1827 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001828 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001829 * @param cell The cell it should be added to, optional
1830 * @param position The location on the screen where it was dropped, optional
1831 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001832 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001833 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001834 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001835 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001836 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001837 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001838 mPendingAddInfo.minSpanX = info.minSpanX;
1839 mPendingAddInfo.minSpanY = info.minSpanY;
1840
Adam Cohenfbba09b2011-07-18 21:43:05 -07001841 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001842 mPendingAddInfo.cellX = cell[0];
1843 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001844 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001845 if (span != null) {
1846 mPendingAddInfo.spanX = span[0];
1847 mPendingAddInfo.spanY = span[1];
1848 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001849
Adam Cohened66b2b2012-01-23 17:28:51 -08001850 AppWidgetHostView hostView = info.boundWidget;
1851 int appWidgetId;
1852 if (hostView != null) {
1853 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001854 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001855 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001856 // In this case, we either need to start an activity to get permission to bind
1857 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001858 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001859 Bundle options = info.bindOptions;
1860
1861 boolean success = false;
1862 if (options != null) {
1863 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1864 info.componentName, options);
1865 } else {
1866 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1867 info.componentName);
1868 }
1869 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001870 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001871 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001872 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001873 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1874 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1875 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001876 // TODO: we need to make sure that this accounts for the options bundle.
1877 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001878 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1879 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001880 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001881 }
1882
Joe Onoratodeb98af2010-02-19 14:59:39 -08001883 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001884 // Handle case where user selected "Applications"
1885 String applicationName = getResources().getString(R.string.group_applications);
1886 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001887
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001888 if (applicationName != null && applicationName.equals(shortcutName)) {
1889 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1890 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001891
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001892 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1893 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001894 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001895 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001896 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001897 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
1900
Winson Chung24ab2f12010-09-16 14:10:47 -07001901 void processWallpaper(Intent intent) {
1902 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1903 }
1904
Adam Cohendcd297f2013-06-18 13:13:40 -07001905 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001906 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001907 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 folderInfo.title = getText(R.string.folder_name);
1909
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001911 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001912 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001913 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914
1915 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001916 FolderIcon newFolder =
1917 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001918 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001919 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001920 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
Romain Guycbb89e42009-06-08 15:52:54 -07001922
Joe Onorato9c1289c2009-08-17 11:03:03 -04001923 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001924 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001925 }
1926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001928 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001930 Intent chooser = Intent.createChooser(pickWallpaper,
1931 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001932 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1933 // Removed in Eclair MR1
1934// WallpaperManager wm = (WallpaperManager)
1935// getSystemService(Context.WALLPAPER_SERVICE);
1936// WallpaperInfo wi = wm.getWallpaperInfo();
1937// if (wi != null && wi.getSettingsActivity() != null) {
1938// LabeledIntent li = new LabeledIntent(getPackageName(),
1939// R.string.configure_wallpaper, 0);
1940// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1941// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1942// }
Mike Clerona0618e42009-10-22 13:55:21 -07001943 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
1945
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001946 /**
1947 * Registers various content observers. The current implementation registers
1948 * only a favorites observer to keep track of the favorites applications.
1949 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001950 private void registerContentObservers() {
1951 ContentResolver resolver = getContentResolver();
1952 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1953 true, mWidgetObserver);
1954 }
1955
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 @Override
1957 public boolean dispatchKeyEvent(KeyEvent event) {
1958 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1959 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001961 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001962 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001963 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001964 dumpState();
1965 return true;
1966 }
1967 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001968 }
1969 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1970 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001971 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 return true;
1973 }
1974 }
1975
1976 return super.dispatchKeyEvent(event);
1977 }
1978
Joe Onorato88ec0992009-11-19 13:16:06 -08001979 @Override
1980 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001981 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001982 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001983 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001984 Folder openFolder = mWorkspace.getOpenFolder();
1985 if (openFolder.isEditingName()) {
1986 openFolder.dismissEditingName();
1987 } else {
1988 closeFolder();
1989 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001990 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001991 mWorkspace.exitWidgetResizeMode();
1992
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001993 // Back button is a no-op here, but give at least some feedback for the button press
1994 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001995 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001996 }
1997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001999 * Re-listen when widgets are reset.
2000 */
2001 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002002 if (mAppWidgetHost != null) {
2003 mAppWidgetHost.startListening();
2004 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002005 }
2006
2007 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 * Launches the intent referred by the clicked shortcut.
2009 *
2010 * @param v The view representing the clicked shortcut.
2011 */
2012 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002013 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2014 // view has detached (it's possible for this to happen if the view is removed mid touch).
2015 if (v.getWindowToken() == null) {
2016 return;
2017 }
2018
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002019 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002020 return;
2021 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002024 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002026 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002027
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002028 // Check for special shortcuts
2029 if (intent.getComponent() != null) {
2030 final String shortcutClass = intent.getComponent().getClassName();
2031
2032 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2033 showAllApps(true);
2034 return;
2035 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2036 MemoryDumpActivity.startDump(this);
2037 return;
2038 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002039 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002040
2041 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002042 int[] pos = new int[2];
2043 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002044 intent.setSourceBounds(new Rect(pos[0], pos[1],
2045 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002046
2047 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002048
2049 if (success && v instanceof BubbleTextView) {
2050 mWaitingForResume = (BubbleTextView) v;
2051 mWaitingForResume.setStayPressed(true);
2052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002054 if (v instanceof FolderIcon) {
2055 FolderIcon fi = (FolderIcon) v;
2056 handleFolderClick(fi);
2057 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002058 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002059 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002060 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002061 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002062 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 }
2065 }
2066
Michael Jurka0e260592010-06-30 17:07:39 -07002067 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002068 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002069 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002070 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2071 showWorkspace(true);
2072 }
Michael Jurka0e260592010-06-30 17:07:39 -07002073 return false;
2074 }
2075
Michael Jurkaaf442092010-06-10 17:01:57 -07002076 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002077 * Event handler for the search button
2078 *
2079 * @param v The view that was clicked.
2080 */
2081 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002082 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2083
Amith Yamasania135ba82011-08-09 17:42:01 -07002084 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002085 }
2086
2087 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002088 * Event handler for the voice button
2089 *
2090 * @param v The view that was clicked.
2091 */
2092 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002093 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002094
Bjorn Bringertc459e522013-06-07 19:36:01 +01002095 startVoice();
2096 }
2097
2098 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002099 try {
2100 final SearchManager searchManager =
2101 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2102 ComponentName activityName = searchManager.getGlobalSearchActivity();
2103 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002104 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002105 if (activityName != null) {
2106 intent.setPackage(activityName.getPackageName());
2107 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002108 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002109 } catch (ActivityNotFoundException e) {
2110 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002111 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002112 startActivitySafely(null, intent, "onClickVoiceButton");
2113 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002114 }
2115
2116 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002117 * Event handler for the "grid" button that appears on the home screen, which
2118 * enters all apps mode.
2119 *
2120 * @param v The view that was clicked.
2121 */
2122 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002123 showAllApps(true);
2124 }
2125
2126 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002127 // Provide the same haptic feedback that the system offers for virtual keys.
2128 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002129 }
2130
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002131 public void onClickAppMarketButton(View v) {
2132 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002133 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002134 } else {
2135 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002136 }
2137 }
2138
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002139 void startApplicationDetailsActivity(ComponentName componentName) {
2140 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002141 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2142 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002143 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002144 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002145 }
2146
Patrick Dubroy5539af72010-09-07 15:22:01 -07002147 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2148 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2149 // System applications cannot be installed. For now, show a toast explaining that.
2150 // We may give them the option of disabling apps this way.
2151 int messageId = R.string.uninstall_system_app_text;
2152 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2153 } else {
2154 String packageName = appInfo.componentName.getPackageName();
2155 String className = appInfo.componentName.getClassName();
2156 Intent intent = new Intent(
2157 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002158 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2159 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002160 startActivity(intent);
2161 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002162 }
2163
Michael Jurka86a720e2012-05-09 11:23:23 -07002164 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002168 // Only launch using the new animation if the shortcut has not opted out (this is a
2169 // private contract between launcher and may be ignored in the future).
2170 boolean useLaunchAnimation = (v != null) &&
2171 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2172 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002173 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2174 v.getMeasuredWidth(), v.getMeasuredHeight());
2175
2176 startActivity(intent, opts.toBundle());
2177 } else {
2178 startActivity(intent);
2179 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002180 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 } catch (SecurityException e) {
2182 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002183 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002185 "or use the exported attribute for this activity. "
2186 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002188 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002190
Michael Jurka86a720e2012-05-09 11:23:23 -07002191 boolean startActivitySafely(View v, Intent intent, Object tag) {
2192 boolean success = false;
2193 try {
2194 success = startActivity(v, intent, tag);
2195 } catch (ActivityNotFoundException e) {
2196 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2197 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2198 }
2199 return success;
2200 }
2201
Romain Guy8e633c52010-03-04 12:51:36 -08002202 void startActivityForResultSafely(Intent intent, int requestCode) {
2203 try {
2204 startActivityForResult(intent, requestCode);
2205 } catch (ActivityNotFoundException e) {
2206 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2207 } catch (SecurityException e) {
2208 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2209 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2210 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2211 "or use the exported attribute for this activity.", e);
2212 }
2213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214
Adam Cohena9cf38f2011-05-02 15:36:58 -07002215 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002216 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002217 Folder openFolder = mWorkspace.getFolderForTag(info);
2218
2219 // If the folder info reports that the associated folder is open, then verify that
2220 // it is actually opened. There have been a few instances where this gets out of sync.
2221 if (info.opened && openFolder == null) {
2222 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002223 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002224 info.opened = false;
2225 }
2226
Adam Cohenfb91f302012-06-11 15:45:18 -07002227 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002228 // Close any open folder
2229 closeFolder();
2230 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002231 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 } else {
2233 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 int folderScreen;
2235 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002236 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 // .. and close it
2238 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002239 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 // Close any folder open on the current screen
2241 closeFolder();
2242 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002243 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 }
2245 }
2246 }
2247 }
2248
Adam Cohen268c4752012-06-06 17:47:33 -07002249 /**
2250 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2251 * in the DragLayer in the exact absolute location of the original FolderIcon.
2252 */
2253 private void copyFolderIconToImage(FolderIcon fi) {
2254 final int width = fi.getMeasuredWidth();
2255 final int height = fi.getMeasuredHeight();
2256
2257 // Lazy load ImageView, Bitmap and Canvas
2258 if (mFolderIconImageView == null) {
2259 mFolderIconImageView = new ImageView(this);
2260 }
2261 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2262 mFolderIconBitmap.getHeight() != height) {
2263 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2264 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2265 }
2266
2267 DragLayer.LayoutParams lp;
2268 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2269 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2270 } else {
2271 lp = new DragLayer.LayoutParams(width, height);
2272 }
2273
Adam Cohen307fe232012-08-16 17:55:58 -07002274 // The layout from which the folder is being opened may be scaled, adjust the starting
2275 // view size by this scale factor.
2276 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002277 lp.customPosition = true;
2278 lp.x = mRectForFolderAnimation.left;
2279 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002280 lp.width = (int) (scale * width);
2281 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002282
2283 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2284 fi.draw(mFolderIconCanvas);
2285 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002286 if (fi.getFolder() != null) {
2287 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2288 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002289 }
Adam Cohen268c4752012-06-06 17:47:33 -07002290 // Just in case this image view is still in the drag layer from a previous animation,
2291 // we remove it and re-add it.
2292 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2293 mDragLayer.removeView(mFolderIconImageView);
2294 }
2295 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002296 if (fi.getFolder() != null) {
2297 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002298 }
Adam Cohen268c4752012-06-06 17:47:33 -07002299 }
2300
Adam Cohen2801caf2011-05-13 20:57:39 -07002301 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002302 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002303 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2304 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2305 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2306
Adam Cohenc51934b2011-07-26 21:07:43 -07002307 FolderInfo info = (FolderInfo) fi.getTag();
2308 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2309 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002310 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2311 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002312 }
2313
Adam Cohen268c4752012-06-06 17:47:33 -07002314 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2315 copyFolderIconToImage(fi);
2316 fi.setVisibility(View.INVISIBLE);
2317
Michael Jurka2ecf9952012-06-18 12:52:28 -07002318 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002319 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002320 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2321 oa.start();
2322 }
2323
Adam Cohen268c4752012-06-06 17:47:33 -07002324 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002325 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002326 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2327 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2328 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2329
Adam Cohen268c4752012-06-06 17:47:33 -07002330 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002331
Adam Cohen268c4752012-06-06 17:47:33 -07002332 // We remove and re-draw the FolderIcon in-case it has changed
2333 mDragLayer.removeView(mFolderIconImageView);
2334 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002335 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002336 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002337 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002338 oa.addListener(new AnimatorListenerAdapter() {
2339 @Override
2340 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002341 if (cl != null) {
2342 cl.clearFolderLeaveBehind();
2343 // Remove the ImageView copy of the FolderIcon and make the original visible.
2344 mDragLayer.removeView(mFolderIconImageView);
2345 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002346 }
2347 }
2348 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002349 oa.start();
2350 }
2351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002353 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 * is animated relative to the specified View. If the View is null, no animation
2355 * is played.
2356 *
2357 * @param folderInfo The FolderInfo describing the folder to open.
2358 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002359 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002360 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002361 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362
Adam Cohena9cf38f2011-05-02 15:36:58 -07002363 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364
Adam Cohen4554ee12011-08-03 16:13:21 -07002365 // Just verify that the folder hasn't already been added to the DragLayer.
2366 // There was a one-off crash where the folder had a parent already.
2367 if (folder.getParent() == null) {
2368 mDragLayer.addView(folder);
2369 mDragController.addDropTarget((DropTarget) folder);
2370 } else {
2371 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2372 folder.getParent() + ").");
2373 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002374 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002375 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002376
2377 // Notify the accessibility manager that this folder "window" has appeared and occluded
2378 // the workspace items
2379 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2380 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002381 }
2382
2383 public void closeFolder() {
2384 Folder folder = mWorkspace.getOpenFolder();
2385 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002386 if (folder.isEditingName()) {
2387 folder.dismissEditingName();
2388 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002389 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002390
2391 // Dismiss the folder cling
2392 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002393 }
2394 }
2395
2396 void closeFolder(Folder folder) {
2397 folder.getInfo().opened = false;
2398
2399 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2400 if (parent != null) {
2401 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2402 shrinkAndFadeInFolderIcon(fi);
2403 }
2404 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002405
2406 // Notify the accessibility manager that this folder "window" has disappeard and no
2407 // longer occludeds the workspace items
2408 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 }
2410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002412 if (!isDraggingEnabled()) return false;
2413 if (isWorkspaceLocked()) return false;
2414 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415
2416 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002417 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 }
2419
Michael Jurka0280c3b2010-09-17 15:00:07 -07002420 resetAddInfo();
2421 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002423 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 return true;
2425 }
2426
Winson Chung3d503fb2011-07-13 17:25:49 -07002427 // The hotseat touch handling does not go through Workspace, and we always allow long press
2428 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002429 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002430 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2431 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002432 if (itemUnderLongClick == null) {
2433 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002434 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2435 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002436 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002438 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002440 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 }
2442 }
2443 }
2444 return true;
2445 }
2446
Winson Chung3d503fb2011-07-13 17:25:49 -07002447 boolean isHotseatLayout(View layout) {
2448 return mHotseat != null && layout != null &&
2449 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2450 }
2451 Hotseat getHotseat() {
2452 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002453 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002454 SearchDropTargetBar getSearchBar() {
2455 return mSearchDropTargetBar;
2456 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002457
Winson Chung3d503fb2011-07-13 17:25:49 -07002458 /**
2459 * Returns the CellLayout of the specified container at the specified screen.
2460 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002461 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002462 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2463 if (mHotseat != null) {
2464 return mHotseat.getLayout();
2465 } else {
2466 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002467 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002468 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002469 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002470 }
2471 }
2472
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 Workspace getWorkspace() {
2474 return mWorkspace;
2475 }
2476
Daniel Sandler843e8602010-06-07 14:59:01 -04002477 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002478 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002479 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002480 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002481 }
2482
Craig Mautner360310b2012-10-26 15:13:08 -07002483 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002484 public boolean isAllAppsButtonRank(int rank) {
2485 return mHotseat.isAllAppsButtonRank(rank);
2486 }
2487
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002488 /**
2489 * Helper method for the cameraZoomIn/cameraZoomOut animations
2490 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002491 * @param scaleFactor The scale factor used for the zoom
2492 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002493 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002494 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002495 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002496 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002497
Winson Chung18f41f82012-05-09 13:28:10 -07002498 void disableWallpaperIfInAllApps() {
2499 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002500 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002501 if (mAppsCustomizeTabHost != null &&
2502 !mAppsCustomizeTabHost.isTransitioning()) {
2503 updateWallpaperVisibility(false);
2504 }
2505 }
2506 }
2507
Craig Mautner360310b2012-10-26 15:13:08 -07002508 private void setWorkspaceBackground(boolean workspace) {
2509 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002510 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002511 }
2512
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002513 void updateWallpaperVisibility(boolean visible) {
2514 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2515 int curflags = getWindow().getAttributes().flags
2516 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2517 if (wpflags != curflags) {
2518 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2519 }
Craig Mautner360310b2012-10-26 15:13:08 -07002520 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002521 }
2522
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002523 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2524 if (v instanceof LauncherTransitionable) {
2525 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2526 }
2527 }
2528
Michael Jurkabed61d22012-02-14 22:51:29 -08002529 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2530 if (v instanceof LauncherTransitionable) {
2531 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2532 }
Winson Chung70442722012-02-10 15:43:22 -08002533
2534 // Update the workspace transition step as well
2535 dispatchOnLauncherTransitionStep(v, 0f);
2536 }
2537
2538 private void dispatchOnLauncherTransitionStep(View v, float t) {
2539 if (v instanceof LauncherTransitionable) {
2540 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2541 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002542 }
2543
2544 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2545 if (v instanceof LauncherTransitionable) {
2546 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2547 }
Winson Chung70442722012-02-10 15:43:22 -08002548
2549 // Update the workspace transition step as well
2550 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002551 }
2552
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002553 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002554 * Things to test when changing the following seven functions.
2555 * - Home from workspace
2556 * - from center screen
2557 * - from other screens
2558 * - Home from all apps
2559 * - from center screen
2560 * - from other screens
2561 * - Back from all apps
2562 * - from center screen
2563 * - from other screens
2564 * - Launch app from workspace and quit
2565 * - with back
2566 * - with home
2567 * - Launch app from all apps and quit
2568 * - with back
2569 * - with home
2570 * - Go to a screen that's not the default, then all
2571 * apps, and launch and app, and go back
2572 * - with back
2573 * -with home
2574 * - On workspace, long press power and go back
2575 * - with back
2576 * - with home
2577 * - On all apps, long press power and go back
2578 * - with back
2579 * - with home
2580 * - On workspace, power off
2581 * - On all apps, power off
2582 * - Launch an app and turn off the screen while in that app
2583 * - Go back with home key
2584 * - Go back with back key TODO: make this not go to workspace
2585 * - From all apps
2586 * - From workspace
2587 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2588 * - From all apps
2589 * - From the center workspace
2590 * - From another workspace
2591 */
2592
2593 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002594 * Zoom the camera out from the workspace to reveal 'toView'.
2595 * Assumes that the view to show is anchored at either the very top or very bottom
2596 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002597 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002598 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002599 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002600 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002601 mStateAnimation.cancel();
2602 mStateAnimation = null;
2603 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002604 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002605
Winson Chungf0ea4d32011-06-06 14:27:16 -07002606 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2607 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2608 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002609 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002610 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002611 final int startDelay =
2612 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002613
Michael Jurkab3e22d92011-10-31 15:58:33 -07002614 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002615
Michael Jurkad74c9842011-07-10 12:44:21 -07002616 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002617 Animator workspaceAnim =
2618 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002619
2620 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002621 toView.setScaleX(scale);
2622 toView.setScaleY(scale);
2623 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2624 scaleAnim.
2625 scaleX(1f).scaleY(1f).
2626 setDuration(duration).
2627 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002628
Winson Chungf0ea4d32011-06-06 14:27:16 -07002629 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002630 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002631 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002632 .ofFloat(toView, "alpha", 0f, 1f)
2633 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002634 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002635 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2636 @Override
2637 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002638 if (animation == null) {
2639 throw new RuntimeException("animation is null");
2640 }
Winson Chung70442722012-02-10 15:43:22 -08002641 float t = (Float) animation.getAnimatedValue();
2642 dispatchOnLauncherTransitionStep(fromView, t);
2643 dispatchOnLauncherTransitionStep(toView, t);
2644 }
2645 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002646
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002647 // toView should appear right at the end of the workspace shrink
2648 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002649 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002650 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002651 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002652
2653 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002654 boolean animationCancelled = false;
2655
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002656 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002657 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002658 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002659 // Prepare the position
2660 toView.setTranslationX(0.0f);
2661 toView.setTranslationY(0.0f);
2662 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002663 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002664 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002665 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002666 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002667 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2668 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002669
Daniel Sandlere4f98912013-06-25 15:13:26 -04002670 if (mWorkspace != null
2671 && !springLoaded
2672 && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002673 // Hide the workspace scrollbar
2674 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002675 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002676 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002677 if (!animationCancelled) {
2678 updateWallpaperVisibility(false);
2679 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002680
2681 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002682 if (mSearchDropTargetBar != null) {
2683 mSearchDropTargetBar.hideSearchBar(false);
2684 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002685 }
2686
Adam Cohencff6af82011-09-13 14:51:53 -07002687 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002688 public void onAnimationCancel(Animator animation) {
2689 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002690 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002691 });
2692
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002693 if (workspaceAnim != null) {
2694 mStateAnimation.play(workspaceAnim);
2695 }
Michael Jurka1899a362011-11-03 13:50:45 -07002696
2697 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002698
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002699 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2700 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002701
2702 // If any of the objects being animated haven't been measured/laid out
2703 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002704 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002705 (mWorkspace.getMeasuredWidth() == 0) ||
2706 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002707 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002708 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002709
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002710 final AnimatorSet stateAnimation = mStateAnimation;
2711 final Runnable startAnimRunnable = new Runnable() {
2712 public void run() {
2713 // Check that mStateAnimation hasn't changed while
2714 // we waited for a layout/draw pass
2715 if (mStateAnimation != stateAnimation)
2716 return;
2717 setPivotsForZoom(toView, scale);
2718 dispatchOnLauncherTransitionStart(fromView, animated, false);
2719 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002720 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002721 }
2722 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002723 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002724 final ViewTreeObserver observer = toView.getViewTreeObserver();
2725 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2726 public void onGlobalLayout() {
2727 startAnimRunnable.run();
2728 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2729 }
2730 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002731 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002732 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002733 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002734 } else {
2735 toView.setTranslationX(0.0f);
2736 toView.setTranslationY(0.0f);
2737 toView.setScaleX(1.0f);
2738 toView.setScaleY(1.0f);
2739 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002740 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002741
Daniel Sandlere4f98912013-06-25 15:13:26 -04002742 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002743 // Hide the workspace scrollbar
2744 mWorkspace.hideScrollingIndicator(true);
2745 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002746
2747 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002748 if (mSearchDropTargetBar != null) {
2749 mSearchDropTargetBar.hideSearchBar(false);
2750 }
Michael Jurkaabded662011-03-04 12:06:57 -08002751 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002752 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002753 dispatchOnLauncherTransitionStart(fromView, animated, false);
2754 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002755 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002756 dispatchOnLauncherTransitionStart(toView, animated, false);
2757 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002758 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002759 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002760 }
2761
2762 /**
2763 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002764 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002765 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002766 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002767 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2768 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002769
Michael Jurkab3e22d92011-10-31 15:58:33 -07002770 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002771 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002772 mStateAnimation.cancel();
2773 mStateAnimation = null;
2774 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002775 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002776
Winson Chungf0ea4d32011-06-06 14:27:16 -07002777 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002778 final int fadeOutDuration =
2779 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002780 final float scaleFactor = (float)
2781 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2782 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002783 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002784 Animator workspaceAnim = null;
2785
2786 if (toState == State.WORKSPACE) {
2787 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2788 workspaceAnim = mWorkspace.getChangeStateAnimation(
2789 Workspace.State.NORMAL, animated, stagger);
2790 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2791 workspaceAnim = mWorkspace.getChangeStateAnimation(
2792 Workspace.State.SPRING_LOADED, animated);
2793 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002794
Michael Jurkab3e22d92011-10-31 15:58:33 -07002795 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002796 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002797 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002798 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002799 final LauncherViewPropertyAnimator scaleAnim =
2800 new LauncherViewPropertyAnimator(fromView);
2801 scaleAnim.
2802 scaleX(scaleFactor).scaleY(scaleFactor).
2803 setDuration(duration).
2804 setInterpolator(new Workspace.ZoomInInterpolator());
2805
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002806 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002807 .ofFloat(fromView, "alpha", 1f, 0f)
2808 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002809 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002810 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2811 @Override
2812 public void onAnimationUpdate(ValueAnimator animation) {
2813 float t = 1f - (Float) animation.getAnimatedValue();
2814 dispatchOnLauncherTransitionStep(fromView, t);
2815 dispatchOnLauncherTransitionStep(toView, t);
2816 }
2817 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002818
Michael Jurka2ecf9952012-06-18 12:52:28 -07002819 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002820
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002821 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2822 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002823 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002824
2825 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002826 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002827 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002828 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002830 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2831 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002832 if (mWorkspace != null) {
2833 mWorkspace.hideScrollingIndicator(false);
2834 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002835 if (onCompleteRunnable != null) {
2836 onCompleteRunnable.run();
2837 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002838 mAppsCustomizeContent.updateCurrentPageScroll();
2839 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002840 }
2841 });
2842
Winson Chungf0ea4d32011-06-06 14:27:16 -07002843 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002844 if (workspaceAnim != null) {
2845 mStateAnimation.play(workspaceAnim);
2846 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002847 dispatchOnLauncherTransitionStart(fromView, animated, true);
2848 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002849 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002850 } else {
2851 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002852 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002853 dispatchOnLauncherTransitionStart(fromView, animated, true);
2854 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002855 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002856 dispatchOnLauncherTransitionStart(toView, animated, true);
2857 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002858 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002859 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002860 }
2861
Michael Jurkae326f182011-11-21 14:05:46 -08002862 @Override
2863 public void onTrimMemory(int level) {
2864 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002865 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002866 mAppsCustomizeTabHost.onTrimMemory();
2867 }
2868 }
2869
Winson Chung18f41f82012-05-09 13:28:10 -07002870 @Override
2871 public void onWindowFocusChanged(boolean hasFocus) {
2872 if (!hasFocus) {
2873 // When another window occludes launcher (like the notification shade, or recents),
2874 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2875 updateWallpaperVisibility(true);
2876 } else {
2877 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002878 mWorkspace.postDelayed(new Runnable() {
2879 @Override
2880 public void run() {
2881 disableWallpaperIfInAllApps();
2882 }
2883 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002884 }
2885 }
2886
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002887 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002888 showWorkspace(animated, null);
2889 }
2890
2891 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002892 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002893 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002894 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002895 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002896
Winson Chungc7d2b602012-05-16 17:02:20 -07002897 // Show the search bar (only animate if we were showing the drop target bar in spring
2898 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002899 if (mSearchDropTargetBar != null) {
2900 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2901 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002902
Michael Jurkab737ee62011-11-15 15:57:22 -08002903 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002904 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002905
2906 // Set focus to the AppsCustomize button
2907 if (mAllAppsButton != null) {
2908 mAllAppsButton.requestFocus();
2909 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002910 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002911
Michael Jurkab737ee62011-11-15 15:57:22 -08002912 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002913
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002914 // Change the state *after* we've called all the transition code
2915 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002916
Winson Chung5fb63472011-02-02 17:03:37 -08002917 // Resume the auto-advance of widgets
2918 mUserPresent = true;
2919 updateRunning();
2920
alanv1d4fde62012-10-17 13:15:47 -07002921 // Send an accessibility event to announce the context change
2922 getWindow().getDecorView()
2923 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002924
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002925 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002926 }
2927
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002928 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002929 }
2930
Michael Jurkab3e22d92011-10-31 15:58:33 -07002931 void showAllApps(boolean animated) {
2932 if (mState != State.WORKSPACE) return;
2933
2934 showAppsCustomizeHelper(animated, false);
2935 mAppsCustomizeTabHost.requestFocus();
2936
Michael Jurkab3e22d92011-10-31 15:58:33 -07002937 // Change the state *after* we've called all the transition code
2938 mState = State.APPS_CUSTOMIZE;
2939
2940 // Pause the auto-advance of widgets until we are out of AllApps
2941 mUserPresent = false;
2942 updateRunning();
2943 closeFolder();
2944
2945 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002946 getWindow().getDecorView()
2947 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002948 }
2949
Adam Cohen7777d962011-08-18 18:58:38 -07002950 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002951 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002952 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002953 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002954 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002955 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002956 }
Adam Cohen7777d962011-08-18 18:58:38 -07002957
Adam Cohened66b2b2012-01-23 17:28:51 -08002958 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2959 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002960 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2961
Winson Chunge7a03942011-08-05 15:05:12 -07002962 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002963 @Override
2964 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002965 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002966 // Before we show workspace, hide all apps again because
2967 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2968 // clean up our state transition functions
2969 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002970 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002971 } else {
2972 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002973 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002974 }
2975 }, (extendedDelay ?
2976 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2977 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2978 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002979
Michael Jurkad3ef3062010-11-23 16:23:58 -08002980 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002981 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002982 final boolean animated = true;
2983 final boolean springLoaded = true;
2984 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002985 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002986 }
2987 // Otherwise, we are not in spring loaded mode, so don't do anything.
2988 }
2989
Michael Jurkab737ee62011-11-15 15:57:22 -08002990 void hideDockDivider() {
2991 if (mQsbDivider != null && mDockDivider != null) {
2992 mQsbDivider.setVisibility(View.INVISIBLE);
2993 mDockDivider.setVisibility(View.INVISIBLE);
2994 }
2995 }
2996
2997 void showDockDivider(boolean animated) {
2998 if (mQsbDivider != null && mDockDivider != null) {
2999 mQsbDivider.setVisibility(View.VISIBLE);
3000 mDockDivider.setVisibility(View.VISIBLE);
3001 if (mDividerAnimator != null) {
3002 mDividerAnimator.cancel();
3003 mQsbDivider.setAlpha(1f);
3004 mDockDivider.setAlpha(1f);
3005 mDividerAnimator = null;
3006 }
3007 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003008 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3009 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3010 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003011 int duration = 0;
3012 if (mSearchDropTargetBar != null) {
3013 duration = mSearchDropTargetBar.getTransitionInDuration();
3014 }
3015 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003016 mDividerAnimator.start();
3017 }
3018 }
3019 }
3020
Michael Jurkab3e22d92011-10-31 15:58:33 -07003021 void lockAllApps() {
3022 // TODO
3023 }
3024
3025 void unlockAllApps() {
3026 // TODO
3027 }
3028
Winson Chungf0ea4d32011-06-06 14:27:16 -07003029 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003030 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003031 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003032 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003033 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003034 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003035 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003036 int duration = 0;
3037 if (mSearchDropTargetBar != null) {
3038 duration = mSearchDropTargetBar.getTransitionInDuration();
3039 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003040 mHotseat.animate().alpha(1f).setDuration(duration);
3041 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003042 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003043 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003044 }
3045 }
3046 }
3047
3048 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003049 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003050 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003051 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003052 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003053 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003054 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003055 int duration = 0;
3056 if (mSearchDropTargetBar != null) {
3057 duration = mSearchDropTargetBar.getTransitionOutDuration();
3058 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003059 mHotseat.animate().alpha(0f).setDuration(duration);
3060 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003061 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003062 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003063 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003064 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003065 }
3066
Patrick Dubroy5f445422011-02-18 14:35:21 -08003067 /**
3068 * Add an item from all apps or customize onto the given workspace screen.
3069 * If layout is null, add to the current screen.
3070 */
3071 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003072 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003073 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003074 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003075 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003076
Winson Chungdff8ebb2011-09-08 17:25:31 -07003077 /** Maps the current orientation to an index for referencing orientation correct global icons */
3078 private int getCurrentOrientationIndexForGlobalIcons() {
3079 // default - 0, landscape - 1
3080 switch (getResources().getConfiguration().orientation) {
3081 case Configuration.ORIENTATION_LANDSCAPE:
3082 return 1;
3083 default:
3084 return 0;
3085 }
3086 }
3087
Michael Jurkaae65ee32012-04-30 11:45:54 -07003088 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003089 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003090 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003091 // Look for the toolbar icon specified in the activity meta-data
3092 Bundle metaData = packageManager.getActivityInfo(
3093 activityName, PackageManager.GET_META_DATA).metaData;
3094 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003095 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003096 if (iconResId != 0) {
3097 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003098 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003099 }
3100 }
3101 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003102 // This can happen if the activity defines an invalid drawable
3103 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3104 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003105 } catch (Resources.NotFoundException nfe) {
3106 // This can happen if the activity defines an invalid drawable
3107 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3108 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003109 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003110 return null;
3111 }
3112
3113 // if successful in getting icon, return it; otherwise, set button to use default drawable
3114 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003115 int buttonId, ComponentName activityName, int fallbackDrawableId,
3116 String toolbarResourceName) {
3117 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003118 Resources r = getResources();
3119 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3120 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003121
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003122 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003123 // If we were unable to find the icon via the meta-data, use a generic one
3124 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003125 toolbarIcon = r.getDrawable(fallbackDrawableId);
3126 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003127 if (button != null) {
3128 button.setCompoundDrawables(toolbarIcon, null, null, null);
3129 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003130 return null;
3131 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003132 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003133 if (button != null) {
3134 button.setCompoundDrawables(toolbarIcon, null, null, null);
3135 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003136 return toolbarIcon.getConstantState();
3137 }
3138 }
3139
3140 // if successful in getting icon, return it; otherwise, set button to use default drawable
3141 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003142 int buttonId, ComponentName activityName, int fallbackDrawableId,
3143 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003144 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003145 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003146
Michael Jurka19e0fc52011-07-22 18:00:21 -07003147 if (button != null) {
3148 // If we were unable to find the icon via the meta-data, use a
3149 // generic one
3150 if (toolbarIcon == null) {
3151 button.setImageResource(fallbackDrawableId);
3152 } else {
3153 button.setImageDrawable(toolbarIcon);
3154 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003155 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003156
3157 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3158
Michael Jurka0423dcf2010-10-05 14:56:18 -07003159 }
3160
Winson Chung1b884092012-06-08 17:13:02 -07003161 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003162 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003163 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003164 }
3165
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003166 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003167 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003168 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003169 }
3170
Winson Chungbb185bd2011-11-21 12:31:42 -08003171 private void invalidatePressedFocusedStates(View container, View button) {
3172 if (container instanceof HolographicLinearLayout) {
3173 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3174 layout.invalidatePressedFocusedStates();
3175 } else if (button instanceof HolographicImageView) {
3176 HolographicImageView view = (HolographicImageView) button;
3177 view.invalidatePressedFocusedStates();
3178 }
3179 }
3180
Winson Chungc51db6a2011-10-05 11:44:49 -07003181 private boolean updateGlobalSearchIcon() {
3182 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003183 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003184 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003185 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003186
Winson Chung1cad91e2011-05-25 17:41:01 -07003187 final SearchManager searchManager =
3188 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3189 ComponentName activityName = searchManager.getGlobalSearchActivity();
3190 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003191 int coi = getCurrentOrientationIndexForGlobalIcons();
3192 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003193 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3194 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3195 if (sGlobalSearchIcon[coi] == null) {
3196 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3197 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3198 TOOLBAR_ICON_METADATA_NAME);
3199 }
3200
Winson Chungc51db6a2011-10-05 11:44:49 -07003201 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3202 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003203 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003204 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003205 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003206 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003207 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3208 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3209 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003210 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003211 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003212 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003213 }
3214 }
3215
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003216 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003217 final View searchButtonContainer = findViewById(R.id.search_button_container);
3218 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003219 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003220 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003221 }
3222
Winson Chungc51db6a2011-10-05 11:44:49 -07003223 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003224 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003225 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003226
Winson Chungc51db6a2011-10-05 11:44:49 -07003227 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003228 final SearchManager searchManager =
3229 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3230 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3231
3232 ComponentName activityName = null;
3233 if (globalSearchActivity != null) {
3234 // Check if the global search activity handles voice search
3235 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3236 intent.setPackage(globalSearchActivity.getPackageName());
3237 activityName = intent.resolveActivity(getPackageManager());
3238 }
3239
3240 if (activityName == null) {
3241 // Fallback: check if an activity other than the global search activity
3242 // resolves this
3243 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3244 activityName = intent.resolveActivity(getPackageManager());
3245 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003246 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003247 int coi = getCurrentOrientationIndexForGlobalIcons();
3248 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003249 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3250 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3251 if (sVoiceSearchIcon[coi] == null) {
3252 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3253 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3254 TOOLBAR_ICON_METADATA_NAME);
3255 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003256 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003257 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003258 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003259 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003260 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003261 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003262 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003263 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003264 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003265 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003266 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003267 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003268
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003269 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003270 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3271 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003272 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003273 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003274 }
3275
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003276 public void setVoiceButtonProxyVisible(boolean visible) {
3277 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3278 if (voiceButtonProxy != null) {
3279 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3280 }
3281 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003282 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003283 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003284 */
3285 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003286 final View marketButton = findViewById(R.id.market_button);
3287 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3288 // Find the app market activity by resolving an intent.
3289 // (If multiple app markets are installed, it will return the ResolverActivity.)
3290 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003291 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003292 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003293 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003294 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003295 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3296 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003297 marketButton.setVisibility(View.VISIBLE);
3298 } else {
3299 // We should hide and disable the view so that we don't try and restore the visibility
3300 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3301 marketButton.setVisibility(View.GONE);
3302 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003303 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003304 }
3305
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003306 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003307 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3308 Resources r = getResources();
3309 Drawable marketIconDrawable = d.newDrawable(r);
3310 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3311 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3312 marketIconDrawable.setBounds(0, 0, w, h);
3313
3314 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003315 }
3316
Michael Jurkad7c28052012-04-27 15:43:36 -07003317 @Override
3318 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003319 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003320 final List<CharSequence> text = event.getText();
3321 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003322 // Populate event with a fake title based on the current state.
3323 if (mState == State.APPS_CUSTOMIZE) {
3324 text.add(getString(R.string.all_apps_button_label));
3325 } else {
3326 text.add(getString(R.string.all_apps_home_button_label));
3327 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003328 return result;
3329 }
3330
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003331 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003332 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003333 */
3334 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3335 @Override
3336 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003337 closeSystemDialogs();
3338 }
3339 }
3340
3341 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003342 * Receives notifications whenever the appwidgets are reset.
3343 */
3344 private class AppWidgetResetObserver extends ContentObserver {
3345 public AppWidgetResetObserver() {
3346 super(new Handler());
3347 }
3348
3349 @Override
3350 public void onChange(boolean selfChange) {
3351 onAppWidgetReset();
3352 }
3353 }
3354
3355 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003356 * If the activity is currently paused, signal that we need to run the passed Runnable
3357 * in onResume.
3358 *
3359 * This needs to be called from incoming places where resources might have been loaded
3360 * while we are paused. That is becaues the Configuration might be wrong
3361 * when we're not running, and if it comes back to what it was when we
3362 * were paused, we are not restarted.
3363 *
3364 * Implementation of the method from LauncherModel.Callbacks.
3365 *
3366 * @return true if we are currently paused. The caller might be able to
3367 * skip some work in that case since we will come back again.
3368 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003369 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003370 if (mPaused) {
3371 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003372 if (deletePreviousRunnables) {
3373 while (mOnResumeCallbacks.remove(run)) {
3374 }
3375 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003376 mOnResumeCallbacks.add(run);
3377 return true;
3378 } else {
3379 return false;
3380 }
3381 }
3382
Michael Jurkac402cd92013-05-20 15:49:32 +02003383 private boolean waitUntilResume(Runnable run) {
3384 return waitUntilResume(run, false);
3385 }
3386
Michael Jurka7607c2f2013-04-03 14:33:19 -07003387 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003388 * If the activity is currently paused, signal that we need to re-run the loader
3389 * in onResume.
3390 *
3391 * This needs to be called from incoming places where resources might have been loaded
3392 * while we are paused. That is becaues the Configuration might be wrong
3393 * when we're not running, and if it comes back to what it was when we
3394 * were paused, we are not restarted.
3395 *
3396 * Implementation of the method from LauncherModel.Callbacks.
3397 *
3398 * @return true if we are currently paused. The caller might be able to
3399 * skip some work in that case since we will come back again.
3400 */
3401 public boolean setLoadOnResume() {
3402 if (mPaused) {
3403 Log.i(TAG, "setLoadOnResume");
3404 mOnResumeNeedsLoad = true;
3405 return true;
3406 } else {
3407 return false;
3408 }
3409 }
3410
3411 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003413 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003415 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003416 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003417 } else {
3418 return SCREEN_COUNT / 2;
3419 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003421
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 /**
3423 * Refreshes the shortcuts shown on the workspace.
3424 *
3425 * Implementation of the method from LauncherModel.Callbacks.
3426 */
3427 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003428 // If we're starting binding all over again, clear any bind calls we'd postponed in
3429 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3430 // from scratch again
3431 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003432
Michael Jurka7607c2f2013-04-03 14:33:19 -07003433 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003434 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003435 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003436 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 int count = workspace.getChildCount();
3438 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003439 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003440 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3441 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003443 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003444 if (mHotseat != null) {
3445 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003446 }
3447 }
3448
Adam Cohendcd297f2013-06-18 13:13:40 -07003449 @Override
3450 public void bindScreens(ArrayList<Long> orderedScreenIds) {
3451 int count = orderedScreenIds.size();
3452 for (int i = 0; i < count; i++) {
3453 mWorkspace.insertNewWorkspaceScreenOnBind(orderedScreenIds.get(i));
3454 }
3455 mWorkspace.addExtraEmptyScreen();
3456 }
3457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003458 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003459 * Bind the items start-end from the list.
3460 *
3461 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003462 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003463 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3464 if (waitUntilResume(new Runnable() {
3465 public void run() {
3466 bindItems(shortcuts, start, end);
3467 }
3468 })) {
3469 return;
3470 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003471
Winson Chungf0c6ae02012-03-21 16:10:31 -07003472 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3473 Set<String> newApps = new HashSet<String>();
3474 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3475
3476 Workspace workspace = mWorkspace;
3477 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003478 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003479
3480 // Short circuit if we are loading dock items for a configuration which has no dock
3481 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3482 mHotseat == null) {
3483 continue;
3484 }
3485
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486 switch (item.itemType) {
3487 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3488 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003489 ShortcutInfo info = (ShortcutInfo) item;
3490 String uri = info.intent.toUri(0).toString();
3491 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003492
3493 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3494 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003495 boolean animateIconUp = false;
3496 synchronized (newApps) {
3497 if (newApps.contains(uri)) {
3498 animateIconUp = newApps.remove(uri);
3499 }
3500 }
3501 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003502 // Prepare the view to be animated up
3503 shortcut.setAlpha(0f);
3504 shortcut.setScaleX(0f);
3505 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003506 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003507 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3508 mNewShortcutAnimateViews.add(shortcut);
3509 }
3510 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003511 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003512 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003513 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003514 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003515 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003516 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3517 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003518 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003519 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003520 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003521
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003523 }
3524
Joe Onorato9c1289c2009-08-17 11:03:03 -04003525 /**
3526 * Implementation of the method from LauncherModel.Callbacks.
3527 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003528 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3529 if (waitUntilResume(new Runnable() {
3530 public void run() {
3531 bindFolders(folders);
3532 }
3533 })) {
3534 return;
3535 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003536 sFolders.clear();
3537 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003538 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539
3540 /**
3541 * Add the views for a widget to the workspace.
3542 *
3543 * Implementation of the method from LauncherModel.Callbacks.
3544 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003545 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3546 if (waitUntilResume(new Runnable() {
3547 public void run() {
3548 bindAppWidget(item);
3549 }
3550 })) {
3551 return;
3552 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003553
Daniel Sandler843e8602010-06-07 14:59:01 -04003554 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3555 if (DEBUG_WIDGETS) {
3556 Log.d(TAG, "bindAppWidget: " + item);
3557 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003558 final Workspace workspace = mWorkspace;
3559
3560 final int appWidgetId = item.appWidgetId;
3561 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003562 if (DEBUG_WIDGETS) {
3563 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3564 }
3565
Joe Onorato9c1289c2009-08-17 11:03:03 -04003566 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3567
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003569 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570
Adam Cohendcd297f2013-06-18 13:13:40 -07003571 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003573 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3574
Joe Onorato9c1289c2009-08-17 11:03:03 -04003575 workspace.requestLayout();
3576
Daniel Sandler843e8602010-06-07 14:59:01 -04003577 if (DEBUG_WIDGETS) {
3578 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3579 + (SystemClock.uptimeMillis()-start) + "ms");
3580 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003581 }
3582
Adam Cohen1462de32012-07-24 22:34:36 -07003583 public void onPageBoundSynchronously(int page) {
3584 mSynchronouslyBoundPages.add(page);
3585 }
3586
Joe Onorato9c1289c2009-08-17 11:03:03 -04003587 /**
3588 * Callback saying that there aren't any more items to bind.
3589 *
3590 * Implementation of the method from LauncherModel.Callbacks.
3591 */
Adam Cohene25af792013-06-06 23:08:25 -07003592 public void finishBindingItems(final boolean upgradePath) {
Winson Chungc9168342013-06-26 14:54:55 -07003593
Michael Jurka7607c2f2013-04-03 14:33:19 -07003594 if (waitUntilResume(new Runnable() {
3595 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003596 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003597 }
3598 })) {
3599 return;
3600 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 if (mSavedState != null) {
3602 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003603 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 mSavedState = null;
3606 }
3607
Adam Cohen1462de32012-07-24 22:34:36 -07003608 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003610 // If we received the result of any pending adds while the loader was running (e.g. the
3611 // widget configuration forced an orientation change), process them now.
3612 for (int i = 0; i < sPendingAddList.size(); i++) {
3613 completeAdd(sPendingAddList.get(i));
3614 }
3615 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616
Winson Chungc51db6a2011-10-05 11:44:49 -07003617 // Update the market app icon as necessary (the other icons will be managed in response to
3618 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003619 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003620
Winson Chungf0c6ae02012-03-21 16:10:31 -07003621 // Animate up any icons as necessary
3622 if (mVisible || mWorkspaceLoading) {
3623 Runnable newAppsRunnable = new Runnable() {
3624 @Override
3625 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003626 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003627 }
3628 };
Winson Chunga2413752012-04-03 14:22:34 -07003629
Adam Cohendcd297f2013-06-18 13:13:40 -07003630 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3631 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003632 if (canRunNewAppsAnimation()) {
3633 // If the user has not interacted recently, then either snap to the new page to show
3634 // the new-apps animation or just run them if they are to appear on the current page
3635 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003636 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003637 } else {
3638 runNewAppsAnimation(false);
3639 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003640 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003641 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003642 // are on another page (or just normally if they are added to the current page)
3643 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003644 }
3645 }
3646
3647 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003648 if (upgradePath) {
3649 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003650 mWorkspace.stripDuplicateApps();
3651 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003652 }
Adam Cohen99894d92013-06-14 11:22:59 -07003653
Adam Cohen66a01fd2013-06-11 12:48:00 -07003654 mWorkspace.post(new Runnable() {
3655 @Override
3656 public void run() {
3657 onFinishBindingItems();
3658 }
3659 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003660 }
3661
Winson Chunga2413752012-04-03 14:22:34 -07003662 private boolean canRunNewAppsAnimation() {
3663 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3664 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3665 }
3666
Winson Chungc9168342013-06-26 14:54:55 -07003667 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3668 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3669 PropertyValuesHolder.ofFloat("alpha", 1f),
3670 PropertyValuesHolder.ofFloat("scaleX", 1f),
3671 PropertyValuesHolder.ofFloat("scaleY", 1f));
3672 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3673 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3674 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3675 return bounceAnim;
3676 }
3677
Winson Chungf0c6ae02012-03-21 16:10:31 -07003678 /**
3679 * Runs a new animation that scales up icons that were added while Launcher was in the
3680 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003681 *
3682 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003683 */
Winson Chunga2413752012-04-03 14:22:34 -07003684 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003685 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003686 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003687
3688 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003689 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3690 @Override
3691 public int compare(View a, View b) {
3692 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3693 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3694 int cellCountX = LauncherModel.getCellCountX();
3695 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3696 }
3697 });
Winson Chunga2413752012-04-03 14:22:34 -07003698
3699 // Animate each of the views in place (or show them immediately if requested)
3700 if (immediate) {
3701 for (View v : mNewShortcutAnimateViews) {
3702 v.setAlpha(1f);
3703 v.setScaleX(1f);
3704 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003705 }
Winson Chunga2413752012-04-03 14:22:34 -07003706 } else {
3707 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3708 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003709 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003710 }
3711 anim.playTogether(bounceAnims);
3712 anim.addListener(new AnimatorListenerAdapter() {
3713 @Override
3714 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003715 if (mWorkspace != null) {
3716 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3717 }
Winson Chunga2413752012-04-03 14:22:34 -07003718 }
3719 });
3720 anim.start();
3721 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003722
3723 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003724 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003725 mNewShortcutAnimateViews.clear();
3726 new Thread("clearNewAppsThread") {
3727 public void run() {
3728 mSharedPrefs.edit()
3729 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3730 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3731 .commit();
3732 }
3733 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003734 }
3735
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003736 @Override
3737 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003738 boolean searchVisible = updateGlobalSearchIcon();
3739 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003740 if (mSearchDropTargetBar != null) {
3741 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3742 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003743 }
3744
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003745 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003746 * Add the icons for all apps.
3747 *
3748 * Implementation of the method from LauncherModel.Callbacks.
3749 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003750 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003751 Runnable setAllAppsRunnable = new Runnable() {
3752 public void run() {
3753 if (mAppsCustomizeContent != null) {
3754 mAppsCustomizeContent.setApps(apps);
Adam Cohene25af792013-06-06 23:08:25 -07003755
3756 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3757 getHotseat().addAllAppsFolder(mIconCache, apps,
3758 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3759 mIntentsOnWorkspaceFromUpgradePath = null;
3760 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07003761 }
3762 }
3763 };
3764
Michael Jurkac57b7a82011-08-09 22:02:20 -07003765 // Remove the progress bar entirely; we could also make it GONE
3766 // but better to remove it since we know it's not going to be used
3767 View progressBar = mAppsCustomizeTabHost.
3768 findViewById(R.id.apps_customize_progress_bar);
3769 if (progressBar != null) {
3770 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003771
3772 // We just post the call to setApps so the user sees the progress bar
3773 // disappear-- otherwise, it just looks like the progress bar froze
3774 // which doesn't look great
3775 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3776 } else {
3777 // If we did not initialize the spinner in onCreate, then we can directly set the
3778 // list of applications without waiting for any progress bars views to be hidden.
3779 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003780 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003781 }
3782
3783 /**
3784 * A package was installed.
3785 *
3786 * Implementation of the method from LauncherModel.Callbacks.
3787 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003788 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3789 if (waitUntilResume(new Runnable() {
3790 public void run() {
3791 bindAppsAdded(apps);
3792 }
3793 })) {
3794 return;
3795 }
3796
Winson Chungc9168342013-06-26 14:54:55 -07003797 final Launcher launcher = this;
3798 final Context context = this;
3799 final ArrayList<ApplicationInfo> appsCopy = new ArrayList<ApplicationInfo>();
3800 appsCopy.addAll(apps);
3801
3802 // Process newly added apps
3803 mWorkspace.post(new Runnable() {
3804 @Override
3805 public void run() {
3806 // Process newly added apps
3807 LauncherModel model = getModel();
3808 Iterator<ApplicationInfo> iter = appsCopy.iterator();
3809 ArrayList<View> animatedShortcuts = new ArrayList<View>();
3810
3811 while (iter.hasNext()) {
3812 ApplicationInfo a = iter.next();
3813 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
3814 a.title.toString(), a.intent);
3815 if (coords == null) {
3816 // If we can't find a valid position, then just add a new screen.
3817 // This takes time so we need to re-queue the add until the new
3818 // page is added.
Daniel Sandlere4f98912013-06-25 15:13:26 -04003819 long screenId = LauncherAppState.getInstance().getLauncherProvider()
3820 .generateNewScreenId();
Winson Chungc9168342013-06-26 14:54:55 -07003821 mWorkspace.insertNewWorkspaceScreen(screenId, false);
3822 model.updateWorkspaceScreenOrder(launcher, mWorkspace.getScreenOrder(),
3823 new Runnable() {
3824 @Override
3825 public void run() {
3826 bindAppsAdded(appsCopy);
3827 }
3828 });
3829 return;
3830 } else {
3831 final ShortcutInfo shortcutInfo = a.makeShortcut();
3832 final View shortcut = createShortcut(shortcutInfo);
3833 // Add the view to the screen
3834 mWorkspace.addInScreenFromBind(shortcut,
3835 LauncherSettings.Favorites.CONTAINER_DESKTOP,
3836 coords.first, coords.second[0], coords.second[1], 1, 1);
3837 // Add the shortcut to the db
3838 model.addItemToDatabase(context, shortcutInfo,
3839 LauncherSettings.Favorites.CONTAINER_DESKTOP,
3840 coords.first, coords.second[0], coords.second[1], false);
3841 // Animate the shortcut
3842 animatedShortcuts.add(shortcut);
3843 }
3844 iter.remove();
3845 }
3846
3847 // Animate all the applications up
3848 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3849 Collection<Animator> bounceAnims = new ArrayList<Animator>();
3850 for (int i = 0; i < animatedShortcuts.size(); ++i) {
3851 View shortcut = animatedShortcuts.get(i);
3852 shortcut.setAlpha(0f);
3853 shortcut.setScaleX(0f);
3854 shortcut.setScaleY(0f);
3855 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3856 }
3857 anim.playTogether(bounceAnims);
3858 anim.start();
3859 }
3860 });
Winson Chungf0ea4d32011-06-06 14:27:16 -07003861
Winson Chung785d2eb2011-04-14 16:08:02 -07003862 if (mAppsCustomizeContent != null) {
3863 mAppsCustomizeContent.addApps(apps);
3864 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003865 }
3866
3867 /**
3868 * A package was updated.
3869 *
3870 * Implementation of the method from LauncherModel.Callbacks.
3871 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003872 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3873 if (waitUntilResume(new Runnable() {
3874 public void run() {
3875 bindAppsUpdated(apps);
3876 }
3877 })) {
3878 return;
3879 }
3880
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003881 if (mWorkspace != null) {
3882 mWorkspace.updateShortcuts(apps);
3883 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003884
Winson Chung785d2eb2011-04-14 16:08:02 -07003885 if (mAppsCustomizeContent != null) {
3886 mAppsCustomizeContent.updateApps(apps);
3887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003888 }
3889
3890 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003891 * A package was uninstalled. We take both the super set of packageNames
3892 * in addition to specific applications to remove, the reason being that
3893 * this can be called when a package is updated as well. In that scenario,
3894 * we only remove specific components from the workspace, where as
3895 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896 *
3897 * Implementation of the method from LauncherModel.Callbacks.
3898 */
Winson Chung83892cc2013-05-01 16:53:33 -07003899 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3900 final ArrayList<ApplicationInfo> appInfos,
3901 final boolean matchPackageNamesOnly) {
3902 if (waitUntilResume(new Runnable() {
3903 public void run() {
3904 bindComponentsRemoved(packageNames, appInfos, matchPackageNamesOnly);
3905 }
3906 })) {
3907 return;
3908 }
3909
3910 if (matchPackageNamesOnly) {
3911 mWorkspace.removeItemsByPackageName(packageNames);
3912 } else {
3913 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003914 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003915
Winson Chung785d2eb2011-04-14 16:08:02 -07003916 if (mAppsCustomizeContent != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07003917 mAppsCustomizeContent.removeApps(appInfos);
Winson Chung785d2eb2011-04-14 16:08:02 -07003918 }
Winson Chunga1820962011-10-03 16:31:06 -07003919
3920 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003921 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922 }
Joe Onoratobe386092009-11-17 17:32:16 -08003923
3924 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003925 * A number of packages were updated.
3926 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003927
3928 private ArrayList<Object> mWidgetsAndShortcuts;
3929 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003930 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003931 bindPackagesUpdated(mWidgetsAndShortcuts);
3932 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003933 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003934 };
3935
3936 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3937 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3938 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003939 return;
3940 }
3941
Winson Chung785d2eb2011-04-14 16:08:02 -07003942 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003943 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003944 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003945 }
3946
Winson Chung400438b2011-01-16 17:53:48 -08003947 private int mapConfigurationOriActivityInfoOri(int configOri) {
3948 final Display d = getWindowManager().getDefaultDisplay();
3949 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3950 switch (d.getRotation()) {
3951 case Surface.ROTATION_0:
3952 case Surface.ROTATION_180:
3953 // We are currently in the same basic orientation as the natural orientation
3954 naturalOri = configOri;
3955 break;
3956 case Surface.ROTATION_90:
3957 case Surface.ROTATION_270:
3958 // We are currently in the other basic orientation to the natural orientation
3959 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3960 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3961 break;
3962 }
3963
3964 int[] oriMap = {
3965 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3966 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3967 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3968 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3969 };
3970 // Since the map starts at portrait, we need to offset if this device's natural orientation
3971 // is landscape.
3972 int indexOffset = 0;
3973 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3974 indexOffset = 1;
3975 }
3976 return oriMap[(d.getRotation() + indexOffset) % 4];
3977 }
Adam Cohen446e9402011-09-15 18:21:21 -07003978
Winson Chung4b919f82012-05-01 10:44:08 -07003979 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003980 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003981 getResources().getBoolean(R.bool.allow_rotation);
3982 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003983 }
Winson Chung4b919f82012-05-01 10:44:08 -07003984 public void lockScreenOrientation() {
3985 if (isRotationEnabled()) {
3986 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3987 .getConfiguration().orientation));
3988 }
3989 }
3990 public void unlockScreenOrientation(boolean immediate) {
3991 if (isRotationEnabled()) {
3992 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003993 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003994 } else {
3995 mHandler.postDelayed(new Runnable() {
3996 public void run() {
3997 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3998 }
3999 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004000 }
Winson Chung4b919f82012-05-01 10:44:08 -07004001 }
Winson Chung400438b2011-01-16 17:53:48 -08004002 }
4003
Winson Chung82f55532011-08-09 14:14:23 -07004004 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004005 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004006 if (DISABLE_CLINGS) {
4007 return false;
4008 }
4009
Brett Chabot2a9e2282011-08-23 15:03:13 -07004010 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004011 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004012
Michael Jurkae233a8b2013-03-19 13:49:20 +01004013 // Restricted secondary users (child mode) will potentially have very few apps
4014 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004015// boolean supportsLimitedUsers =
4016// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4017// Account[] accounts = AccountManager.get(this).getAccounts();
4018// if (supportsLimitedUsers && accounts.length == 0) {
4019// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4020// Bundle restrictions = um.getUserRestrictions();
4021// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4022// return false;
4023// }
4024// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004025 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004026 }
Michael Jurka22143132012-10-04 17:42:38 +02004027
Winson Chung7d7541e2011-09-16 20:14:36 -07004028 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004029 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004030 if (cling != null) {
4031 cling.init(this, positionData);
4032 cling.setVisibility(View.VISIBLE);
4033 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4034 if (animate) {
4035 cling.buildLayer();
4036 cling.setAlpha(0f);
4037 cling.animate()
4038 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004039 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004040 .setDuration(SHOW_CLING_DURATION)
4041 .setStartDelay(delay)
4042 .start();
4043 } else {
4044 cling.setAlpha(1f);
4045 }
Michael Jurka22143132012-10-04 17:42:38 +02004046 cling.setFocusableInTouchMode(true);
4047 cling.post(new Runnable() {
4048 public void run() {
4049 cling.setFocusable(true);
4050 cling.requestFocus();
4051 }
4052 });
4053 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4054 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004055 }
4056 return cling;
4057 }
Michael Jurka22143132012-10-04 17:42:38 +02004058
Winson Chung7d7541e2011-09-16 20:14:36 -07004059 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004060 // To catch cases where siblings of top-level views are made invisible, just check whether
4061 // the cling is directly set to GONE before dismissing it.
4062 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004063 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004064 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004065 anim.addListener(new AnimatorListenerAdapter() {
4066 public void onAnimationEnd(Animator animation) {
4067 cling.setVisibility(View.GONE);
4068 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004069 // We should update the shared preferences on a background thread
4070 new Thread("dismissClingThread") {
4071 public void run() {
4072 SharedPreferences.Editor editor = mSharedPrefs.edit();
4073 editor.putBoolean(flag, true);
4074 editor.commit();
4075 }
4076 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004077 };
4078 });
4079 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004080 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004081 }
4082 }
Michael Jurka22143132012-10-04 17:42:38 +02004083
Winson Chung9d9d74f2011-09-19 11:49:12 -07004084 private void removeCling(int id) {
4085 final View cling = findViewById(id);
4086 if (cling != null) {
4087 final ViewGroup parent = (ViewGroup) cling.getParent();
4088 parent.post(new Runnable() {
4089 @Override
4090 public void run() {
4091 parent.removeView(cling);
4092 }
4093 });
Michael Jurka22143132012-10-04 17:42:38 +02004094 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004095 }
4096 }
Michael Jurka974c3862012-05-22 22:00:31 -07004097
4098 private boolean skipCustomClingIfNoAccounts() {
4099 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4100 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4101 if (customCling) {
4102 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004103 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004104 Account[] accounts = am.getAccountsByType("com.google");
4105 return accounts.length == 0;
4106 }
4107 return false;
4108 }
4109
Winson Chung7d7541e2011-09-16 20:14:36 -07004110 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004111 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004112 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004113 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4114 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004115 // If we're not using the default workspace layout, replace workspace cling
4116 // with a custom workspace cling (usually specified in an overlay)
4117 // For now, only do this on tablets
4118 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004119 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004120 // Use a custom cling
4121 View cling = findViewById(R.id.workspace_cling);
4122 ViewGroup clingParent = (ViewGroup) cling.getParent();
4123 int clingIndex = clingParent.indexOfChild(cling);
4124 clingParent.removeViewAt(clingIndex);
4125 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4126 clingParent.addView(customCling, clingIndex);
4127 customCling.setId(R.id.workspace_cling);
4128 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004129 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004130 } else {
4131 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004132 }
4133 }
4134 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004135 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004136 if (isClingsEnabled() &&
4137 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004138 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004139 } else {
4140 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004141 }
4142 }
4143 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004144 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004145 if (isClingsEnabled() &&
4146 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4147 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004148 } else {
4149 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004150 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004151 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004152 }
4153 public boolean isFolderClingVisible() {
4154 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004155 if (cling != null) {
4156 return cling.getVisibility() == View.VISIBLE;
4157 }
4158 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004159 }
Winson Chung82f55532011-08-09 14:14:23 -07004160 public void dismissWorkspaceCling(View v) {
4161 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004162 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004163 }
4164 public void dismissAllAppsCling(View v) {
4165 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004166 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4167 }
4168 public void dismissFolderCling(View v) {
4169 Cling cling = (Cling) findViewById(R.id.folder_cling);
4170 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004171 }
4172
Winson Chung80baf5a2010-08-09 16:03:15 -07004173 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004174 * Prints out out state for debugging.
4175 */
4176 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004177 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004178 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004179 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4180 Log.d(TAG, "mRestoring=" + mRestoring);
4181 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4182 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004183 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004184 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004185
Winson Chung785d2eb2011-04-14 16:08:02 -07004186 if (mAppsCustomizeContent != null) {
4187 mAppsCustomizeContent.dumpState();
4188 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004189 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004190 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004191
4192 @Override
4193 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4194 super.dump(prefix, fd, writer, args);
4195 writer.println(" ");
4196 writer.println("Debug logs: ");
4197 for (int i = 0; i < sDumpLogs.size(); i++) {
4198 writer.println(" " + sDumpLogs.get(i));
4199 }
4200 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004201
4202 public static void dumpDebugLogsToConsole() {
4203 Log.d(TAG, "");
4204 Log.d(TAG, "*********************");
4205 Log.d(TAG, "Launcher debug logs: ");
4206 for (int i = 0; i < sDumpLogs.size(); i++) {
4207 Log.d(TAG, " " + sDumpLogs.get(i));
4208 }
4209 Log.d(TAG, "*********************");
4210 Log.d(TAG, "");
4211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004212}
Michael Jurka2763be32011-02-24 11:19:57 -08004213
Michael Jurkaabded662011-03-04 12:06:57 -08004214interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004215 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004216 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004217 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004218 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004219 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004220}