blob: 561bd16ffbf51958499e6192377ae1fc35b25b4b [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;
Adam Cohen96d30a12013-07-16 18:13:21 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
79import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070089import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Daniel Sandler325dc232013-06-05 22:57:57 -040098import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100118public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700120 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Daniel Sandlerf061f822013-06-27 13:59:36 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700126 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
Winson Chung70d72102011-08-12 11:24:35 -0700129 private static final int MENU_GROUP_WALLPAPER = 1;
130 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
131 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700132 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
133 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chung2672ff92012-05-04 16:22:30 -0700161 // The Intent extra that defines whether to ignore the launch animation
162 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400163 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
166 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700167 // Type: int
168 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
171 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: boolean
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
179 // Type: long
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
185 // Type: parcelable
186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100188 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700189 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700191 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100192 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700193
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700195 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700196 private State mState = State.WORKSPACE;
197 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800198 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700201 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
202 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700203 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700204 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800207 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Winson Chunga2413752012-04-03 14:22:34 -0700209 // How long to wait before the new-shortcut animation automatically pans the workspace
210 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700211 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700212
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800213 private final BroadcastReceiver mCloseSystemDialogsReceiver
214 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800215 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private LayoutInflater mInflater;
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800220 private View mQsbDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700221 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800222 private DragLayer mDragLayer;
223 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private AppWidgetManager mAppWidgetManager;
226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Michael Jurkac9d95c52011-08-29 14:03:34 -0700228 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700229 private AppWidgetProviderInfo mPendingAddWidgetInfo;
230
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231 private int[] mTmpAddItemCellCoordinates = new int[2];
232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private FolderInfo mFolderInfo;
234
Winson Chung3d503fb2011-07-13 17:25:49 -0700235 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800236 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700237
Winson Chungc51db6a2011-10-05 11:44:49 -0700238 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700239 private AppsCustomizeTabHost mAppsCustomizeTabHost;
240 private AppsCustomizePagedView mAppsCustomizeContent;
241 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700244 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
245 // scroll issues (because the workspace may not have been measured yet) and extra work.
246 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
247 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248
249 private SpannableStringBuilder mDefaultKeySsb = null;
250
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 private boolean mWorkspaceLoading = true;
252
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800253 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 private boolean mRestoring;
255 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700256 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257
Michael Jurka7607c2f2013-04-03 14:33:19 -0700258 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
259
Winson Chung4a2afa32012-07-19 14:53:05 -0700260 // Keep track of whether the user has left launcher
261 private static boolean sPausedFromUserAction = false;
262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 private Bundle mSavedInstanceState;
264
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800266 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800267 private boolean mUserPresent = true;
268 private boolean mVisible = false;
269 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700270 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700272 private static LocaleConfiguration sLocaleConfiguration = null;
273
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700274 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700275
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700276 private Intent mAppMarketIntent = null;
277
Adam Cohended9f8d2010-11-03 13:25:16 -0700278 // Related to the auto-advancing of widgets
279 private final int ADVANCE_MSG = 1;
280 private final int mAdvanceInterval = 20000;
281 private final int mAdvanceStagger = 250;
282 private long mAutoAdvanceSentTime;
283 private long mAutoAdvanceTimeLeft = -1;
284 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
285 new HashMap<View, AppWidgetProviderInfo>();
286
Winson Chung400438b2011-01-16 17:53:48 -0800287 // Determines how long to wait after a rotation before restoring the screen orientation to
288 // match the sensor state.
289 private final int mRestoreScreenOrientationDelay = 500;
290
Michael Jurka4ef207b2010-11-29 17:05:45 -0800291 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700292 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
293 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
294 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800295
Craig Mautner360310b2012-10-26 15:13:08 -0700296 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700297
Adam Cohen1462de32012-07-24 22:34:36 -0700298 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
299
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700300 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
301
Winson Chung46353de2012-02-16 14:05:10 -0800302 // We only want to get the SharedPreferences once since it does an FS stat each time we get
303 // it from the context.
304 private SharedPreferences mSharedPrefs;
305
Adam Cohene25af792013-06-06 23:08:25 -0700306 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
307
Winson Chungf0c6ae02012-03-21 16:10:31 -0700308 // Holds the page that we need to animate to, and the icon views that we need to animate up
309 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700310 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700311 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700312 private ImageView mFolderIconImageView;
313 private Bitmap mFolderIconBitmap;
314 private Canvas mFolderIconCanvas;
315 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700316
Michael Jurkaddd62e92011-02-16 17:49:14 -0800317 private BubbleTextView mWaitingForResume;
318
Michael Jurka22143132012-10-04 17:42:38 +0200319 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
320 = new HideFromAccessibilityHelper();
321
Michael Jurkac1f5d262011-09-30 19:32:27 -0700322 private Runnable mBuildLayersRunnable = new Runnable() {
323 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700324 if (mWorkspace != null) {
325 mWorkspace.buildPageHardwareLayers();
326 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700327 }
328 };
329
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800330 private static ArrayList<PendingAddArguments> sPendingAddList
331 = new ArrayList<PendingAddArguments>();
332
Michael Jurka0a457bf2012-11-19 14:05:05 -0800333 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
334
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800335 private static class PendingAddArguments {
336 int requestCode;
337 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700338 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700339 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800340 int cellX;
341 int cellY;
342 }
343
Michael Jurka0a457bf2012-11-19 14:05:05 -0800344 private static boolean isPropertyEnabled(String propertyName) {
345 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700346 }
347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 @Override
349 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700350 if (DEBUG_STRICT_MODE) {
351 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
352 .detectDiskReads()
353 .detectDiskWrites()
354 .detectNetwork() // or .detectAll() for all detectable problems
355 .penaltyLog()
356 .build());
357 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
358 .detectLeakedSqlLiteObjects()
359 .detectLeakedClosableObjects()
360 .penaltyLog()
361 .penaltyDeath()
362 .build());
363 }
364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400366
367 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
368 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400369 LauncherAppState app = LauncherAppState.getInstance();
370 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700371 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800372 mModel = app.setLauncher(this);
373 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400374 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700376
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700377 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700378 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
379 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700380
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700381 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
382 // this also ensures that any synchronous binding below doesn't re-trigger another
383 // LauncherModel load.
384 mPaused = false;
385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200387 android.os.Debug.startMethodTracing(
388 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 }
390
391 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 setContentView(R.layout.launcher);
393 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700394 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800396 registerContentObservers();
397
Joe Onorato7c312c12009-08-13 21:36:53 -0700398 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 mSavedState = savedInstanceState;
401 restoreState(mSavedState);
402
Winson Chunga12a2502010-12-20 14:41:35 -0800403 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700404 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200405 mAppsCustomizeContent.onPackagesUpdated(
406 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700407 }
Winson Chunga12a2502010-12-20 14:41:35 -0800408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 if (PROFILE_STARTUP) {
410 android.os.Debug.stopMethodTracing();
411 }
412
413 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700414 if (sPausedFromUserAction) {
415 // If the user leaves launcher, then we should just load items asynchronously when
416 // they return.
417 mModel.startLoader(true, -1);
418 } else {
419 // We only load the page synchronously if the user rotates (or triggers a
420 // configuration change) while launcher is in the foreground
421 mModel.startLoader(true, mWorkspace.getCurrentPage());
422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
424
425 // For handling default keys
426 mDefaultKeySsb = new SpannableStringBuilder();
427 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800428
429 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
430 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800431
Adam Cohenf9426d52012-06-04 17:26:21 -0700432 updateGlobalIcons();
433
434 // On large interfaces, we want the screen to auto-rotate based on the current orientation
435 unlockScreenOrientation(true);
436 }
437
Winson Chung4a2afa32012-07-19 14:53:05 -0700438 protected void onUserLeaveHint() {
439 super.onUserLeaveHint();
440 sPausedFromUserAction = true;
441 }
442
Adam Cohenf9426d52012-06-04 17:26:21 -0700443 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700444 boolean searchVisible = false;
445 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800446 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700447 int coi = getCurrentOrientationIndexForGlobalIcons();
448 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
449 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700450 updateAppMarketIcon();
451 searchVisible = updateGlobalSearchIcon();
452 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700453 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700454 if (sGlobalSearchIcon[coi] != null) {
455 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700456 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700457 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700458 if (sVoiceSearchIcon[coi] != null) {
459 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700460 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700461 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700462 if (sAppMarketIcon[coi] != null) {
463 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800464 }
Winson Chungadf0c182012-08-23 14:59:07 -0700465 if (mSearchDropTargetBar != null) {
466 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 }
Romain Guycbb89e42009-06-08 15:52:54 -0700469
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700471 if (sLocaleConfiguration == null) {
472 new AsyncTask<Void, Void, LocaleConfiguration>() {
473 @Override
474 protected LocaleConfiguration doInBackground(Void... unused) {
475 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
476 readConfiguration(Launcher.this, localeConfiguration);
477 return localeConfiguration;
478 }
479
480 @Override
481 protected void onPostExecute(LocaleConfiguration result) {
482 sLocaleConfiguration = result;
483 checkForLocaleChange(); // recursive, but now with a locale configuration
484 }
485 }.execute();
486 return;
487 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700488
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 final Configuration configuration = getResources().getConfiguration();
490
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700491 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 final String locale = configuration.locale.toString();
493
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700494 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 final int mcc = configuration.mcc;
496
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700497 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 final int mnc = configuration.mnc;
499
Romain Guy84f296c2009-11-04 15:00:44 -0800500 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501
Romain Guy84f296c2009-11-04 15:00:44 -0800502 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700503 sLocaleConfiguration.locale = locale;
504 sLocaleConfiguration.mcc = mcc;
505 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700506
Joe Onorato0589f0f2010-02-08 13:44:00 -0800507 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700508
509 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
510 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700511 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700512 public void run() {
513 writeConfiguration(Launcher.this, localeConfiguration);
514 }
515 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700516 }
517 }
518
519 private static class LocaleConfiguration {
520 public String locale;
521 public int mcc = -1;
522 public int mnc = -1;
523 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700524
Romain Guy98d01652009-06-30 16:21:04 -0700525 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
526 DataInputStream in = null;
527 try {
528 in = new DataInputStream(context.openFileInput(PREFERENCES));
529 configuration.locale = in.readUTF();
530 configuration.mcc = in.readInt();
531 configuration.mnc = in.readInt();
532 } catch (FileNotFoundException e) {
533 // Ignore
534 } catch (IOException e) {
535 // Ignore
536 } finally {
537 if (in != null) {
538 try {
539 in.close();
540 } catch (IOException e) {
541 // Ignore
542 }
543 }
544 }
545 }
546
547 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
548 DataOutputStream out = null;
549 try {
550 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
551 out.writeUTF(configuration.locale);
552 out.writeInt(configuration.mcc);
553 out.writeInt(configuration.mnc);
554 out.flush();
555 } catch (FileNotFoundException e) {
556 // Ignore
557 } catch (IOException e) {
558 //noinspection ResultOfMethodCallIgnored
559 context.getFileStreamPath(PREFERENCES).delete();
560 } finally {
561 if (out != null) {
562 try {
563 out.close();
564 } catch (IOException e) {
565 // Ignore
566 }
567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
569 }
570
Bjorn Bringertc459e522013-06-07 19:36:01 +0100571 public LayoutInflater getInflater() {
572 return mInflater;
573 }
574
Adam Cohen716b51e2011-06-30 12:09:54 -0700575 public DragLayer getDragLayer() {
576 return mDragLayer;
577 }
578
Winson Chung36a62fe2012-05-06 18:04:42 -0700579 boolean isDraggingEnabled() {
580 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
581 // that is subsequently removed from the workspace in startBinding().
582 return !mModel.isLoadingWorkspace();
583 }
584
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 static int getScreen() {
586 synchronized (sLock) {
587 return sScreen;
588 }
589 }
590
591 static void setScreen(int screen) {
592 synchronized (sLock) {
593 sScreen = screen;
594 }
595 }
596
Winson Chung557d6ed2011-07-08 15:34:52 -0700597 /**
598 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
599 * a configuration step, this allows the proper animations to run after other transitions.
600 */
601 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700602 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800603 switch (args.requestCode) {
604 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700605 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700606 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800607 break;
608 case REQUEST_PICK_SHORTCUT:
609 processShortcut(args.intent);
610 break;
611 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700612 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700613 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700614 result = true;
615 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800616 case REQUEST_CREATE_APPWIDGET:
617 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700618 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700619 result = true;
620 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800621 case REQUEST_PICK_WALLPAPER:
622 // We just wanted the activity result here so we can clear mWaitingForResult
623 break;
624 }
Michael Jurka27614d22012-04-02 06:40:22 -0700625 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
626 // if you turned the screen off and then back while in All Apps, Launcher would not
627 // return to the workspace. Clearing mAddInfo.container here fixes this issue
628 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700629 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800630 }
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700633 protected void onActivityResult(
634 final int requestCode, final int resultCode, final Intent data) {
635 if (requestCode == REQUEST_BIND_APPWIDGET) {
636 int appWidgetId = data != null ?
637 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
638 if (resultCode == RESULT_CANCELED) {
639 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
640 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700641 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700642 }
643 return;
644 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700645 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800646 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
647 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700648 mWaitingForResult = false;
649
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 // We have special handling for widgets
651 if (isWidgetDrop) {
652 int appWidgetId = data != null ?
653 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700654 if (appWidgetId < 0) {
655 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
656 "widget configuration activity.");
657 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
658 } else {
659 completeTwoStageWidgetDrop(resultCode, appWidgetId);
660 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 return;
662 }
663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 // The pattern used here is that a user PICKs a specific application,
665 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
668 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700669 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800670 final PendingAddArguments args = new PendingAddArguments();
671 args.requestCode = requestCode;
672 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700673 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700674 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700675 args.cellX = mPendingAddInfo.cellX;
676 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800677 if (isWorkspaceLocked()) {
678 sPendingAddList.add(args);
679 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700680 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800683 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700684 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
686 null);
687 }
688
689 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700690 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 Runnable onCompleteRunnable = null;
693 int animationType = 0;
694
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700695 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800696 if (resultCode == RESULT_OK) {
697 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
698 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700699 mPendingAddWidgetInfo);
700 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800701 onCompleteRunnable = new Runnable() {
702 @Override
703 public void run() {
704 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800706 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
707 null);
708 }
709 };
710 } else if (resultCode == RESULT_CANCELED) {
711 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
712 onCompleteRunnable = new Runnable() {
713 @Override
714 public void run() {
715 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
716 null);
717 }
718 };
719 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700720 if (mDragLayer.getAnimatedView() != null) {
721 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
722 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
723 animationType, boundWidget, true);
724 } else {
725 // The animated view may be null in the case of a rotation during widget configuration
726 onCompleteRunnable.run();
727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
729
730 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700731 protected void onStop() {
732 super.onStop();
733 FirstFrameAnimatorHelper.setIsVisible(false);
734 }
735
736 @Override
737 protected void onStart() {
738 super.onStart();
739 FirstFrameAnimatorHelper.setIsVisible(true);
740 }
741
742 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200744 long startTime = 0;
745 if (DEBUG_RESUME_TIME) {
746 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400747 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700750
Winson Chung4a2afa32012-07-19 14:53:05 -0700751 // Restore the previous launcher state
752 if (mOnResumeState == State.WORKSPACE) {
753 showWorkspace(false);
754 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
755 showAllApps(false);
756 }
757 mOnResumeState = State.NONE;
758
Craig Mautner360310b2012-10-26 15:13:08 -0700759 // Background was set to gradient in onPause(), restore to black if in all apps.
760 setWorkspaceBackground(mState == State.WORKSPACE);
761
Winson Chungde0fb8f2012-05-08 14:37:08 -0700762 // Process any items that were added while Launcher was away
763 InstallShortcutReceiver.flushInstallQueue(this);
764
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800765 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700766 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700767 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700769 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400770 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700771 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200773 if (mOnResumeCallbacks.size() > 0) {
774 // We might have postponed some bind calls until onResume (see waitUntilResume) --
775 // execute them here
776 long startTimeCallbacks = 0;
777 if (DEBUG_RESUME_TIME) {
778 startTimeCallbacks = System.currentTimeMillis();
779 }
780
781 if (mAppsCustomizeContent != null) {
782 mAppsCustomizeContent.setBulkBind(true);
783 }
784 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
785 mOnResumeCallbacks.get(i).run();
786 }
787 if (mAppsCustomizeContent != null) {
788 mAppsCustomizeContent.setBulkBind(false);
789 }
790 mOnResumeCallbacks.clear();
791 if (DEBUG_RESUME_TIME) {
792 Log.d(TAG, "Time spent processing callbacks in onResume: " +
793 (System.currentTimeMillis() - startTimeCallbacks));
794 }
Michael Jurka447bf842013-05-15 14:52:15 +0200795 }
Winson Chunge4e50662012-01-23 14:45:13 -0800796
797 // Reset the pressed state of icons that were locked in the press state while activities
798 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800799 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800800 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800801 mWaitingForResume.setStayPressed(false);
802 }
Winson Chunge4e50662012-01-23 14:45:13 -0800803 if (mAppsCustomizeContent != null) {
804 // Resets the previous all apps icon press state
805 mAppsCustomizeContent.resetDrawableState();
806 }
Adam Cohen06dff352012-06-01 17:17:08 -0700807 // It is possible that widgets can receive updates while launcher is not in the foreground.
808 // Consequently, the widgets will be inflated in the orientation of the foreground activity
809 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
810 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700811 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700812
813 // Again, as with the above scenario, it's possible that one or more of the global icons
814 // were updated in the wrong orientation.
815 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200816 if (DEBUG_RESUME_TIME) {
817 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
818 }
Adam Cohen06dff352012-06-01 17:17:08 -0700819 }
820
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700822 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700823 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
824 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
825 // when Launcher resumes and we are still in AllApps.
826 updateWallpaperVisibility(true);
827
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700828 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700829 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800830 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700831 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700832 }
Romain Guycbb89e42009-06-08 15:52:54 -0700833
Adam Cohen66a01fd2013-06-11 12:48:00 -0700834 protected void onFinishBindingItems() {
835 }
836
837 // Add a fullscreen unpadded view to the workspace to the left all other screens.
838 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700839 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700840 }
841
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700842 @Override
843 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 // Flag the loader to stop early before switching
845 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700846 if (mAppsCustomizeContent != null) {
847 mAppsCustomizeContent.surrender();
848 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800849 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700850 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700851
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800852 // We can't hide the IME if it was forced open. So don't bother
853 /*
854 @Override
855 public void onWindowFocusChanged(boolean hasFocus) {
856 super.onWindowFocusChanged(hasFocus);
857
858 if (hasFocus) {
859 final InputMethodManager inputManager = (InputMethodManager)
860 getSystemService(Context.INPUT_METHOD_SERVICE);
861 WindowManager.LayoutParams lp = getWindow().getAttributes();
862 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
863 android.os.Handler()) {
864 protected void onReceiveResult(int resultCode, Bundle resultData) {
865 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
866 }
867 });
868 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
869 }
870 }
871 */
872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 private boolean acceptFilter() {
874 final InputMethodManager inputManager = (InputMethodManager)
875 getSystemService(Context.INPUT_METHOD_SERVICE);
876 return !inputManager.isFullscreenMode();
877 }
878
879 @Override
880 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700881 final int uniChar = event.getUnicodeChar();
882 final boolean handled = super.onKeyDown(keyCode, event);
883 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
884 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
886 keyCode, event);
887 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700888 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700889 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700890 // showSearchDialog()
891 // If there are multiple keystrokes before the search dialog takes focus,
892 // onSearchRequested() will be called for every keystroke,
893 // but it is idempotent, so it's fine.
894 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896 }
897
Joe Onorato8a9625e2010-01-28 15:55:35 -0800898 // Eat the long press event so the keyboard doesn't come up.
899 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
900 return true;
901 }
902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 return handled;
904 }
905
Karl Rosaen138a0412009-04-23 19:00:21 -0700906 private String getTypedText() {
907 return mDefaultKeySsb.toString();
908 }
909
910 private void clearTypedText() {
911 mDefaultKeySsb.clear();
912 mDefaultKeySsb.clearSpans();
913 Selection.setSelection(mDefaultKeySsb, 0);
914 }
915
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700917 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
918 * State
919 */
920 private static State intToState(int stateOrdinal) {
921 State state = State.WORKSPACE;
922 final State[] stateValues = State.values();
923 for (int i = 0; i < stateValues.length; i++) {
924 if (stateValues[i].ordinal() == stateOrdinal) {
925 state = stateValues[i];
926 break;
927 }
928 }
929 return state;
930 }
931
932 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 * Restores the previous state, if it exists.
934 *
935 * @param savedState The previous state.
936 */
937 private void restoreState(Bundle savedState) {
938 if (savedState == null) {
939 return;
940 }
941
Michael Jurka883f55b2010-10-21 15:47:14 -0700942 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700943 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700944 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800945 }
946
Winson Chungf0c6ae02012-03-21 16:10:31 -0700947 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700949 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 }
951
Winson Chung3d503fb2011-07-13 17:25:49 -0700952 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700953 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700954
Winson Chung3d503fb2011-07-13 17:25:49 -0700955 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
956 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700957 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700958 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
959 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700960 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
961 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
962 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700963 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 mRestoring = true;
965 }
966
967 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
968 if (renameFolder) {
969 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700970 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 mRestoring = true;
972 }
Winson Chunga12a2502010-12-20 14:41:35 -0800973
Winson Chung785d2eb2011-04-14 16:08:02 -0700974 // Restore the AppsCustomize tab
975 if (mAppsCustomizeTabHost != null) {
976 String curTab = savedState.getString("apps_customize_currentTab");
977 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700978 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700979 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700980 mAppsCustomizeContent.loadAssociatedPages(
981 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700982 }
983
Winson Chung5afbf7b2011-07-25 11:53:08 -0700984 int currentIndex = savedState.getInt("apps_customize_currentIndex");
985 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 }
988
989 /**
990 * Finds all the views we need and configure them properly.
991 */
992 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700993 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400994
Craig Mautner360310b2012-10-26 15:13:08 -0700995 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700996 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
997 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700998 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -0700999
1000 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1001 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002
Winson Chung4c98d922011-05-31 16:50:48 -07001003 // Setup the drag layer
1004 mDragLayer.setup(this, dragController);
1005
Winson Chung3d503fb2011-07-13 17:25:49 -07001006 // Setup the hotseat
1007 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1008 if (mHotseat != null) {
1009 mHotseat.setup(this);
1010 }
1011
Winson Chung4c98d922011-05-31 16:50:48 -07001012 // Setup the workspace
1013 mWorkspace.setHapticFeedbackEnabled(false);
1014 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001015 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001016 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001017
Winson Chungf0ea4d32011-06-06 14:27:16 -07001018 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001019 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001020
Winson Chungf0ea4d32011-06-06 14:27:16 -07001021 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001022 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001023 mAppsCustomizeContent = (AppsCustomizePagedView)
1024 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1025 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001026
Winson Chung3d503fb2011-07-13 17:25:49 -07001027 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001028 dragController.setDragScoller(mWorkspace);
1029 dragController.setScrollView(mDragLayer);
1030 dragController.setMoveTarget(mWorkspace);
1031 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001032 if (mSearchDropTargetBar != null) {
1033 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001034 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001035
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001036 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001037 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001038 final View ww = new WeightWatcher(this);
1039 ww.setAlpha(0.5f);
1040 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001041 new FrameLayout.LayoutParams(
1042 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001043 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001044 Gravity.BOTTOM)
1045 );
1046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 }
1048
1049 /**
1050 * Creates a view representing a shortcut.
1051 *
1052 * @param info The data structure describing the shortcut.
1053 *
1054 * @return A View inflated from R.layout.application.
1055 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001056 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001058 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
1060
1061 /**
1062 * Creates a view representing a shortcut inflated from the specified resource.
1063 *
1064 * @param layoutResId The id of the XML layout used to create the shortcut.
1065 * @param parent The group the shortcut belongs to.
1066 * @param info The data structure describing the shortcut.
1067 *
1068 * @return A View inflated from layoutResId.
1069 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001070 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001071 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1072 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 return favorite;
1075 }
1076
1077 /**
1078 * Add an application shortcut to the workspace.
1079 *
1080 * @param data The intent describing the application.
1081 * @param cellInfo The position on screen where to create the shortcut.
1082 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001084 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001085 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086
Adam Cohenfbba09b2011-07-18 21:43:05 -07001087 // First we check if we already know the exact location where we want to add this item.
1088 if (cellX >= 0 && cellY >= 0) {
1089 cellXY[0] = cellX;
1090 cellXY[1] = cellY;
1091 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001092 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001093 return;
1094 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001096 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001097
Romain Guy73b979d2009-06-09 12:57:21 -07001098 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001099 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001101 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001102 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001103 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001104 } else {
1105 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 }
1107 }
Romain Guycbb89e42009-06-08 15:52:54 -07001108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 /**
1110 * Add a shortcut to the workspace.
1111 *
1112 * @param data The intent describing the shortcut.
1113 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001116 int cellY) {
1117 int[] cellXY = mTmpAddItemCellCoordinates;
1118 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001119 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001120
Michael Jurkad3ef3062010-11-23 16:23:58 -08001121 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001122
Adam Cohen558baaf2011-08-15 15:22:57 -07001123 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001124 if (info == null) {
1125 return;
1126 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001127 final View view = createShortcut(info);
1128
Adam Cohenfbba09b2011-07-18 21:43:05 -07001129 // First we check if we already know the exact location where we want to add this item.
1130 if (cellX >= 0 && cellY >= 0) {
1131 cellXY[0] = cellX;
1132 cellXY[1] = cellY;
1133 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001134
1135 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001136 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001137 true, null,null)) {
1138 return;
1139 }
1140 DragObject dragObject = new DragObject();
1141 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001142 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1143 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001144 return;
1145 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001146 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001147 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001148 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001149 foundCellSpan = (result != null);
1150 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001151 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001152 }
1153
1154 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001155 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 return;
1157 }
1158
Adam Cohendcd297f2013-06-18 13:13:40 -07001159 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160
1161 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001162 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001163 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 }
1165 }
1166
Adam Cohen2f093b62012-04-30 18:59:53 -07001167 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1168 int minHeight) {
1169 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001170 // We want to account for the extra amount of padding that we are adding to the widget
1171 // to ensure that it gets the full amount of space that it has requested
1172 int requiredWidth = minWidth + padding.left + padding.right;
1173 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001174 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001175 }
1176
Adam Cohen2f093b62012-04-30 18:59:53 -07001177 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1178 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001179 }
1180
Adam Cohen2f093b62012-04-30 18:59:53 -07001181 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1182 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001183 }
1184
Adam Cohen2f093b62012-04-30 18:59:53 -07001185 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1186 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001187 }
1188
Adam Cohen2f093b62012-04-30 18:59:53 -07001189 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1190 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001191 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001192 }
1193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001195 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001197 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001198 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001200 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001201 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1202 if (appWidgetInfo == null) {
1203 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1204 }
Romain Guycbb89e42009-06-08 15:52:54 -07001205
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001206 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001207 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001208
Adam Cohen2f093b62012-04-30 18:59:53 -07001209 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1210 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001213 // We have saved the position to which the widget was dragged-- this really only matters
1214 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001215 int[] cellXY = mTmpAddItemCellCoordinates;
1216 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001217 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001218 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001219 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1220 cellXY[0] = mPendingAddInfo.cellX;
1221 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001222 spanXY[0] = mPendingAddInfo.spanX;
1223 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001224 foundCellSpan = true;
1225 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001226 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001227 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001228 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1229 spanXY[1], cellXY, finalSpan);
1230 spanXY[0] = finalSpan[0];
1231 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001232 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001233 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001234 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001235 }
1236
Michael Jurka0280c3b2010-09-17 15:00:07 -07001237 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001238 if (appWidgetId != -1) {
1239 // Deleting an app widget ID is a void call but writes to disk before returning
1240 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001241 new Thread("deleteAppWidgetId") {
1242 public void run() {
1243 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1244 }
1245 }.start();
1246 }
Winson Chung93eef082012-03-23 15:59:27 -07001247 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001248 return;
1249 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001251 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001252 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1253 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001254 launcherInfo.spanX = spanXY[0];
1255 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001256 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1257 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001258
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001260 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261
1262 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001263 if (hostView == null) {
1264 // Perform actual inflation because we're live
1265 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1266 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1267 } else {
1268 // The AppWidgetHostView has already been inflated and instantiated
1269 launcherInfo.hostView = hostView;
1270 }
Romain Guycbb89e42009-06-08 15:52:54 -07001271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001273 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001274 launcherInfo.notifyWidgetSizeChanged(this);
1275
Adam Cohendcd297f2013-06-18 13:13:40 -07001276 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001277 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001278
1279 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001281 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
Romain Guycbb89e42009-06-08 15:52:54 -07001283
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1285 @Override
1286 public void onReceive(Context context, Intent intent) {
1287 final String action = intent.getAction();
1288 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1289 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001290 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001291 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001292
Winson Chungc100e8e2011-08-09 16:02:43 -07001293 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001294 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001295 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1296 mAppsCustomizeTabHost.reset();
1297 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001298 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001299 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1300 mUserPresent = true;
1301 updateRunning();
1302 }
1303 }
1304 };
1305
1306 @Override
1307 public void onAttachedToWindow() {
1308 super.onAttachedToWindow();
1309
1310 // Listen for broadcasts related to user-presence
1311 final IntentFilter filter = new IntentFilter();
1312 filter.addAction(Intent.ACTION_SCREEN_OFF);
1313 filter.addAction(Intent.ACTION_USER_PRESENT);
1314 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001315 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001316 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001317 mVisible = true;
1318 }
1319
1320 @Override
1321 public void onDetachedFromWindow() {
1322 super.onDetachedFromWindow();
1323 mVisible = false;
1324
Adam Cohend113e0c2010-11-11 10:48:05 -08001325 if (mAttached) {
1326 unregisterReceiver(mReceiver);
1327 mAttached = false;
1328 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001329 updateRunning();
1330 }
1331
1332 public void onWindowVisibilityChanged(int visibility) {
1333 mVisible = visibility == View.VISIBLE;
1334 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001335 // The following code used to be in onResume, but it turns out onResume is called when
1336 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1337 // is a more appropriate event to handle
1338 if (mVisible) {
1339 mAppsCustomizeTabHost.onWindowVisible();
1340 if (!mWorkspaceLoading) {
1341 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001342 // We want to let Launcher draw itself at least once before we force it to build
1343 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001344 // apps is nice and speedy.
1345 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001346 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001347 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001348 if (mStarted) return;
1349 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001350 // We delay the layer building a bit in order to give
1351 // other message processing a time to run. In particular
1352 // this avoids a delay in hiding the IME if it was
1353 // currently shown, because doing that may involve
1354 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001355 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001356 final ViewTreeObserver.OnDrawListener listener = this;
1357 mWorkspace.post(new Runnable() {
1358 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001359 if (mWorkspace != null &&
1360 mWorkspace.getViewTreeObserver() != null) {
1361 mWorkspace.getViewTreeObserver().
1362 removeOnDrawListener(listener);
1363 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001364 }
1365 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001366 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001367 }
1368 });
1369 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001370 // When Launcher comes back to foreground, a different Activity might be responsible for
1371 // the app market intent, so refresh the icon
1372 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001373 clearTypedText();
1374 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001375 }
1376
1377 private void sendAdvanceMessage(long delay) {
1378 mHandler.removeMessages(ADVANCE_MSG);
1379 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1380 mHandler.sendMessageDelayed(msg, delay);
1381 mAutoAdvanceSentTime = System.currentTimeMillis();
1382 }
1383
1384 private void updateRunning() {
1385 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1386 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1387 mAutoAdvanceRunning = autoAdvanceRunning;
1388 if (autoAdvanceRunning) {
1389 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1390 sendAdvanceMessage(delay);
1391 } else {
1392 if (!mWidgetsToAdvance.isEmpty()) {
1393 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1394 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1395 }
1396 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001397 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001398 }
1399 }
1400 }
1401
1402 private final Handler mHandler = new Handler() {
1403 @Override
1404 public void handleMessage(Message msg) {
1405 if (msg.what == ADVANCE_MSG) {
1406 int i = 0;
1407 for (View key: mWidgetsToAdvance.keySet()) {
1408 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1409 final int delay = mAdvanceStagger * i;
1410 if (v instanceof Advanceable) {
1411 postDelayed(new Runnable() {
1412 public void run() {
1413 ((Advanceable) v).advance();
1414 }
1415 }, delay);
1416 }
1417 i++;
1418 }
1419 sendAdvanceMessage(mAdvanceInterval);
1420 }
1421 }
1422 };
1423
1424 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001425 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001426 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1427 if (v instanceof Advanceable) {
1428 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001429 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001430 updateRunning();
1431 }
1432 }
1433
1434 void removeWidgetToAutoAdvance(View hostView) {
1435 if (mWidgetsToAdvance.containsKey(hostView)) {
1436 mWidgetsToAdvance.remove(hostView);
1437 updateRunning();
1438 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001439 }
1440
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001442 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443 launcherInfo.hostView = null;
1444 }
1445
Winson Chung93eef082012-03-23 15:59:27 -07001446 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1447 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1448 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001449 }
1450
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001451 public LauncherAppWidgetHost getAppWidgetHost() {
1452 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
Romain Guycbb89e42009-06-08 15:52:54 -07001454
Winson Chunga9abd0e2010-10-27 17:18:37 -07001455 public LauncherModel getModel() {
1456 return mModel;
1457 }
1458
Bjorn Bringertc459e522013-06-07 19:36:01 +01001459 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001460 getWindow().closeAllPanels();
1461
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001462 // Whatever we were doing is hereby canceled.
1463 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001464 }
1465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 @Override
1467 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001468 long startTime = 0;
1469 if (DEBUG_RESUME_TIME) {
1470 startTime = System.currentTimeMillis();
1471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 super.onNewIntent(intent);
1473
1474 // Close the menu
1475 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001476 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001477 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001478
Jamie Genniscb222e82012-10-23 15:44:26 -07001479 final boolean alreadyOnHome =
1480 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001481 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001482
Jamie Genniscb222e82012-10-23 15:44:26 -07001483 Runnable processIntent = new Runnable() {
1484 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001485 if (mWorkspace == null) {
1486 // Can be cases where mWorkspace is null, this prevents a NPE
1487 return;
1488 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001489 Folder openFolder = mWorkspace.getOpenFolder();
1490 // In all these cases, only animate if we're already on home
1491 mWorkspace.exitWidgetResizeMode();
1492 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1493 openFolder == null) {
1494 mWorkspace.moveToDefaultScreen(true);
1495 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001496
Jamie Genniscb222e82012-10-23 15:44:26 -07001497 closeFolder();
1498 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001499
Jamie Genniscb222e82012-10-23 15:44:26 -07001500 // If we are already on home, then just animate back to the workspace,
1501 // otherwise, just wait until onResume to set the state back to Workspace
1502 if (alreadyOnHome) {
1503 showWorkspace(true);
1504 } else {
1505 mOnResumeState = State.WORKSPACE;
1506 }
1507
1508 final View v = getWindow().peekDecorView();
1509 if (v != null && v.getWindowToken() != null) {
1510 InputMethodManager imm = (InputMethodManager)getSystemService(
1511 INPUT_METHOD_SERVICE);
1512 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1513 }
1514
1515 // Reset AllApps to its initial state
1516 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1517 mAppsCustomizeTabHost.reset();
1518 }
1519 }
1520 };
1521
1522 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1523 // Delay processing of the intent to allow the status bar animation to finish
1524 // first in order to avoid janky animations.
1525 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001526 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001527 // Process the intent immediately.
1528 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001529 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001530
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
Michael Jurka447bf842013-05-15 14:52:15 +02001532 if (DEBUG_RESUME_TIME) {
1533 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
1536
1537 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001538 public void onRestoreInstanceState(Bundle state) {
1539 super.onRestoreInstanceState(state);
1540 for (int page: mSynchronouslyBoundPages) {
1541 mWorkspace.restoreInstanceStateForChild(page);
1542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
1544
1545 @Override
1546 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001547 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001548 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
Michael Jurka883f55b2010-10-21 15:47:14 -07001550 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001551 // We close any open folder since it will not be re-opened, and we need to make sure
1552 // this state is reflected.
1553 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554
Adam Cohendcd297f2013-06-18 13:13:40 -07001555 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001556 mWaitingForResult) {
1557 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001559 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1560 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001561 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1562 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1563 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
1565
1566 if (mFolderInfo != null && mWaitingForResult) {
1567 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1568 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1569 }
Michael Jurka946ad472010-07-09 18:05:18 -07001570
Winson Chung785d2eb2011-04-14 16:08:02 -07001571 // Save the current AppsCustomize tab
1572 if (mAppsCustomizeTabHost != null) {
1573 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1574 if (currentTabTag != null) {
1575 outState.putString("apps_customize_currentTab", currentTabTag);
1576 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001577 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1578 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581
1582 @Override
1583 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001585
Winson Chunge7a03942011-08-05 15:05:12 -07001586 // Remove all pending runnables
1587 mHandler.removeMessages(ADVANCE_MSG);
1588 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001589 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001590
Winson Chungcd2b0142011-06-08 16:02:26 -07001591 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001592 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001593 mModel.stopLoader();
1594 app.setLauncher(null);
1595
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001597 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001599 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001601 mAppWidgetHost = null;
1602
1603 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604
1605 TextKeyListener.getInstance().release();
1606
Winson Chung81b52252012-08-27 15:34:29 -07001607 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1608 // to prevent leaking Launcher activities on orientation change.
1609 if (mModel != null) {
1610 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1611 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001612
1613 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001614 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001615
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001616 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001617 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1618 mWorkspace.removeAllViews();
1619 mWorkspace = null;
1620 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001621
Michael Jurka2ecf9952012-06-18 12:52:28 -07001622 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 }
1624
Adam Cohena9cf38f2011-05-02 15:36:58 -07001625 public DragController getDragController() {
1626 return mDragController;
1627 }
1628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 @Override
1630 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001631 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 super.startActivityForResult(intent, requestCode);
1633 }
1634
Winson Chung70d72102011-08-12 11:24:35 -07001635 /**
1636 * Indicates that we want global search for this activity by setting the globalSearch
1637 * argument for {@link #startSearch} to true.
1638 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001640 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001642
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001643 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001644
Karl Rosaen138a0412009-04-23 19:00:21 -07001645 if (initialQuery == null) {
1646 // Use any text typed in the launcher as the initial query
1647 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 if (appSearchData == null) {
1650 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001651 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 }
Winson Chungadf0c182012-08-23 14:59:07 -07001653 Rect sourceBounds = new Rect();
1654 if (mSearchDropTargetBar != null) {
1655 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1656 }
Romain Guycbb89e42009-06-08 15:52:54 -07001657
Bjorn Bringertc459e522013-06-07 19:36:01 +01001658 startSearch(initialQuery, selectInitialQuery,
1659 appSearchData, sourceBounds);
1660 }
1661
1662 public void startSearch(String initialQuery,
1663 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001664 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001665 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001666 }
1667
1668 /**
1669 * Starts the global search activity. This code is a copied from SearchManager
1670 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001671 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001672 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001673 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001674 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1675 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1676 if (globalSearchActivity == null) {
1677 Log.w(TAG, "No global search activity found.");
1678 return;
1679 }
1680 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1681 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1682 intent.setComponent(globalSearchActivity);
1683 // Make sure that we have a Bundle to put source in
1684 if (appSearchData == null) {
1685 appSearchData = new Bundle();
1686 } else {
1687 appSearchData = new Bundle(appSearchData);
1688 }
1689 // Set source to package name of app that starts global search, if not set already.
1690 if (!appSearchData.containsKey("source")) {
1691 appSearchData.putString("source", getPackageName());
1692 }
1693 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1694 if (!TextUtils.isEmpty(initialQuery)) {
1695 intent.putExtra(SearchManager.QUERY, initialQuery);
1696 }
1697 if (selectInitialQuery) {
1698 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1699 }
1700 intent.setSourceBounds(sourceBounds);
1701 try {
1702 startActivity(intent);
1703 } catch (ActivityNotFoundException ex) {
1704 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1705 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 }
1707
Winson Chung70d72102011-08-12 11:24:35 -07001708 @Override
1709 public boolean onCreateOptionsMenu(Menu menu) {
1710 if (isWorkspaceLocked()) {
1711 return false;
1712 }
1713
1714 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001715
1716 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1717 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1718 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001719 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1720 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1721 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001722 String helpUrl = getString(R.string.help_url);
1723 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001724 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1725 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1726
Winson Chung70d72102011-08-12 11:24:35 -07001727 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1728 .setIcon(android.R.drawable.ic_menu_gallery)
1729 .setAlphabeticShortcut('W');
1730 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1731 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001732 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001733 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001734 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1735 .setIcon(android.R.drawable.ic_menu_preferences)
1736 .setIntent(settings)
1737 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001738 if (!helpUrl.isEmpty()) {
1739 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1740 .setIcon(android.R.drawable.ic_menu_help)
1741 .setIntent(help)
1742 .setAlphabeticShortcut('H');
1743 }
Winson Chung70d72102011-08-12 11:24:35 -07001744 return true;
1745 }
1746
1747 @Override
1748 public boolean onPrepareOptionsMenu(Menu menu) {
1749 super.onPrepareOptionsMenu(menu);
1750
1751 if (mAppsCustomizeTabHost.isTransitioning()) {
1752 return false;
1753 }
1754 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1755 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1756
1757 return true;
1758 }
1759
1760 @Override
1761 public boolean onOptionsItemSelected(MenuItem item) {
1762 switch (item.getItemId()) {
1763 case MENU_WALLPAPER_SETTINGS:
1764 startWallpaper();
1765 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001766 }
1767
1768 return super.onOptionsItemSelected(item);
1769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001771 @Override
1772 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001773 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001774 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001775 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001776 }
1777
Joe Onorato9c1289c2009-08-17 11:03:03 -04001778 public boolean isWorkspaceLocked() {
1779 return mWorkspaceLoading || mWaitingForResult;
1780 }
1781
Michael Jurka0280c3b2010-09-17 15:00:07 -07001782 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001783 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001784 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001785 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1786 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001787 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001788 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001789 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001790
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001791 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1792 AppWidgetProviderInfo appWidgetInfo) {
1793 if (appWidgetInfo.configure != null) {
1794 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001795
Bjorn Bringert7984c942009-12-09 15:38:25 +00001796 // Launch over to configure widget, if needed
1797 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001798 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001799 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001800 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001802 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001803 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001804 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001805 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001806 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 }
1808 }
Romain Guycbb89e42009-06-08 15:52:54 -07001809
Adam Cohenfbba09b2011-07-18 21:43:05 -07001810 /**
1811 * Process a shortcut drop.
1812 *
1813 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001814 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001815 * @param cell The cell it should be added to, optional
1816 * @param position The location on the screen where it was dropped, optional
1817 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001818 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001819 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001820 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001821 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001822 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001823 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001824
1825 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001826 mPendingAddInfo.cellX = cell[0];
1827 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001828 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001829
1830 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1831 createShortcutIntent.setComponent(componentName);
1832 processShortcut(createShortcutIntent);
1833 }
1834
Adam Cohenfbba09b2011-07-18 21:43:05 -07001835 /**
1836 * Process a widget drop.
1837 *
1838 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001839 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001840 * @param cell The cell it should be added to, optional
1841 * @param position The location on the screen where it was dropped, optional
1842 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001843 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001844 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001845 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001846 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001847 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001848 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001849 mPendingAddInfo.minSpanX = info.minSpanX;
1850 mPendingAddInfo.minSpanY = info.minSpanY;
1851
Adam Cohenfbba09b2011-07-18 21:43:05 -07001852 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001853 mPendingAddInfo.cellX = cell[0];
1854 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001855 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001856 if (span != null) {
1857 mPendingAddInfo.spanX = span[0];
1858 mPendingAddInfo.spanY = span[1];
1859 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001860
Adam Cohened66b2b2012-01-23 17:28:51 -08001861 AppWidgetHostView hostView = info.boundWidget;
1862 int appWidgetId;
1863 if (hostView != null) {
1864 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001865 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001866 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001867 // In this case, we either need to start an activity to get permission to bind
1868 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001869 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001870 Bundle options = info.bindOptions;
1871
1872 boolean success = false;
1873 if (options != null) {
1874 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1875 info.componentName, options);
1876 } else {
1877 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1878 info.componentName);
1879 }
1880 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001881 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001882 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001883 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001884 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1885 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1886 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001887 // TODO: we need to make sure that this accounts for the options bundle.
1888 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001889 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1890 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001891 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001892 }
1893
Joe Onoratodeb98af2010-02-19 14:59:39 -08001894 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001895 // Handle case where user selected "Applications"
1896 String applicationName = getResources().getString(R.string.group_applications);
1897 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001898
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001899 if (applicationName != null && applicationName.equals(shortcutName)) {
1900 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1901 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001902
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001903 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1904 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001905 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001906 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001907 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001908 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001909 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
1911
Winson Chung24ab2f12010-09-16 14:10:47 -07001912 void processWallpaper(Intent intent) {
1913 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1914 }
1915
Adam Cohendcd297f2013-06-18 13:13:40 -07001916 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001917 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001918 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 folderInfo.title = getText(R.string.folder_name);
1920
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001922 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001923 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001924 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925
1926 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001927 FolderIcon newFolder =
1928 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001929 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001930 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001931 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
Romain Guycbb89e42009-06-08 15:52:54 -07001933
Joe Onorato9c1289c2009-08-17 11:03:03 -04001934 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001935 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001936 }
1937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001939 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001941 Intent chooser = Intent.createChooser(pickWallpaper,
1942 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001943 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1944 // Removed in Eclair MR1
1945// WallpaperManager wm = (WallpaperManager)
1946// getSystemService(Context.WALLPAPER_SERVICE);
1947// WallpaperInfo wi = wm.getWallpaperInfo();
1948// if (wi != null && wi.getSettingsActivity() != null) {
1949// LabeledIntent li = new LabeledIntent(getPackageName(),
1950// R.string.configure_wallpaper, 0);
1951// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1952// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1953// }
Mike Clerona0618e42009-10-22 13:55:21 -07001954 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
1956
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001957 /**
1958 * Registers various content observers. The current implementation registers
1959 * only a favorites observer to keep track of the favorites applications.
1960 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001961 private void registerContentObservers() {
1962 ContentResolver resolver = getContentResolver();
1963 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1964 true, mWidgetObserver);
1965 }
1966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 @Override
1968 public boolean dispatchKeyEvent(KeyEvent event) {
1969 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1970 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001972 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001973 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001974 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001975 dumpState();
1976 return true;
1977 }
1978 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001979 }
1980 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1981 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001982 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 return true;
1984 }
1985 }
1986
1987 return super.dispatchKeyEvent(event);
1988 }
1989
Joe Onorato88ec0992009-11-19 13:16:06 -08001990 @Override
1991 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001992 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001993 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001994 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001995 Folder openFolder = mWorkspace.getOpenFolder();
1996 if (openFolder.isEditingName()) {
1997 openFolder.dismissEditingName();
1998 } else {
1999 closeFolder();
2000 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002001 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002002 mWorkspace.exitWidgetResizeMode();
2003
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002004 // Back button is a no-op here, but give at least some feedback for the button press
2005 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002006 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002007 }
2008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002010 * Re-listen when widgets are reset.
2011 */
2012 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002013 if (mAppWidgetHost != null) {
2014 mAppWidgetHost.startListening();
2015 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002016 }
2017
2018 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 * Launches the intent referred by the clicked shortcut.
2020 *
2021 * @param v The view representing the clicked shortcut.
2022 */
2023 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002024 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2025 // view has detached (it's possible for this to happen if the view is removed mid touch).
2026 if (v.getWindowToken() == null) {
2027 return;
2028 }
2029
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002030 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002031 return;
2032 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002035 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002037 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002038
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002039 // Check for special shortcuts
2040 if (intent.getComponent() != null) {
2041 final String shortcutClass = intent.getComponent().getClassName();
2042
2043 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2044 showAllApps(true);
2045 return;
2046 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2047 MemoryDumpActivity.startDump(this);
2048 return;
2049 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002050 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002051
2052 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002053 int[] pos = new int[2];
2054 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002055 intent.setSourceBounds(new Rect(pos[0], pos[1],
2056 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002057
2058 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002059
2060 if (success && v instanceof BubbleTextView) {
2061 mWaitingForResume = (BubbleTextView) v;
2062 mWaitingForResume.setStayPressed(true);
2063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002065 if (v instanceof FolderIcon) {
2066 FolderIcon fi = (FolderIcon) v;
2067 handleFolderClick(fi);
2068 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002069 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002070 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002071 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002072 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002073 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076 }
2077
Michael Jurka0e260592010-06-30 17:07:39 -07002078 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002079 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002080 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002081 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2082 showWorkspace(true);
2083 }
Michael Jurka0e260592010-06-30 17:07:39 -07002084 return false;
2085 }
2086
Michael Jurkaaf442092010-06-10 17:01:57 -07002087 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002088 * Event handler for the search button
2089 *
2090 * @param v The view that was clicked.
2091 */
2092 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002093 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2094
Amith Yamasania135ba82011-08-09 17:42:01 -07002095 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002096 }
2097
2098 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002099 * Event handler for the voice button
2100 *
2101 * @param v The view that was clicked.
2102 */
2103 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002104 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002105
Bjorn Bringertc459e522013-06-07 19:36:01 +01002106 startVoice();
2107 }
2108
2109 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002110 try {
2111 final SearchManager searchManager =
2112 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2113 ComponentName activityName = searchManager.getGlobalSearchActivity();
2114 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002115 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002116 if (activityName != null) {
2117 intent.setPackage(activityName.getPackageName());
2118 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002119 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002120 } catch (ActivityNotFoundException e) {
2121 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002122 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002123 startActivitySafely(null, intent, "onClickVoiceButton");
2124 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002125 }
2126
2127 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002128 * Event handler for the "grid" button that appears on the home screen, which
2129 * enters all apps mode.
2130 *
2131 * @param v The view that was clicked.
2132 */
2133 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002134 showAllApps(true);
2135 }
2136
2137 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002138 // Provide the same haptic feedback that the system offers for virtual keys.
2139 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002140 }
2141
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002142 public void onClickAppMarketButton(View v) {
2143 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002144 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002145 } else {
2146 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002147 }
2148 }
2149
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002150 void startApplicationDetailsActivity(ComponentName componentName) {
2151 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002152 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2153 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002154 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002155 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002156 }
2157
Patrick Dubroy5539af72010-09-07 15:22:01 -07002158 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2159 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2160 // System applications cannot be installed. For now, show a toast explaining that.
2161 // We may give them the option of disabling apps this way.
2162 int messageId = R.string.uninstall_system_app_text;
2163 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2164 } else {
2165 String packageName = appInfo.componentName.getPackageName();
2166 String className = appInfo.componentName.getClassName();
2167 Intent intent = new Intent(
2168 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002169 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2170 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002171 startActivity(intent);
2172 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002173 }
2174
Michael Jurka86a720e2012-05-09 11:23:23 -07002175 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002179 // Only launch using the new animation if the shortcut has not opted out (this is a
2180 // private contract between launcher and may be ignored in the future).
2181 boolean useLaunchAnimation = (v != null) &&
2182 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2183 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002184 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2185 v.getMeasuredWidth(), v.getMeasuredHeight());
2186
2187 startActivity(intent, opts.toBundle());
2188 } else {
2189 startActivity(intent);
2190 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002191 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 } catch (SecurityException e) {
2193 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002194 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002196 "or use the exported attribute for this activity. "
2197 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002199 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002201
Michael Jurka86a720e2012-05-09 11:23:23 -07002202 boolean startActivitySafely(View v, Intent intent, Object tag) {
2203 boolean success = false;
2204 try {
2205 success = startActivity(v, intent, tag);
2206 } catch (ActivityNotFoundException e) {
2207 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2208 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2209 }
2210 return success;
2211 }
2212
Romain Guy8e633c52010-03-04 12:51:36 -08002213 void startActivityForResultSafely(Intent intent, int requestCode) {
2214 try {
2215 startActivityForResult(intent, requestCode);
2216 } catch (ActivityNotFoundException e) {
2217 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2218 } catch (SecurityException e) {
2219 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2220 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2221 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2222 "or use the exported attribute for this activity.", e);
2223 }
2224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225
Adam Cohena9cf38f2011-05-02 15:36:58 -07002226 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002227 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002228 Folder openFolder = mWorkspace.getFolderForTag(info);
2229
2230 // If the folder info reports that the associated folder is open, then verify that
2231 // it is actually opened. There have been a few instances where this gets out of sync.
2232 if (info.opened && openFolder == null) {
2233 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002235 info.opened = false;
2236 }
2237
Adam Cohenfb91f302012-06-11 15:45:18 -07002238 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 // Close any open folder
2240 closeFolder();
2241 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002242 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 } else {
2244 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 int folderScreen;
2246 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002247 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 // .. and close it
2249 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002250 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 // Close any folder open on the current screen
2252 closeFolder();
2253 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002254 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 }
2256 }
2257 }
2258 }
2259
Adam Cohen268c4752012-06-06 17:47:33 -07002260 /**
2261 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2262 * in the DragLayer in the exact absolute location of the original FolderIcon.
2263 */
2264 private void copyFolderIconToImage(FolderIcon fi) {
2265 final int width = fi.getMeasuredWidth();
2266 final int height = fi.getMeasuredHeight();
2267
2268 // Lazy load ImageView, Bitmap and Canvas
2269 if (mFolderIconImageView == null) {
2270 mFolderIconImageView = new ImageView(this);
2271 }
2272 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2273 mFolderIconBitmap.getHeight() != height) {
2274 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2275 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2276 }
2277
2278 DragLayer.LayoutParams lp;
2279 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2280 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2281 } else {
2282 lp = new DragLayer.LayoutParams(width, height);
2283 }
2284
Adam Cohen307fe232012-08-16 17:55:58 -07002285 // The layout from which the folder is being opened may be scaled, adjust the starting
2286 // view size by this scale factor.
2287 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002288 lp.customPosition = true;
2289 lp.x = mRectForFolderAnimation.left;
2290 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002291 lp.width = (int) (scale * width);
2292 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002293
2294 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2295 fi.draw(mFolderIconCanvas);
2296 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002297 if (fi.getFolder() != null) {
2298 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2299 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002300 }
Adam Cohen268c4752012-06-06 17:47:33 -07002301 // Just in case this image view is still in the drag layer from a previous animation,
2302 // we remove it and re-add it.
2303 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2304 mDragLayer.removeView(mFolderIconImageView);
2305 }
2306 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002307 if (fi.getFolder() != null) {
2308 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002309 }
Adam Cohen268c4752012-06-06 17:47:33 -07002310 }
2311
Adam Cohen2801caf2011-05-13 20:57:39 -07002312 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002313 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002314 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2315 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2316 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2317
Adam Cohenc51934b2011-07-26 21:07:43 -07002318 FolderInfo info = (FolderInfo) fi.getTag();
2319 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2320 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002321 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2322 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002323 }
2324
Adam Cohen268c4752012-06-06 17:47:33 -07002325 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2326 copyFolderIconToImage(fi);
2327 fi.setVisibility(View.INVISIBLE);
2328
Michael Jurka2ecf9952012-06-18 12:52:28 -07002329 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002330 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002331 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2332 oa.start();
2333 }
2334
Adam Cohen268c4752012-06-06 17:47:33 -07002335 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002336 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002337 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2338 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2339 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2340
Adam Cohen268c4752012-06-06 17:47:33 -07002341 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002342
Adam Cohen268c4752012-06-06 17:47:33 -07002343 // We remove and re-draw the FolderIcon in-case it has changed
2344 mDragLayer.removeView(mFolderIconImageView);
2345 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002346 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002347 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002348 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002349 oa.addListener(new AnimatorListenerAdapter() {
2350 @Override
2351 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002352 if (cl != null) {
2353 cl.clearFolderLeaveBehind();
2354 // Remove the ImageView copy of the FolderIcon and make the original visible.
2355 mDragLayer.removeView(mFolderIconImageView);
2356 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002357 }
2358 }
2359 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002360 oa.start();
2361 }
2362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002364 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 * is animated relative to the specified View. If the View is null, no animation
2366 * is played.
2367 *
2368 * @param folderInfo The FolderInfo describing the folder to open.
2369 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002370 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002371 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002372 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373
Adam Cohena9cf38f2011-05-02 15:36:58 -07002374 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002375
Adam Cohen4554ee12011-08-03 16:13:21 -07002376 // Just verify that the folder hasn't already been added to the DragLayer.
2377 // There was a one-off crash where the folder had a parent already.
2378 if (folder.getParent() == null) {
2379 mDragLayer.addView(folder);
2380 mDragController.addDropTarget((DropTarget) folder);
2381 } else {
2382 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2383 folder.getParent() + ").");
2384 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002385 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002386 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002387
2388 // Notify the accessibility manager that this folder "window" has appeared and occluded
2389 // the workspace items
2390 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2391 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002392 }
2393
2394 public void closeFolder() {
2395 Folder folder = mWorkspace.getOpenFolder();
2396 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002397 if (folder.isEditingName()) {
2398 folder.dismissEditingName();
2399 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002400 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002401
2402 // Dismiss the folder cling
2403 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002404 }
2405 }
2406
2407 void closeFolder(Folder folder) {
2408 folder.getInfo().opened = false;
2409
2410 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2411 if (parent != null) {
2412 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2413 shrinkAndFadeInFolderIcon(fi);
2414 }
2415 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002416
2417 // Notify the accessibility manager that this folder "window" has disappeard and no
2418 // longer occludeds the workspace items
2419 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 }
2421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002423 if (!isDraggingEnabled()) return false;
2424 if (isWorkspaceLocked()) return false;
2425 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426
2427 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002428 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 }
2430
Michael Jurka0280c3b2010-09-17 15:00:07 -07002431 resetAddInfo();
2432 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002434 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 return true;
2436 }
2437
Winson Chung3d503fb2011-07-13 17:25:49 -07002438 // The hotseat touch handling does not go through Workspace, and we always allow long press
2439 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002440 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002441 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2442 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002443 if (itemUnderLongClick == null) {
2444 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002445 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2446 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002447 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002448 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2449 mWorkspace.startReordering();
2450 } else {
2451 startWallpaper();
2452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002454 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002456 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 }
2458 }
2459 }
2460 return true;
2461 }
2462
Winson Chung3d503fb2011-07-13 17:25:49 -07002463 boolean isHotseatLayout(View layout) {
2464 return mHotseat != null && layout != null &&
2465 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2466 }
2467 Hotseat getHotseat() {
2468 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002469 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002470 SearchDropTargetBar getSearchBar() {
2471 return mSearchDropTargetBar;
2472 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002473
Winson Chung3d503fb2011-07-13 17:25:49 -07002474 /**
2475 * Returns the CellLayout of the specified container at the specified screen.
2476 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002477 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002478 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2479 if (mHotseat != null) {
2480 return mHotseat.getLayout();
2481 } else {
2482 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002483 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002484 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002485 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002486 }
2487 }
2488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 Workspace getWorkspace() {
2490 return mWorkspace;
2491 }
2492
Daniel Sandler843e8602010-06-07 14:59:01 -04002493 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002494 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002495 }
2496
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002497 /**
2498 * Helper method for the cameraZoomIn/cameraZoomOut animations
2499 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002500 * @param scaleFactor The scale factor used for the zoom
2501 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002502 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002503 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002504 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002505 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002506
Winson Chung18f41f82012-05-09 13:28:10 -07002507 void disableWallpaperIfInAllApps() {
2508 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002509 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002510 if (mAppsCustomizeTabHost != null &&
2511 !mAppsCustomizeTabHost.isTransitioning()) {
2512 updateWallpaperVisibility(false);
2513 }
2514 }
2515 }
2516
Craig Mautner360310b2012-10-26 15:13:08 -07002517 private void setWorkspaceBackground(boolean workspace) {
2518 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002519 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002520 }
2521
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002522 void updateWallpaperVisibility(boolean visible) {
2523 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2524 int curflags = getWindow().getAttributes().flags
2525 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2526 if (wpflags != curflags) {
2527 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2528 }
Craig Mautner360310b2012-10-26 15:13:08 -07002529 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002530 }
2531
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002532 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2533 if (v instanceof LauncherTransitionable) {
2534 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2535 }
2536 }
2537
Michael Jurkabed61d22012-02-14 22:51:29 -08002538 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2539 if (v instanceof LauncherTransitionable) {
2540 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2541 }
Winson Chung70442722012-02-10 15:43:22 -08002542
2543 // Update the workspace transition step as well
2544 dispatchOnLauncherTransitionStep(v, 0f);
2545 }
2546
2547 private void dispatchOnLauncherTransitionStep(View v, float t) {
2548 if (v instanceof LauncherTransitionable) {
2549 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2550 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002551 }
2552
2553 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2554 if (v instanceof LauncherTransitionable) {
2555 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2556 }
Winson Chung70442722012-02-10 15:43:22 -08002557
2558 // Update the workspace transition step as well
2559 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002560 }
2561
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002562 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002563 * Things to test when changing the following seven functions.
2564 * - Home from workspace
2565 * - from center screen
2566 * - from other screens
2567 * - Home from all apps
2568 * - from center screen
2569 * - from other screens
2570 * - Back from all apps
2571 * - from center screen
2572 * - from other screens
2573 * - Launch app from workspace and quit
2574 * - with back
2575 * - with home
2576 * - Launch app from all apps and quit
2577 * - with back
2578 * - with home
2579 * - Go to a screen that's not the default, then all
2580 * apps, and launch and app, and go back
2581 * - with back
2582 * -with home
2583 * - On workspace, long press power and go back
2584 * - with back
2585 * - with home
2586 * - On all apps, long press power and go back
2587 * - with back
2588 * - with home
2589 * - On workspace, power off
2590 * - On all apps, power off
2591 * - Launch an app and turn off the screen while in that app
2592 * - Go back with home key
2593 * - Go back with back key TODO: make this not go to workspace
2594 * - From all apps
2595 * - From workspace
2596 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2597 * - From all apps
2598 * - From the center workspace
2599 * - From another workspace
2600 */
2601
2602 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002603 * Zoom the camera out from the workspace to reveal 'toView'.
2604 * Assumes that the view to show is anchored at either the very top or very bottom
2605 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002606 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002607 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002608 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002609 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002610 mStateAnimation.cancel();
2611 mStateAnimation = null;
2612 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002613 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002614
Winson Chungf0ea4d32011-06-06 14:27:16 -07002615 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2616 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2617 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002618 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002619 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002620 final int startDelay =
2621 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002622
Michael Jurkab3e22d92011-10-31 15:58:33 -07002623 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002624
Michael Jurkad74c9842011-07-10 12:44:21 -07002625 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002626 Animator workspaceAnim =
2627 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002628
2629 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002630 toView.setScaleX(scale);
2631 toView.setScaleY(scale);
2632 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2633 scaleAnim.
2634 scaleX(1f).scaleY(1f).
2635 setDuration(duration).
2636 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002637
Winson Chungf0ea4d32011-06-06 14:27:16 -07002638 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002639 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002640 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002641 .ofFloat(toView, "alpha", 0f, 1f)
2642 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002643 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002644 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2645 @Override
2646 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002647 if (animation == null) {
2648 throw new RuntimeException("animation is null");
2649 }
Winson Chung70442722012-02-10 15:43:22 -08002650 float t = (Float) animation.getAnimatedValue();
2651 dispatchOnLauncherTransitionStep(fromView, t);
2652 dispatchOnLauncherTransitionStep(toView, t);
2653 }
2654 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002655
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002656 // toView should appear right at the end of the workspace shrink
2657 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002658 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002659 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002660 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002661
2662 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002663 boolean animationCancelled = false;
2664
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002665 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002666 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002667 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002668 // Prepare the position
2669 toView.setTranslationX(0.0f);
2670 toView.setTranslationY(0.0f);
2671 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002672 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002673 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002674 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002675 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002676 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2677 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002678
Daniel Sandlere4f98912013-06-25 15:13:26 -04002679 if (mWorkspace != null
2680 && !springLoaded
2681 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002682 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002683 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002684 if (!animationCancelled) {
2685 updateWallpaperVisibility(false);
2686 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002687
2688 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002689 if (mSearchDropTargetBar != null) {
2690 mSearchDropTargetBar.hideSearchBar(false);
2691 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002692 }
2693
Adam Cohencff6af82011-09-13 14:51:53 -07002694 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002695 public void onAnimationCancel(Animator animation) {
2696 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002697 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002698 });
2699
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002700 if (workspaceAnim != null) {
2701 mStateAnimation.play(workspaceAnim);
2702 }
Michael Jurka1899a362011-11-03 13:50:45 -07002703
2704 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002705
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002706 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2707 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002708
2709 // If any of the objects being animated haven't been measured/laid out
2710 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002711 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002712 (mWorkspace.getMeasuredWidth() == 0) ||
2713 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002714 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002715 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002716
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002717 final AnimatorSet stateAnimation = mStateAnimation;
2718 final Runnable startAnimRunnable = new Runnable() {
2719 public void run() {
2720 // Check that mStateAnimation hasn't changed while
2721 // we waited for a layout/draw pass
2722 if (mStateAnimation != stateAnimation)
2723 return;
2724 setPivotsForZoom(toView, scale);
2725 dispatchOnLauncherTransitionStart(fromView, animated, false);
2726 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002727 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002728 }
2729 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002730 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002731 final ViewTreeObserver observer = toView.getViewTreeObserver();
2732 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2733 public void onGlobalLayout() {
2734 startAnimRunnable.run();
2735 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2736 }
2737 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002738 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002739 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002740 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002741 } else {
2742 toView.setTranslationX(0.0f);
2743 toView.setTranslationY(0.0f);
2744 toView.setScaleX(1.0f);
2745 toView.setScaleY(1.0f);
2746 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002747 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002748
Daniel Sandlere4f98912013-06-25 15:13:26 -04002749 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002750 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002751
2752 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002753 if (mSearchDropTargetBar != null) {
2754 mSearchDropTargetBar.hideSearchBar(false);
2755 }
Michael Jurkaabded662011-03-04 12:06:57 -08002756 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002757 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002758 dispatchOnLauncherTransitionStart(fromView, animated, false);
2759 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002760 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002761 dispatchOnLauncherTransitionStart(toView, animated, false);
2762 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002763 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002764 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 }
2766
2767 /**
2768 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002769 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002770 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002772 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2773 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002774
Michael Jurkab3e22d92011-10-31 15:58:33 -07002775 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002776 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002777 mStateAnimation.cancel();
2778 mStateAnimation = null;
2779 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002780 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002781
Winson Chungf0ea4d32011-06-06 14:27:16 -07002782 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002783 final int fadeOutDuration =
2784 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002785 final float scaleFactor = (float)
2786 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2787 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002788 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002789 Animator workspaceAnim = null;
2790
2791 if (toState == State.WORKSPACE) {
2792 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2793 workspaceAnim = mWorkspace.getChangeStateAnimation(
2794 Workspace.State.NORMAL, animated, stagger);
2795 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2796 workspaceAnim = mWorkspace.getChangeStateAnimation(
2797 Workspace.State.SPRING_LOADED, animated);
2798 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002799
Michael Jurkab3e22d92011-10-31 15:58:33 -07002800 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002801 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002802 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002804 final LauncherViewPropertyAnimator scaleAnim =
2805 new LauncherViewPropertyAnimator(fromView);
2806 scaleAnim.
2807 scaleX(scaleFactor).scaleY(scaleFactor).
2808 setDuration(duration).
2809 setInterpolator(new Workspace.ZoomInInterpolator());
2810
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002811 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002812 .ofFloat(fromView, "alpha", 1f, 0f)
2813 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002814 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002815 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2816 @Override
2817 public void onAnimationUpdate(ValueAnimator animation) {
2818 float t = 1f - (Float) animation.getAnimatedValue();
2819 dispatchOnLauncherTransitionStep(fromView, t);
2820 dispatchOnLauncherTransitionStep(toView, t);
2821 }
2822 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002823
Michael Jurka2ecf9952012-06-18 12:52:28 -07002824 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002825
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002826 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2827 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002828 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002829
2830 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002831 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002832 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002833 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002834 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002835 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2836 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002837 if (onCompleteRunnable != null) {
2838 onCompleteRunnable.run();
2839 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002840 mAppsCustomizeContent.updateCurrentPageScroll();
2841 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002842 }
2843 });
2844
Winson Chungf0ea4d32011-06-06 14:27:16 -07002845 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002846 if (workspaceAnim != null) {
2847 mStateAnimation.play(workspaceAnim);
2848 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002849 dispatchOnLauncherTransitionStart(fromView, animated, true);
2850 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002851 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002852 } else {
2853 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002854 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002855 dispatchOnLauncherTransitionStart(fromView, animated, true);
2856 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002857 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002858 dispatchOnLauncherTransitionStart(toView, animated, true);
2859 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002860 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002861 }
2862
Michael Jurkae326f182011-11-21 14:05:46 -08002863 @Override
2864 public void onTrimMemory(int level) {
2865 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002866 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002867 mAppsCustomizeTabHost.onTrimMemory();
2868 }
2869 }
2870
Winson Chung18f41f82012-05-09 13:28:10 -07002871 @Override
2872 public void onWindowFocusChanged(boolean hasFocus) {
2873 if (!hasFocus) {
2874 // When another window occludes launcher (like the notification shade, or recents),
2875 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2876 updateWallpaperVisibility(true);
2877 } else {
2878 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002879 mWorkspace.postDelayed(new Runnable() {
2880 @Override
2881 public void run() {
2882 disableWallpaperIfInAllApps();
2883 }
2884 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002885 }
2886 }
2887
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002888 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002889 showWorkspace(animated, null);
2890 }
2891
2892 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002893 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002894 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002895 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002896 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002897
Winson Chungc7d2b602012-05-16 17:02:20 -07002898 // Show the search bar (only animate if we were showing the drop target bar in spring
2899 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002900 if (mSearchDropTargetBar != null) {
2901 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2902 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002903
Michael Jurkab737ee62011-11-15 15:57:22 -08002904 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002905 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002906
2907 // Set focus to the AppsCustomize button
2908 if (mAllAppsButton != null) {
2909 mAllAppsButton.requestFocus();
2910 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002911 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002912
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002913 // Change the state *after* we've called all the transition code
2914 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002915
Winson Chung5fb63472011-02-02 17:03:37 -08002916 // Resume the auto-advance of widgets
2917 mUserPresent = true;
2918 updateRunning();
2919
alanv1d4fde62012-10-17 13:15:47 -07002920 // Send an accessibility event to announce the context change
2921 getWindow().getDecorView()
2922 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002923
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002924 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002925 }
2926
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002927 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002928 }
2929
Michael Jurkab3e22d92011-10-31 15:58:33 -07002930 void showAllApps(boolean animated) {
2931 if (mState != State.WORKSPACE) return;
2932
2933 showAppsCustomizeHelper(animated, false);
2934 mAppsCustomizeTabHost.requestFocus();
2935
Michael Jurkab3e22d92011-10-31 15:58:33 -07002936 // Change the state *after* we've called all the transition code
2937 mState = State.APPS_CUSTOMIZE;
2938
2939 // Pause the auto-advance of widgets until we are out of AllApps
2940 mUserPresent = false;
2941 updateRunning();
2942 closeFolder();
2943
2944 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002945 getWindow().getDecorView()
2946 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002947 }
2948
Adam Cohen7777d962011-08-18 18:58:38 -07002949 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002950 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002951 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002952 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002953 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002954 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002955 }
Adam Cohen7777d962011-08-18 18:58:38 -07002956
Adam Cohened66b2b2012-01-23 17:28:51 -08002957 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2958 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002959 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2960
Winson Chunge7a03942011-08-05 15:05:12 -07002961 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002962 @Override
2963 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002964 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002965 // Before we show workspace, hide all apps again because
2966 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2967 // clean up our state transition functions
2968 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002969 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002970 } else {
2971 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002972 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002973 }
2974 }, (extendedDelay ?
2975 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2976 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2977 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002978
Michael Jurkad3ef3062010-11-23 16:23:58 -08002979 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002980 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002981 final boolean animated = true;
2982 final boolean springLoaded = true;
2983 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002984 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002985 }
2986 // Otherwise, we are not in spring loaded mode, so don't do anything.
2987 }
2988
Michael Jurkab737ee62011-11-15 15:57:22 -08002989 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07002990 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002991 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08002992 }
2993 }
2994
2995 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07002996 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002997 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08002998 if (mDividerAnimator != null) {
2999 mDividerAnimator.cancel();
3000 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003001 mDividerAnimator = null;
3002 }
3003 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003004 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003005 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003006 int duration = 0;
3007 if (mSearchDropTargetBar != null) {
3008 duration = mSearchDropTargetBar.getTransitionInDuration();
3009 }
3010 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003011 mDividerAnimator.start();
3012 }
3013 }
3014 }
3015
Michael Jurkab3e22d92011-10-31 15:58:33 -07003016 void lockAllApps() {
3017 // TODO
3018 }
3019
3020 void unlockAllApps() {
3021 // TODO
3022 }
3023
Winson Chungf0ea4d32011-06-06 14:27:16 -07003024 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003025 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003026 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003027 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003028 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003029 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003030 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003031 int duration = 0;
3032 if (mSearchDropTargetBar != null) {
3033 duration = mSearchDropTargetBar.getTransitionInDuration();
3034 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003035 mHotseat.animate().alpha(1f).setDuration(duration);
3036 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003037 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003038 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003039 }
3040 }
3041 }
3042
3043 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003044 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003045 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003046 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003047 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003048 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003049 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003050 int duration = 0;
3051 if (mSearchDropTargetBar != null) {
3052 duration = mSearchDropTargetBar.getTransitionOutDuration();
3053 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003054 mHotseat.animate().alpha(0f).setDuration(duration);
3055 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003056 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003057 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003058 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003059 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003060 }
3061
Patrick Dubroy5f445422011-02-18 14:35:21 -08003062 /**
3063 * Add an item from all apps or customize onto the given workspace screen.
3064 * If layout is null, add to the current screen.
3065 */
3066 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003067 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003068 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003069 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003070 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003071
Winson Chungdff8ebb2011-09-08 17:25:31 -07003072 /** Maps the current orientation to an index for referencing orientation correct global icons */
3073 private int getCurrentOrientationIndexForGlobalIcons() {
3074 // default - 0, landscape - 1
3075 switch (getResources().getConfiguration().orientation) {
3076 case Configuration.ORIENTATION_LANDSCAPE:
3077 return 1;
3078 default:
3079 return 0;
3080 }
3081 }
3082
Michael Jurkaae65ee32012-04-30 11:45:54 -07003083 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003084 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003085 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003086 // Look for the toolbar icon specified in the activity meta-data
3087 Bundle metaData = packageManager.getActivityInfo(
3088 activityName, PackageManager.GET_META_DATA).metaData;
3089 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003090 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003091 if (iconResId != 0) {
3092 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003093 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003094 }
3095 }
3096 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003097 // This can happen if the activity defines an invalid drawable
3098 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3099 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003100 } catch (Resources.NotFoundException nfe) {
3101 // This can happen if the activity defines an invalid drawable
3102 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3103 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003104 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003105 return null;
3106 }
3107
3108 // if successful in getting icon, return it; otherwise, set button to use default drawable
3109 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003110 int buttonId, ComponentName activityName, int fallbackDrawableId,
3111 String toolbarResourceName) {
3112 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003113 Resources r = getResources();
3114 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3115 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003116
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003117 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003118 // If we were unable to find the icon via the meta-data, use a generic one
3119 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003120 toolbarIcon = r.getDrawable(fallbackDrawableId);
3121 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003122 if (button != null) {
3123 button.setCompoundDrawables(toolbarIcon, null, null, null);
3124 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003125 return null;
3126 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003127 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003128 if (button != null) {
3129 button.setCompoundDrawables(toolbarIcon, null, null, null);
3130 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003131 return toolbarIcon.getConstantState();
3132 }
3133 }
3134
3135 // if successful in getting icon, return it; otherwise, set button to use default drawable
3136 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003137 int buttonId, ComponentName activityName, int fallbackDrawableId,
3138 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003139 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003140 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003141
Michael Jurka19e0fc52011-07-22 18:00:21 -07003142 if (button != null) {
3143 // If we were unable to find the icon via the meta-data, use a
3144 // generic one
3145 if (toolbarIcon == null) {
3146 button.setImageResource(fallbackDrawableId);
3147 } else {
3148 button.setImageDrawable(toolbarIcon);
3149 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003150 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003151
3152 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3153
Michael Jurka0423dcf2010-10-05 14:56:18 -07003154 }
3155
Winson Chung1b884092012-06-08 17:13:02 -07003156 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003157 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003158 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003159 }
3160
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003161 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003162 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003163 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003164 }
3165
Winson Chungbb185bd2011-11-21 12:31:42 -08003166 private void invalidatePressedFocusedStates(View container, View button) {
3167 if (container instanceof HolographicLinearLayout) {
3168 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3169 layout.invalidatePressedFocusedStates();
3170 } else if (button instanceof HolographicImageView) {
3171 HolographicImageView view = (HolographicImageView) button;
3172 view.invalidatePressedFocusedStates();
3173 }
3174 }
3175
Winson Chungc51db6a2011-10-05 11:44:49 -07003176 private boolean updateGlobalSearchIcon() {
3177 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003178 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003179 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003180 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003181
Winson Chung1cad91e2011-05-25 17:41:01 -07003182 final SearchManager searchManager =
3183 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3184 ComponentName activityName = searchManager.getGlobalSearchActivity();
3185 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003186 int coi = getCurrentOrientationIndexForGlobalIcons();
3187 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003188 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3189 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3190 if (sGlobalSearchIcon[coi] == null) {
3191 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3192 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3193 TOOLBAR_ICON_METADATA_NAME);
3194 }
3195
Winson Chungc51db6a2011-10-05 11:44:49 -07003196 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3197 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003198 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003199 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003200 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003201 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003202 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3203 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3204 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003205 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003206 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003207 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003208 }
3209 }
3210
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003211 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003212 final View searchButtonContainer = findViewById(R.id.search_button_container);
3213 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003214 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003215 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003216 }
3217
Winson Chungc51db6a2011-10-05 11:44:49 -07003218 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003219 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003220 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003221
Winson Chungc51db6a2011-10-05 11:44:49 -07003222 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003223 final SearchManager searchManager =
3224 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3225 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3226
3227 ComponentName activityName = null;
3228 if (globalSearchActivity != null) {
3229 // Check if the global search activity handles voice search
3230 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3231 intent.setPackage(globalSearchActivity.getPackageName());
3232 activityName = intent.resolveActivity(getPackageManager());
3233 }
3234
3235 if (activityName == null) {
3236 // Fallback: check if an activity other than the global search activity
3237 // resolves this
3238 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3239 activityName = intent.resolveActivity(getPackageManager());
3240 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003241 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003242 int coi = getCurrentOrientationIndexForGlobalIcons();
3243 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003244 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3245 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3246 if (sVoiceSearchIcon[coi] == null) {
3247 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3248 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3249 TOOLBAR_ICON_METADATA_NAME);
3250 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003251 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003252 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003253 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003254 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003255 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003256 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003257 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003258 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003259 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003260 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003261 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003262 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003263
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003264 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003265 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3266 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003267 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003268 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003269 }
3270
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003271 public void setVoiceButtonProxyVisible(boolean visible) {
3272 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3273 if (voiceButtonProxy != null) {
3274 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3275 }
3276 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003277 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003278 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003279 */
3280 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003281 final View marketButton = findViewById(R.id.market_button);
3282 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3283 // Find the app market activity by resolving an intent.
3284 // (If multiple app markets are installed, it will return the ResolverActivity.)
3285 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003286 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003287 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003288 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003289 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003290 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3291 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003292 marketButton.setVisibility(View.VISIBLE);
3293 } else {
3294 // We should hide and disable the view so that we don't try and restore the visibility
3295 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3296 marketButton.setVisibility(View.GONE);
3297 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003298 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003299 }
3300
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003301 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003302 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3303 Resources r = getResources();
3304 Drawable marketIconDrawable = d.newDrawable(r);
3305 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3306 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3307 marketIconDrawable.setBounds(0, 0, w, h);
3308
3309 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003310 }
3311
Michael Jurkad7c28052012-04-27 15:43:36 -07003312 @Override
3313 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003314 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003315 final List<CharSequence> text = event.getText();
3316 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003317 // Populate event with a fake title based on the current state.
3318 if (mState == State.APPS_CUSTOMIZE) {
3319 text.add(getString(R.string.all_apps_button_label));
3320 } else {
3321 text.add(getString(R.string.all_apps_home_button_label));
3322 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003323 return result;
3324 }
3325
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003326 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003327 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003328 */
3329 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3330 @Override
3331 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003332 closeSystemDialogs();
3333 }
3334 }
3335
3336 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003337 * Receives notifications whenever the appwidgets are reset.
3338 */
3339 private class AppWidgetResetObserver extends ContentObserver {
3340 public AppWidgetResetObserver() {
3341 super(new Handler());
3342 }
3343
3344 @Override
3345 public void onChange(boolean selfChange) {
3346 onAppWidgetReset();
3347 }
3348 }
3349
3350 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003351 * If the activity is currently paused, signal that we need to run the passed Runnable
3352 * in onResume.
3353 *
3354 * This needs to be called from incoming places where resources might have been loaded
3355 * while we are paused. That is becaues the Configuration might be wrong
3356 * when we're not running, and if it comes back to what it was when we
3357 * were paused, we are not restarted.
3358 *
3359 * Implementation of the method from LauncherModel.Callbacks.
3360 *
3361 * @return true if we are currently paused. The caller might be able to
3362 * skip some work in that case since we will come back again.
3363 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003364 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003365 if (mPaused) {
3366 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003367 if (deletePreviousRunnables) {
3368 while (mOnResumeCallbacks.remove(run)) {
3369 }
3370 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003371 mOnResumeCallbacks.add(run);
3372 return true;
3373 } else {
3374 return false;
3375 }
3376 }
3377
Michael Jurkac402cd92013-05-20 15:49:32 +02003378 private boolean waitUntilResume(Runnable run) {
3379 return waitUntilResume(run, false);
3380 }
3381
Michael Jurka7607c2f2013-04-03 14:33:19 -07003382 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003383 * If the activity is currently paused, signal that we need to re-run the loader
3384 * in onResume.
3385 *
3386 * This needs to be called from incoming places where resources might have been loaded
3387 * while we are paused. That is becaues the Configuration might be wrong
3388 * when we're not running, and if it comes back to what it was when we
3389 * were paused, we are not restarted.
3390 *
3391 * Implementation of the method from LauncherModel.Callbacks.
3392 *
3393 * @return true if we are currently paused. The caller might be able to
3394 * skip some work in that case since we will come back again.
3395 */
3396 public boolean setLoadOnResume() {
3397 if (mPaused) {
3398 Log.i(TAG, "setLoadOnResume");
3399 mOnResumeNeedsLoad = true;
3400 return true;
3401 } else {
3402 return false;
3403 }
3404 }
3405
3406 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003408 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003410 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003411 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003412 } else {
3413 return SCREEN_COUNT / 2;
3414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003415 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003416
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 /**
3418 * Refreshes the shortcuts shown on the workspace.
3419 *
3420 * Implementation of the method from LauncherModel.Callbacks.
3421 */
3422 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003423 // If we're starting binding all over again, clear any bind calls we'd postponed in
3424 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3425 // from scratch again
3426 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003427
Michael Jurka7607c2f2013-04-03 14:33:19 -07003428 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003429 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003430 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003431 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003432 int count = workspace.getChildCount();
3433 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003434 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003435 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3436 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003438 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003439 if (mHotseat != null) {
3440 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003441 }
3442 }
3443
Adam Cohendcd297f2013-06-18 13:13:40 -07003444 @Override
3445 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003446 bindAddScreens(orderedScreenIds);
3447 mWorkspace.addExtraEmptyScreen();
3448 }
3449
3450 @Override
3451 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003452 int count = orderedScreenIds.size();
3453 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003454 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003455 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003456 }
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 */
Winson Chung64359a52013-07-08 17:17:08 -07003463 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3464 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003465 if (waitUntilResume(new Runnable() {
3466 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003467 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003468 }
3469 })) {
3470 return;
3471 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003472
Winson Chungf0c6ae02012-03-21 16:10:31 -07003473 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3474 Set<String> newApps = new HashSet<String>();
3475 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3476
Winson Chung64359a52013-07-08 17:17:08 -07003477 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3478 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003479 Workspace workspace = mWorkspace;
3480 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003482
3483 // Short circuit if we are loading dock items for a configuration which has no dock
3484 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3485 mHotseat == null) {
3486 continue;
3487 }
3488
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 switch (item.itemType) {
3490 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3491 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003492 ShortcutInfo info = (ShortcutInfo) item;
3493 String uri = info.intent.toUri(0).toString();
3494 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003495
Winson Chung64359a52013-07-08 17:17:08 -07003496 /*
3497 * TODO: FIX collision case
3498 */
Winson Chungce376632013-07-11 16:12:41 -07003499 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3500 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3501 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3502 throw new RuntimeException("OCCUPIED");
3503 }
Winson Chung64359a52013-07-08 17:17:08 -07003504 }
3505
Adam Cohendcd297f2013-06-18 13:13:40 -07003506 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3507 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003508 boolean animateIconUp = false;
3509 synchronized (newApps) {
3510 if (newApps.contains(uri)) {
3511 animateIconUp = newApps.remove(uri);
3512 }
3513 }
Winson Chung64359a52013-07-08 17:17:08 -07003514 if (forceAnimateIcons) {
3515 // Animate all the applications up now
3516 shortcut.setAlpha(0f);
3517 shortcut.setScaleX(0f);
3518 shortcut.setScaleY(0f);
3519 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3520 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003521 // Prepare the view to be animated up
3522 shortcut.setAlpha(0f);
3523 shortcut.setScaleX(0f);
3524 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003525 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003526 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3527 mNewShortcutAnimateViews.add(shortcut);
3528 }
3529 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003531 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003532 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003533 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003534 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003535 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3536 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003537 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003539 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003540
Winson Chung64359a52013-07-08 17:17:08 -07003541 if (forceAnimateIcons) {
3542 // We post the animation slightly delayed to prevent slowdowns when we are loading
3543 // right after we return to launcher.
3544 mWorkspace.postDelayed(new Runnable() {
3545 public void run() {
3546 anim.playTogether(bounceAnims);
3547 anim.start();
3548 }
3549 }, NEW_APPS_ANIMATION_DELAY);
3550 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003551 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003552 }
3553
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 /**
3555 * Implementation of the method from LauncherModel.Callbacks.
3556 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003557 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3558 if (waitUntilResume(new Runnable() {
3559 public void run() {
3560 bindFolders(folders);
3561 }
3562 })) {
3563 return;
3564 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003565 sFolders.clear();
3566 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003567 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568
3569 /**
3570 * Add the views for a widget to the workspace.
3571 *
3572 * Implementation of the method from LauncherModel.Callbacks.
3573 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003574 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3575 if (waitUntilResume(new Runnable() {
3576 public void run() {
3577 bindAppWidget(item);
3578 }
3579 })) {
3580 return;
3581 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003582
Daniel Sandler843e8602010-06-07 14:59:01 -04003583 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3584 if (DEBUG_WIDGETS) {
3585 Log.d(TAG, "bindAppWidget: " + item);
3586 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003587 final Workspace workspace = mWorkspace;
3588
3589 final int appWidgetId = item.appWidgetId;
3590 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003591 if (DEBUG_WIDGETS) {
3592 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3593 }
3594
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3596
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003598 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599
Adam Cohendcd297f2013-06-18 13:13:40 -07003600 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003602 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3603
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 workspace.requestLayout();
3605
Daniel Sandler843e8602010-06-07 14:59:01 -04003606 if (DEBUG_WIDGETS) {
3607 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3608 + (SystemClock.uptimeMillis()-start) + "ms");
3609 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003610 }
3611
Adam Cohen1462de32012-07-24 22:34:36 -07003612 public void onPageBoundSynchronously(int page) {
3613 mSynchronouslyBoundPages.add(page);
3614 }
3615
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 /**
3617 * Callback saying that there aren't any more items to bind.
3618 *
3619 * Implementation of the method from LauncherModel.Callbacks.
3620 */
Adam Cohene25af792013-06-06 23:08:25 -07003621 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003622 if (waitUntilResume(new Runnable() {
3623 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003624 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003625 }
3626 })) {
3627 return;
3628 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003629 if (mSavedState != null) {
3630 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003631 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003632 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 mSavedState = null;
3634 }
3635
Adam Cohen1462de32012-07-24 22:34:36 -07003636 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003638 // If we received the result of any pending adds while the loader was running (e.g. the
3639 // widget configuration forced an orientation change), process them now.
3640 for (int i = 0; i < sPendingAddList.size(); i++) {
3641 completeAdd(sPendingAddList.get(i));
3642 }
3643 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003644
Winson Chungc51db6a2011-10-05 11:44:49 -07003645 // Update the market app icon as necessary (the other icons will be managed in response to
3646 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003647 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003648
Winson Chungf0c6ae02012-03-21 16:10:31 -07003649 // Animate up any icons as necessary
3650 if (mVisible || mWorkspaceLoading) {
3651 Runnable newAppsRunnable = new Runnable() {
3652 @Override
3653 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003654 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003655 }
3656 };
Winson Chunga2413752012-04-03 14:22:34 -07003657
Adam Cohendcd297f2013-06-18 13:13:40 -07003658 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3659 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003660 if (canRunNewAppsAnimation()) {
3661 // If the user has not interacted recently, then either snap to the new page to show
3662 // the new-apps animation or just run them if they are to appear on the current page
3663 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003664 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003665 } else {
3666 runNewAppsAnimation(false);
3667 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003668 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003669 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003670 // are on another page (or just normally if they are added to the current page)
3671 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003672 }
3673 }
3674
3675 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003676 if (upgradePath) {
3677 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003678 mWorkspace.stripDuplicateApps();
3679 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003680 }
Adam Cohen99894d92013-06-14 11:22:59 -07003681
Adam Cohen66a01fd2013-06-11 12:48:00 -07003682 mWorkspace.post(new Runnable() {
3683 @Override
3684 public void run() {
3685 onFinishBindingItems();
3686 }
3687 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003688 }
3689
Winson Chunga2413752012-04-03 14:22:34 -07003690 private boolean canRunNewAppsAnimation() {
3691 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3692 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3693 }
3694
Winson Chungc9168342013-06-26 14:54:55 -07003695 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3696 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3697 PropertyValuesHolder.ofFloat("alpha", 1f),
3698 PropertyValuesHolder.ofFloat("scaleX", 1f),
3699 PropertyValuesHolder.ofFloat("scaleY", 1f));
3700 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3701 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3702 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3703 return bounceAnim;
3704 }
3705
Winson Chungf0c6ae02012-03-21 16:10:31 -07003706 /**
3707 * Runs a new animation that scales up icons that were added while Launcher was in the
3708 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003709 *
3710 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003711 */
Winson Chunga2413752012-04-03 14:22:34 -07003712 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003713 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003714 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003715
3716 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003717 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3718 @Override
3719 public int compare(View a, View b) {
3720 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3721 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3722 int cellCountX = LauncherModel.getCellCountX();
3723 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3724 }
3725 });
Winson Chunga2413752012-04-03 14:22:34 -07003726
3727 // Animate each of the views in place (or show them immediately if requested)
3728 if (immediate) {
3729 for (View v : mNewShortcutAnimateViews) {
3730 v.setAlpha(1f);
3731 v.setScaleX(1f);
3732 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003733 }
Winson Chunga2413752012-04-03 14:22:34 -07003734 } else {
3735 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3736 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003737 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003738 }
3739 anim.playTogether(bounceAnims);
3740 anim.addListener(new AnimatorListenerAdapter() {
3741 @Override
3742 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003743 if (mWorkspace != null) {
3744 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3745 }
Winson Chunga2413752012-04-03 14:22:34 -07003746 }
3747 });
3748 anim.start();
3749 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003750
3751 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003752 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003753 mNewShortcutAnimateViews.clear();
3754 new Thread("clearNewAppsThread") {
3755 public void run() {
3756 mSharedPrefs.edit()
3757 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3758 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3759 .commit();
3760 }
3761 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003762 }
3763
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003764 @Override
3765 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003766 boolean searchVisible = updateGlobalSearchIcon();
3767 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003768 if (mSearchDropTargetBar != null) {
3769 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3770 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003771 }
3772
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003773 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003774 * Add the icons for all apps.
3775 *
3776 * Implementation of the method from LauncherModel.Callbacks.
3777 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003778 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003779 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3780 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003781 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003782 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003783 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003784 }
3785
3786 /**
3787 * A package was updated.
3788 *
3789 * Implementation of the method from LauncherModel.Callbacks.
3790 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003791 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3792 if (waitUntilResume(new Runnable() {
3793 public void run() {
3794 bindAppsUpdated(apps);
3795 }
3796 })) {
3797 return;
3798 }
3799
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003800 if (mWorkspace != null) {
3801 mWorkspace.updateShortcuts(apps);
3802 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003803 }
3804
3805 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003806 * A package was uninstalled. We take both the super set of packageNames
3807 * in addition to specific applications to remove, the reason being that
3808 * this can be called when a package is updated as well. In that scenario,
3809 * we only remove specific components from the workspace, where as
3810 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003811 *
3812 * Implementation of the method from LauncherModel.Callbacks.
3813 */
Winson Chung83892cc2013-05-01 16:53:33 -07003814 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3815 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003816 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003817 if (waitUntilResume(new Runnable() {
3818 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003819 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003820 }
3821 })) {
3822 return;
3823 }
3824
Winson Chung64359a52013-07-08 17:17:08 -07003825 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003826 mWorkspace.removeItemsByPackageName(packageNames);
3827 } else {
3828 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003829 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003830
Winson Chunga1820962011-10-03 16:31:06 -07003831 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003832 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003833 }
Joe Onoratobe386092009-11-17 17:32:16 -08003834
3835 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003836 * A number of packages were updated.
3837 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003838 private ArrayList<Object> mWidgetsAndShortcuts;
3839 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003840 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003841 bindPackagesUpdated(mWidgetsAndShortcuts);
3842 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003843 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003844 };
3845
3846 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3847 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3848 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003849 return;
3850 }
3851
Winson Chung64359a52013-07-08 17:17:08 -07003852 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003853 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003854 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003855 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003856 }
3857
Winson Chung400438b2011-01-16 17:53:48 -08003858 private int mapConfigurationOriActivityInfoOri(int configOri) {
3859 final Display d = getWindowManager().getDefaultDisplay();
3860 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3861 switch (d.getRotation()) {
3862 case Surface.ROTATION_0:
3863 case Surface.ROTATION_180:
3864 // We are currently in the same basic orientation as the natural orientation
3865 naturalOri = configOri;
3866 break;
3867 case Surface.ROTATION_90:
3868 case Surface.ROTATION_270:
3869 // We are currently in the other basic orientation to the natural orientation
3870 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3871 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3872 break;
3873 }
3874
3875 int[] oriMap = {
3876 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3877 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3878 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3879 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3880 };
3881 // Since the map starts at portrait, we need to offset if this device's natural orientation
3882 // is landscape.
3883 int indexOffset = 0;
3884 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3885 indexOffset = 1;
3886 }
3887 return oriMap[(d.getRotation() + indexOffset) % 4];
3888 }
Adam Cohen446e9402011-09-15 18:21:21 -07003889
Winson Chung4b919f82012-05-01 10:44:08 -07003890 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003891 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003892 getResources().getBoolean(R.bool.allow_rotation);
3893 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003894 }
Winson Chung4b919f82012-05-01 10:44:08 -07003895 public void lockScreenOrientation() {
3896 if (isRotationEnabled()) {
3897 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3898 .getConfiguration().orientation));
3899 }
3900 }
3901 public void unlockScreenOrientation(boolean immediate) {
3902 if (isRotationEnabled()) {
3903 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003904 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003905 } else {
3906 mHandler.postDelayed(new Runnable() {
3907 public void run() {
3908 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3909 }
3910 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003911 }
Winson Chung4b919f82012-05-01 10:44:08 -07003912 }
Winson Chung400438b2011-01-16 17:53:48 -08003913 }
3914
Winson Chung82f55532011-08-09 14:14:23 -07003915 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003916 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003917 if (DISABLE_CLINGS) {
3918 return false;
3919 }
3920
Brett Chabot2a9e2282011-08-23 15:03:13 -07003921 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003922 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003923
Michael Jurkae233a8b2013-03-19 13:49:20 +01003924 // Restricted secondary users (child mode) will potentially have very few apps
3925 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003926// boolean supportsLimitedUsers =
3927// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3928// Account[] accounts = AccountManager.get(this).getAccounts();
3929// if (supportsLimitedUsers && accounts.length == 0) {
3930// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3931// Bundle restrictions = um.getUserRestrictions();
3932// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3933// return false;
3934// }
3935// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003936 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003937 }
Michael Jurka22143132012-10-04 17:42:38 +02003938
Winson Chung7d7541e2011-09-16 20:14:36 -07003939 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003940 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003941 if (cling != null) {
3942 cling.init(this, positionData);
3943 cling.setVisibility(View.VISIBLE);
3944 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3945 if (animate) {
3946 cling.buildLayer();
3947 cling.setAlpha(0f);
3948 cling.animate()
3949 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003950 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003951 .setDuration(SHOW_CLING_DURATION)
3952 .setStartDelay(delay)
3953 .start();
3954 } else {
3955 cling.setAlpha(1f);
3956 }
Michael Jurka22143132012-10-04 17:42:38 +02003957 cling.setFocusableInTouchMode(true);
3958 cling.post(new Runnable() {
3959 public void run() {
3960 cling.setFocusable(true);
3961 cling.requestFocus();
3962 }
3963 });
3964 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3965 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003966 }
3967 return cling;
3968 }
Michael Jurka22143132012-10-04 17:42:38 +02003969
Winson Chung7d7541e2011-09-16 20:14:36 -07003970 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003971 // To catch cases where siblings of top-level views are made invisible, just check whether
3972 // the cling is directly set to GONE before dismissing it.
3973 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003974 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003975 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003976 anim.addListener(new AnimatorListenerAdapter() {
3977 public void onAnimationEnd(Animator animation) {
3978 cling.setVisibility(View.GONE);
3979 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003980 // We should update the shared preferences on a background thread
3981 new Thread("dismissClingThread") {
3982 public void run() {
3983 SharedPreferences.Editor editor = mSharedPrefs.edit();
3984 editor.putBoolean(flag, true);
3985 editor.commit();
3986 }
3987 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003988 };
3989 });
3990 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003991 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003992 }
3993 }
Michael Jurka22143132012-10-04 17:42:38 +02003994
Winson Chung9d9d74f2011-09-19 11:49:12 -07003995 private void removeCling(int id) {
3996 final View cling = findViewById(id);
3997 if (cling != null) {
3998 final ViewGroup parent = (ViewGroup) cling.getParent();
3999 parent.post(new Runnable() {
4000 @Override
4001 public void run() {
4002 parent.removeView(cling);
4003 }
4004 });
Michael Jurka22143132012-10-04 17:42:38 +02004005 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004006 }
4007 }
Michael Jurka974c3862012-05-22 22:00:31 -07004008
4009 private boolean skipCustomClingIfNoAccounts() {
4010 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4011 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4012 if (customCling) {
4013 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004014 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004015 Account[] accounts = am.getAccountsByType("com.google");
4016 return accounts.length == 0;
4017 }
4018 return false;
4019 }
4020
Winson Chung7d7541e2011-09-16 20:14:36 -07004021 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004022 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004023 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004024 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4025 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004026 // If we're not using the default workspace layout, replace workspace cling
4027 // with a custom workspace cling (usually specified in an overlay)
4028 // For now, only do this on tablets
4029 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004030 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004031 // Use a custom cling
4032 View cling = findViewById(R.id.workspace_cling);
4033 ViewGroup clingParent = (ViewGroup) cling.getParent();
4034 int clingIndex = clingParent.indexOfChild(cling);
4035 clingParent.removeViewAt(clingIndex);
4036 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4037 clingParent.addView(customCling, clingIndex);
4038 customCling.setId(R.id.workspace_cling);
4039 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004040 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004041 } else {
4042 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004043 }
4044 }
4045 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004046 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004047 if (isClingsEnabled() &&
4048 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004049 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004050 } else {
4051 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004052 }
4053 }
4054 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004055 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004056 if (isClingsEnabled() &&
4057 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4058 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004059 } else {
4060 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004061 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004062 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004063 }
4064 public boolean isFolderClingVisible() {
4065 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004066 if (cling != null) {
4067 return cling.getVisibility() == View.VISIBLE;
4068 }
4069 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004070 }
Winson Chung82f55532011-08-09 14:14:23 -07004071 public void dismissWorkspaceCling(View v) {
4072 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004073 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004074 }
4075 public void dismissAllAppsCling(View v) {
4076 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004077 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4078 }
4079 public void dismissFolderCling(View v) {
4080 Cling cling = (Cling) findViewById(R.id.folder_cling);
4081 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004082 }
4083
Winson Chung80baf5a2010-08-09 16:03:15 -07004084 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004085 * Prints out out state for debugging.
4086 */
4087 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004088 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004089 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004090 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4091 Log.d(TAG, "mRestoring=" + mRestoring);
4092 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4093 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004094 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004095 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004096
Winson Chung785d2eb2011-04-14 16:08:02 -07004097 if (mAppsCustomizeContent != null) {
4098 mAppsCustomizeContent.dumpState();
4099 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004100 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004101 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004102
4103 @Override
4104 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4105 super.dump(prefix, fd, writer, args);
4106 writer.println(" ");
4107 writer.println("Debug logs: ");
4108 for (int i = 0; i < sDumpLogs.size(); i++) {
4109 writer.println(" " + sDumpLogs.get(i));
4110 }
4111 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004112
4113 public static void dumpDebugLogsToConsole() {
4114 Log.d(TAG, "");
4115 Log.d(TAG, "*********************");
4116 Log.d(TAG, "Launcher debug logs: ");
4117 for (int i = 0; i < sDumpLogs.size(); i++) {
4118 Log.d(TAG, " " + sDumpLogs.get(i));
4119 }
4120 Log.d(TAG, "*********************");
4121 Log.d(TAG, "");
4122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004123}
Michael Jurka2763be32011-02-24 11:19:57 -08004124
Michael Jurkaabded662011-03-04 12:06:57 -08004125interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004126 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004127 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004128 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004129 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004130 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004131}