blob: b1fbd751c362c34613fc85a8b4cf9779000b8cb8 [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
Adam Cohen39a06042013-07-19 14:30:12 -0700194 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
195
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700196 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700197 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700198 private State mState = State.WORKSPACE;
199 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800200 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700203 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
204 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700205 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700206 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800209 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
212 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700213 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700214
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800215 private final BroadcastReceiver mCloseSystemDialogsReceiver
216 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800217 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private LayoutInflater mInflater;
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800222 private View mQsbDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700223 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private DragLayer mDragLayer;
225 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700226 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700228 private AppWidgetManager mAppWidgetManager;
229 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Michael Jurkac9d95c52011-08-29 14:03:34 -0700231 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700232 private AppWidgetProviderInfo mPendingAddWidgetInfo;
233
Michael Jurka0280c3b2010-09-17 15:00:07 -0700234 private int[] mTmpAddItemCellCoordinates = new int[2];
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private FolderInfo mFolderInfo;
237
Winson Chung3d503fb2011-07-13 17:25:49 -0700238 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800239 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700240
Winson Chungc51db6a2011-10-05 11:44:49 -0700241 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700242 private AppsCustomizeTabHost mAppsCustomizeTabHost;
243 private AppsCustomizePagedView mAppsCustomizeContent;
244 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
248 // scroll issues (because the workspace may not have been measured yet) and extra work.
249 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
250 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
252 private SpannableStringBuilder mDefaultKeySsb = null;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private boolean mWorkspaceLoading = true;
255
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800256 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private boolean mRestoring;
258 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700259 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurka1e2f4652013-07-08 18:03:46 -0700261 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
263
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // Keep track of whether the user has left launcher
265 private static boolean sPausedFromUserAction = false;
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedInstanceState;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800271 private boolean mUserPresent = true;
272 private boolean mVisible = false;
273 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700274 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700276 private static LocaleConfiguration sLocaleConfiguration = null;
277
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700278 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700279
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700280 private Intent mAppMarketIntent = null;
281
Adam Cohended9f8d2010-11-03 13:25:16 -0700282 // Related to the auto-advancing of widgets
283 private final int ADVANCE_MSG = 1;
284 private final int mAdvanceInterval = 20000;
285 private final int mAdvanceStagger = 250;
286 private long mAutoAdvanceSentTime;
287 private long mAutoAdvanceTimeLeft = -1;
288 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
289 new HashMap<View, AppWidgetProviderInfo>();
290
Winson Chung400438b2011-01-16 17:53:48 -0800291 // Determines how long to wait after a rotation before restoring the screen orientation to
292 // match the sensor state.
293 private final int mRestoreScreenOrientationDelay = 500;
294
Michael Jurka4ef207b2010-11-29 17:05:45 -0800295 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700296 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
297 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
298 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800299
Craig Mautner360310b2012-10-26 15:13:08 -0700300 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700301
Adam Cohen1462de32012-07-24 22:34:36 -0700302 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
303
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700304 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
305
Winson Chung46353de2012-02-16 14:05:10 -0800306 // We only want to get the SharedPreferences once since it does an FS stat each time we get
307 // it from the context.
308 private SharedPreferences mSharedPrefs;
309
Adam Cohene25af792013-06-06 23:08:25 -0700310 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
311
Winson Chungf0c6ae02012-03-21 16:10:31 -0700312 // Holds the page that we need to animate to, and the icon views that we need to animate up
313 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700314 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700315 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700316 private ImageView mFolderIconImageView;
317 private Bitmap mFolderIconBitmap;
318 private Canvas mFolderIconCanvas;
319 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700320
Michael Jurkaddd62e92011-02-16 17:49:14 -0800321 private BubbleTextView mWaitingForResume;
322
Michael Jurka22143132012-10-04 17:42:38 +0200323 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
324 = new HideFromAccessibilityHelper();
325
Michael Jurkac1f5d262011-09-30 19:32:27 -0700326 private Runnable mBuildLayersRunnable = new Runnable() {
327 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700328 if (mWorkspace != null) {
329 mWorkspace.buildPageHardwareLayers();
330 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700331 }
332 };
333
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800334 private static ArrayList<PendingAddArguments> sPendingAddList
335 = new ArrayList<PendingAddArguments>();
336
Michael Jurka0a457bf2012-11-19 14:05:05 -0800337 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
338
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800339 private static class PendingAddArguments {
340 int requestCode;
341 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700342 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700343 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800344 int cellX;
345 int cellY;
346 }
347
Michael Jurka0a457bf2012-11-19 14:05:05 -0800348 private static boolean isPropertyEnabled(String propertyName) {
349 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700350 }
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 @Override
353 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700354 if (DEBUG_STRICT_MODE) {
355 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
356 .detectDiskReads()
357 .detectDiskWrites()
358 .detectNetwork() // or .detectAll() for all detectable problems
359 .penaltyLog()
360 .build());
361 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
362 .detectLeakedSqlLiteObjects()
363 .detectLeakedClosableObjects()
364 .penaltyLog()
365 .penaltyDeath()
366 .build());
367 }
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400370
371 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
372 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400373 LauncherAppState app = LauncherAppState.getInstance();
374 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700375 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800376 mModel = app.setLauncher(this);
377 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400378 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700380
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
383 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700384
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700385 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
386 // this also ensures that any synchronous binding below doesn't re-trigger another
387 // LauncherModel load.
388 mPaused = false;
389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200391 android.os.Debug.startMethodTracing(
392 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
394
395 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 setContentView(R.layout.launcher);
397 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700398 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800400 registerContentObservers();
401
Joe Onorato7c312c12009-08-13 21:36:53 -0700402 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 mSavedState = savedInstanceState;
405 restoreState(mSavedState);
406
Winson Chunga12a2502010-12-20 14:41:35 -0800407 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700408 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200409 mAppsCustomizeContent.onPackagesUpdated(
410 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700411 }
Winson Chunga12a2502010-12-20 14:41:35 -0800412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 if (PROFILE_STARTUP) {
414 android.os.Debug.stopMethodTracing();
415 }
416
417 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700418 if (sPausedFromUserAction) {
419 // If the user leaves launcher, then we should just load items asynchronously when
420 // they return.
421 mModel.startLoader(true, -1);
422 } else {
423 // We only load the page synchronously if the user rotates (or triggers a
424 // configuration change) while launcher is in the foreground
425 mModel.startLoader(true, mWorkspace.getCurrentPage());
426 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 }
428
429 // For handling default keys
430 mDefaultKeySsb = new SpannableStringBuilder();
431 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800432
433 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
434 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800435
Adam Cohenf9426d52012-06-04 17:26:21 -0700436 updateGlobalIcons();
437
438 // On large interfaces, we want the screen to auto-rotate based on the current orientation
439 unlockScreenOrientation(true);
440 }
441
Winson Chung4a2afa32012-07-19 14:53:05 -0700442 protected void onUserLeaveHint() {
443 super.onUserLeaveHint();
444 sPausedFromUserAction = true;
445 }
446
Winson Chung98ca0f72013-07-29 12:58:51 -0700447 /** To be overriden by subclasses to hint to Launcher that we have custom content */
448 protected boolean hasCustomContentToLeft() {
449 return false;
450 }
451
Adam Cohenf9426d52012-06-04 17:26:21 -0700452 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700453 boolean searchVisible = false;
454 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800455 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700456 int coi = getCurrentOrientationIndexForGlobalIcons();
457 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
458 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700459 updateAppMarketIcon();
460 searchVisible = updateGlobalSearchIcon();
461 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700462 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700463 if (sGlobalSearchIcon[coi] != null) {
464 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700465 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700466 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700467 if (sVoiceSearchIcon[coi] != null) {
468 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700469 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700470 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700471 if (sAppMarketIcon[coi] != null) {
472 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800473 }
Winson Chungadf0c182012-08-23 14:59:07 -0700474 if (mSearchDropTargetBar != null) {
475 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
Romain Guycbb89e42009-06-08 15:52:54 -0700478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700480 if (sLocaleConfiguration == null) {
481 new AsyncTask<Void, Void, LocaleConfiguration>() {
482 @Override
483 protected LocaleConfiguration doInBackground(Void... unused) {
484 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
485 readConfiguration(Launcher.this, localeConfiguration);
486 return localeConfiguration;
487 }
488
489 @Override
490 protected void onPostExecute(LocaleConfiguration result) {
491 sLocaleConfiguration = result;
492 checkForLocaleChange(); // recursive, but now with a locale configuration
493 }
494 }.execute();
495 return;
496 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700497
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 final Configuration configuration = getResources().getConfiguration();
499
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700500 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 final String locale = configuration.locale.toString();
502
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700503 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 final int mcc = configuration.mcc;
505
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700506 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800507 final int mnc = configuration.mnc;
508
Romain Guy84f296c2009-11-04 15:00:44 -0800509 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510
Romain Guy84f296c2009-11-04 15:00:44 -0800511 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700512 sLocaleConfiguration.locale = locale;
513 sLocaleConfiguration.mcc = mcc;
514 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700515
Joe Onorato0589f0f2010-02-08 13:44:00 -0800516 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700517
518 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
519 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700520 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700521 public void run() {
522 writeConfiguration(Launcher.this, localeConfiguration);
523 }
524 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700525 }
526 }
527
528 private static class LocaleConfiguration {
529 public String locale;
530 public int mcc = -1;
531 public int mnc = -1;
532 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700533
Romain Guy98d01652009-06-30 16:21:04 -0700534 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
535 DataInputStream in = null;
536 try {
537 in = new DataInputStream(context.openFileInput(PREFERENCES));
538 configuration.locale = in.readUTF();
539 configuration.mcc = in.readInt();
540 configuration.mnc = in.readInt();
541 } catch (FileNotFoundException e) {
542 // Ignore
543 } catch (IOException e) {
544 // Ignore
545 } finally {
546 if (in != null) {
547 try {
548 in.close();
549 } catch (IOException e) {
550 // Ignore
551 }
552 }
553 }
554 }
555
556 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
557 DataOutputStream out = null;
558 try {
559 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
560 out.writeUTF(configuration.locale);
561 out.writeInt(configuration.mcc);
562 out.writeInt(configuration.mnc);
563 out.flush();
564 } catch (FileNotFoundException e) {
565 // Ignore
566 } catch (IOException e) {
567 //noinspection ResultOfMethodCallIgnored
568 context.getFileStreamPath(PREFERENCES).delete();
569 } finally {
570 if (out != null) {
571 try {
572 out.close();
573 } catch (IOException e) {
574 // Ignore
575 }
576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 }
578 }
579
Bjorn Bringertc459e522013-06-07 19:36:01 +0100580 public LayoutInflater getInflater() {
581 return mInflater;
582 }
583
Adam Cohen716b51e2011-06-30 12:09:54 -0700584 public DragLayer getDragLayer() {
585 return mDragLayer;
586 }
587
Winson Chung36a62fe2012-05-06 18:04:42 -0700588 boolean isDraggingEnabled() {
589 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
590 // that is subsequently removed from the workspace in startBinding().
591 return !mModel.isLoadingWorkspace();
592 }
593
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 static int getScreen() {
595 synchronized (sLock) {
596 return sScreen;
597 }
598 }
599
600 static void setScreen(int screen) {
601 synchronized (sLock) {
602 sScreen = screen;
603 }
604 }
605
Winson Chung557d6ed2011-07-08 15:34:52 -0700606 /**
607 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
608 * a configuration step, this allows the proper animations to run after other transitions.
609 */
610 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700611 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800612 switch (args.requestCode) {
613 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700614 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700615 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800616 break;
617 case REQUEST_PICK_SHORTCUT:
618 processShortcut(args.intent);
619 break;
620 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700621 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700622 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700623 result = true;
624 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800625 case REQUEST_CREATE_APPWIDGET:
626 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700627 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700628 result = true;
629 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800630 case REQUEST_PICK_WALLPAPER:
631 // We just wanted the activity result here so we can clear mWaitingForResult
632 break;
633 }
Michael Jurka27614d22012-04-02 06:40:22 -0700634 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
635 // if you turned the screen off and then back while in All Apps, Launcher would not
636 // return to the workspace. Clearing mAddInfo.container here fixes this issue
637 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700638 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800639 }
640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700642 protected void onActivityResult(
643 final int requestCode, final int resultCode, final Intent data) {
644 if (requestCode == REQUEST_BIND_APPWIDGET) {
645 int appWidgetId = data != null ?
646 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
647 if (resultCode == RESULT_CANCELED) {
648 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
649 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700650 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700651 }
652 return;
653 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700654 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800655 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
656 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700657 mWaitingForResult = false;
658
Adam Cohened66b2b2012-01-23 17:28:51 -0800659 // We have special handling for widgets
660 if (isWidgetDrop) {
661 int appWidgetId = data != null ?
662 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700663 if (appWidgetId < 0) {
664 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
665 "widget configuration activity.");
666 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
667 } else {
668 completeTwoStageWidgetDrop(resultCode, appWidgetId);
669 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800670 return;
671 }
672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 // The pattern used here is that a user PICKs a specific application,
674 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700675
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
677 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700678 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 final PendingAddArguments args = new PendingAddArguments();
680 args.requestCode = requestCode;
681 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700682 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700683 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700684 args.cellX = mPendingAddInfo.cellX;
685 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800686 if (isWorkspaceLocked()) {
687 sPendingAddList.add(args);
688 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700689 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700693 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800694 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
695 null);
696 }
697
698 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700699 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700700 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800701 Runnable onCompleteRunnable = null;
702 int animationType = 0;
703
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700704 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800705 if (resultCode == RESULT_OK) {
706 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
707 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700708 mPendingAddWidgetInfo);
709 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800710 onCompleteRunnable = new Runnable() {
711 @Override
712 public void run() {
713 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700714 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800715 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
716 null);
717 }
718 };
719 } else if (resultCode == RESULT_CANCELED) {
720 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
721 onCompleteRunnable = new Runnable() {
722 @Override
723 public void run() {
724 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
725 null);
726 }
727 };
728 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700729 if (mDragLayer.getAnimatedView() != null) {
730 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
731 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
732 animationType, boundWidget, true);
733 } else {
734 // The animated view may be null in the case of a rotation during widget configuration
735 onCompleteRunnable.run();
736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
738
739 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700740 protected void onStop() {
741 super.onStop();
742 FirstFrameAnimatorHelper.setIsVisible(false);
743 }
744
745 @Override
746 protected void onStart() {
747 super.onStart();
748 FirstFrameAnimatorHelper.setIsVisible(true);
749 }
750
751 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200753 long startTime = 0;
754 if (DEBUG_RESUME_TIME) {
755 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400756 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700759
Winson Chung4a2afa32012-07-19 14:53:05 -0700760 // Restore the previous launcher state
761 if (mOnResumeState == State.WORKSPACE) {
762 showWorkspace(false);
763 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
764 showAllApps(false);
765 }
766 mOnResumeState = State.NONE;
767
Craig Mautner360310b2012-10-26 15:13:08 -0700768 // Background was set to gradient in onPause(), restore to black if in all apps.
769 setWorkspaceBackground(mState == State.WORKSPACE);
770
Winson Chungde0fb8f2012-05-08 14:37:08 -0700771 // Process any items that were added while Launcher was away
772 InstallShortcutReceiver.flushInstallQueue(this);
773
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800774 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700775 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700776 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400777 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700778 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700780 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700782 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200783 // We might have postponed some bind calls until onResume (see waitUntilResume) --
784 // execute them here
785 long startTimeCallbacks = 0;
786 if (DEBUG_RESUME_TIME) {
787 startTimeCallbacks = System.currentTimeMillis();
788 }
789
790 if (mAppsCustomizeContent != null) {
791 mAppsCustomizeContent.setBulkBind(true);
792 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700793 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
794 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200795 }
796 if (mAppsCustomizeContent != null) {
797 mAppsCustomizeContent.setBulkBind(false);
798 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700799 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200800 if (DEBUG_RESUME_TIME) {
801 Log.d(TAG, "Time spent processing callbacks in onResume: " +
802 (System.currentTimeMillis() - startTimeCallbacks));
803 }
Michael Jurka447bf842013-05-15 14:52:15 +0200804 }
Michael Jurka54554252013-08-01 12:52:23 +0200805 if (mOnResumeCallbacks.size() > 0) {
806 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
807 mOnResumeCallbacks.get(i).run();
808 }
809 mOnResumeCallbacks.clear();
810 }
Winson Chunge4e50662012-01-23 14:45:13 -0800811
812 // Reset the pressed state of icons that were locked in the press state while activities
813 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800814 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800815 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800816 mWaitingForResume.setStayPressed(false);
817 }
Winson Chunge4e50662012-01-23 14:45:13 -0800818 if (mAppsCustomizeContent != null) {
819 // Resets the previous all apps icon press state
820 mAppsCustomizeContent.resetDrawableState();
821 }
Adam Cohen06dff352012-06-01 17:17:08 -0700822 // It is possible that widgets can receive updates while launcher is not in the foreground.
823 // Consequently, the widgets will be inflated in the orientation of the foreground activity
824 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
825 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700826 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700827
828 // Again, as with the above scenario, it's possible that one or more of the global icons
829 // were updated in the wrong orientation.
830 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200831 if (DEBUG_RESUME_TIME) {
832 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
833 }
Adam Cohen06dff352012-06-01 17:17:08 -0700834 }
835
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700837 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700838 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
839 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
840 // when Launcher resumes and we are still in AllApps.
841 updateWallpaperVisibility(true);
842
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700843 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700844 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800845 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700846 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700847 }
Romain Guycbb89e42009-06-08 15:52:54 -0700848
Adam Cohen66a01fd2013-06-11 12:48:00 -0700849 protected void onFinishBindingItems() {
850 }
851
Adam Cohenbffe7452013-07-22 18:21:45 -0700852 QSBScroller mQsbScroller = new QSBScroller() {
853 int scrollY = 0;
854
855 @Override
856 public void setScrollY(int scroll) {
857 scrollY = scroll;
858
859 if (mWorkspace.isOnOrMovingToCustomContent()) {
860 mSearchDropTargetBar.setTranslationY(- scrollY);
861 }
862 }
863 };
864
865 public void resetQSBScroll() {
866 mSearchDropTargetBar.animate().translationY(0).start();
867 }
868
869 public interface CustomContentCallbacks {
870 // Custom content is completely shown
871 public void onShow();
872
873 // Custom content is completely hidden
874 public void onHide();
875 }
876
877 public interface QSBScroller {
878 public void setScrollY(int scrollY);
879 }
880
Adam Cohen66a01fd2013-06-11 12:48:00 -0700881 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700882 public QSBScroller addToCustomContentPage(View customContent) {
883 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700884 }
885
Winson Chung98ca0f72013-07-29 12:58:51 -0700886 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700887 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700888 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700889 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700890 }
891
Adam Cohenedb40762013-07-18 16:45:45 -0700892 // The custom content needs to offset its content to account for the QSB
893 public int getTopOffsetForCustomContent() {
894 return mWorkspace.getPaddingTop();
895 }
896
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700897 @Override
898 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 // Flag the loader to stop early before switching
900 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700901 if (mAppsCustomizeContent != null) {
902 mAppsCustomizeContent.surrender();
903 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800904 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700905 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700906
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800907 // We can't hide the IME if it was forced open. So don't bother
908 /*
909 @Override
910 public void onWindowFocusChanged(boolean hasFocus) {
911 super.onWindowFocusChanged(hasFocus);
912
913 if (hasFocus) {
914 final InputMethodManager inputManager = (InputMethodManager)
915 getSystemService(Context.INPUT_METHOD_SERVICE);
916 WindowManager.LayoutParams lp = getWindow().getAttributes();
917 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
918 android.os.Handler()) {
919 protected void onReceiveResult(int resultCode, Bundle resultData) {
920 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
921 }
922 });
923 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
924 }
925 }
926 */
927
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 private boolean acceptFilter() {
929 final InputMethodManager inputManager = (InputMethodManager)
930 getSystemService(Context.INPUT_METHOD_SERVICE);
931 return !inputManager.isFullscreenMode();
932 }
933
934 @Override
935 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700936 final int uniChar = event.getUnicodeChar();
937 final boolean handled = super.onKeyDown(keyCode, event);
938 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
939 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
941 keyCode, event);
942 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700943 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700944 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700945 // showSearchDialog()
946 // If there are multiple keystrokes before the search dialog takes focus,
947 // onSearchRequested() will be called for every keystroke,
948 // but it is idempotent, so it's fine.
949 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 }
951 }
952
Joe Onorato8a9625e2010-01-28 15:55:35 -0800953 // Eat the long press event so the keyboard doesn't come up.
954 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
955 return true;
956 }
957
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 return handled;
959 }
960
Karl Rosaen138a0412009-04-23 19:00:21 -0700961 private String getTypedText() {
962 return mDefaultKeySsb.toString();
963 }
964
965 private void clearTypedText() {
966 mDefaultKeySsb.clear();
967 mDefaultKeySsb.clearSpans();
968 Selection.setSelection(mDefaultKeySsb, 0);
969 }
970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700972 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
973 * State
974 */
975 private static State intToState(int stateOrdinal) {
976 State state = State.WORKSPACE;
977 final State[] stateValues = State.values();
978 for (int i = 0; i < stateValues.length; i++) {
979 if (stateValues[i].ordinal() == stateOrdinal) {
980 state = stateValues[i];
981 break;
982 }
983 }
984 return state;
985 }
986
987 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 * Restores the previous state, if it exists.
989 *
990 * @param savedState The previous state.
991 */
992 private void restoreState(Bundle savedState) {
993 if (savedState == null) {
994 return;
995 }
996
Michael Jurka883f55b2010-10-21 15:47:14 -0700997 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700998 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700999 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001000 }
1001
Winson Chungf0c6ae02012-03-21 16:10:31 -07001002 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001004 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
1006
Winson Chung3d503fb2011-07-13 17:25:49 -07001007 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001008 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001009
Winson Chung3d503fb2011-07-13 17:25:49 -07001010 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1011 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001012 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001013 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1014 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001015 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1016 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1017 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001018 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 mRestoring = true;
1020 }
1021
1022 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1023 if (renameFolder) {
1024 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001025 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 mRestoring = true;
1027 }
Winson Chunga12a2502010-12-20 14:41:35 -08001028
Winson Chung785d2eb2011-04-14 16:08:02 -07001029 // Restore the AppsCustomize tab
1030 if (mAppsCustomizeTabHost != null) {
1031 String curTab = savedState.getString("apps_customize_currentTab");
1032 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001033 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001034 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001035 mAppsCustomizeContent.loadAssociatedPages(
1036 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001037 }
1038
Winson Chung5afbf7b2011-07-25 11:53:08 -07001039 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1040 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
1043
1044 /**
1045 * Finds all the views we need and configure them properly.
1046 */
1047 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001048 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001049
Craig Mautner360310b2012-10-26 15:13:08 -07001050 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001051 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1052 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001053 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001054
1055 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1056 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057
Winson Chung4c98d922011-05-31 16:50:48 -07001058 // Setup the drag layer
1059 mDragLayer.setup(this, dragController);
1060
Winson Chung3d503fb2011-07-13 17:25:49 -07001061 // Setup the hotseat
1062 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1063 if (mHotseat != null) {
1064 mHotseat.setup(this);
1065 }
1066
Winson Chung4c98d922011-05-31 16:50:48 -07001067 // Setup the workspace
1068 mWorkspace.setHapticFeedbackEnabled(false);
1069 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001070 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001071 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001072
Winson Chungf0ea4d32011-06-06 14:27:16 -07001073 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001074 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001075
Winson Chungf0ea4d32011-06-06 14:27:16 -07001076 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001077 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001078 mAppsCustomizeContent = (AppsCustomizePagedView)
1079 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1080 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001081
Winson Chung3d503fb2011-07-13 17:25:49 -07001082 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001083 dragController.setDragScoller(mWorkspace);
1084 dragController.setScrollView(mDragLayer);
1085 dragController.setMoveTarget(mWorkspace);
1086 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001087 if (mSearchDropTargetBar != null) {
1088 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001089 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001090
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001091 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001092 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001093 mWeightWatcher = new WeightWatcher(this);
1094 mWeightWatcher.setAlpha(0.5f);
1095 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001096 new FrameLayout.LayoutParams(
1097 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001098 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001099 Gravity.BOTTOM)
1100 );
Adam Cohen39a06042013-07-19 14:30:12 -07001101
1102 boolean show = shouldShowWeightWatcher();
1103 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 }
1106
1107 /**
1108 * Creates a view representing a shortcut.
1109 *
1110 * @param info The data structure describing the shortcut.
1111 *
1112 * @return A View inflated from R.layout.application.
1113 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001114 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001116 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
1118
1119 /**
1120 * Creates a view representing a shortcut inflated from the specified resource.
1121 *
1122 * @param layoutResId The id of the XML layout used to create the shortcut.
1123 * @param parent The group the shortcut belongs to.
1124 * @param info The data structure describing the shortcut.
1125 *
1126 * @return A View inflated from layoutResId.
1127 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001128 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001129 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1130 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 return favorite;
1133 }
1134
1135 /**
1136 * Add an application shortcut to the workspace.
1137 *
1138 * @param data The intent describing the application.
1139 * @param cellInfo The position on screen where to create the shortcut.
1140 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001141 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001142 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001143 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001144
Adam Cohenfbba09b2011-07-18 21:43:05 -07001145 // First we check if we already know the exact location where we want to add this item.
1146 if (cellX >= 0 && cellY >= 0) {
1147 cellXY[0] = cellX;
1148 cellXY[1] = cellY;
1149 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001150 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001151 return;
1152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001154 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001155
Romain Guy73b979d2009-06-09 12:57:21 -07001156 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001157 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001159 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001160 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001161 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001162 } else {
1163 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 }
1165 }
Romain Guycbb89e42009-06-08 15:52:54 -07001166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 /**
1168 * Add a shortcut to the workspace.
1169 *
1170 * @param data The intent describing the shortcut.
1171 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001173 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001174 int cellY) {
1175 int[] cellXY = mTmpAddItemCellCoordinates;
1176 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001177 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001178
Michael Jurkad3ef3062010-11-23 16:23:58 -08001179 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001180
Adam Cohen558baaf2011-08-15 15:22:57 -07001181 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001182 if (info == null) {
1183 return;
1184 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001185 final View view = createShortcut(info);
1186
Adam Cohenfbba09b2011-07-18 21:43:05 -07001187 // First we check if we already know the exact location where we want to add this item.
1188 if (cellX >= 0 && cellY >= 0) {
1189 cellXY[0] = cellX;
1190 cellXY[1] = cellY;
1191 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001192
1193 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001194 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001195 true, null,null)) {
1196 return;
1197 }
1198 DragObject dragObject = new DragObject();
1199 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001200 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1201 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001202 return;
1203 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001204 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001205 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001206 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001207 foundCellSpan = (result != null);
1208 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001209 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001210 }
1211
1212 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001213 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 return;
1215 }
1216
Adam Cohendcd297f2013-06-18 13:13:40 -07001217 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218
1219 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001220 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001221 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
1223 }
1224
Adam Cohen2f093b62012-04-30 18:59:53 -07001225 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1226 int minHeight) {
1227 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001228 // We want to account for the extra amount of padding that we are adding to the widget
1229 // to ensure that it gets the full amount of space that it has requested
1230 int requiredWidth = minWidth + padding.left + padding.right;
1231 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001232 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001233 }
1234
Adam Cohen2f093b62012-04-30 18:59:53 -07001235 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1236 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001237 }
1238
Adam Cohen2f093b62012-04-30 18:59:53 -07001239 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1240 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001241 }
1242
Adam Cohen2f093b62012-04-30 18:59:53 -07001243 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1244 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001245 }
1246
Adam Cohen2f093b62012-04-30 18:59:53 -07001247 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1248 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001249 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001250 }
1251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001253 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001255 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001256 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001258 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001259 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1260 if (appWidgetInfo == null) {
1261 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1262 }
Romain Guycbb89e42009-06-08 15:52:54 -07001263
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001264 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001265 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001266
Adam Cohen2f093b62012-04-30 18:59:53 -07001267 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1268 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001271 // We have saved the position to which the widget was dragged-- this really only matters
1272 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001273 int[] cellXY = mTmpAddItemCellCoordinates;
1274 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001275 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001276 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001277 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1278 cellXY[0] = mPendingAddInfo.cellX;
1279 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001280 spanXY[0] = mPendingAddInfo.spanX;
1281 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001282 foundCellSpan = true;
1283 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001284 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001285 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001286 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1287 spanXY[1], cellXY, finalSpan);
1288 spanXY[0] = finalSpan[0];
1289 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001290 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001291 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001292 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001293 }
1294
Michael Jurka0280c3b2010-09-17 15:00:07 -07001295 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001296 if (appWidgetId != -1) {
1297 // Deleting an app widget ID is a void call but writes to disk before returning
1298 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001299 new Thread("deleteAppWidgetId") {
1300 public void run() {
1301 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1302 }
1303 }.start();
1304 }
Winson Chung93eef082012-03-23 15:59:27 -07001305 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001306 return;
1307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001309 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001310 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1311 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001312 launcherInfo.spanX = spanXY[0];
1313 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001314 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1315 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001318 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319
1320 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001321 if (hostView == null) {
1322 // Perform actual inflation because we're live
1323 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1324 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1325 } else {
1326 // The AppWidgetHostView has already been inflated and instantiated
1327 launcherInfo.hostView = hostView;
1328 }
Romain Guycbb89e42009-06-08 15:52:54 -07001329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001331 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001332 launcherInfo.notifyWidgetSizeChanged(this);
1333
Adam Cohendcd297f2013-06-18 13:13:40 -07001334 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001335 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001336
1337 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001339 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 }
Romain Guycbb89e42009-06-08 15:52:54 -07001341
Adam Cohended9f8d2010-11-03 13:25:16 -07001342 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1343 @Override
1344 public void onReceive(Context context, Intent intent) {
1345 final String action = intent.getAction();
1346 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1347 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001348 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001349 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001350
Winson Chungc100e8e2011-08-09 16:02:43 -07001351 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001352 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001353 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1354 mAppsCustomizeTabHost.reset();
1355 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001356 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001357 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1358 mUserPresent = true;
1359 updateRunning();
1360 }
1361 }
1362 };
1363
1364 @Override
1365 public void onAttachedToWindow() {
1366 super.onAttachedToWindow();
1367
1368 // Listen for broadcasts related to user-presence
1369 final IntentFilter filter = new IntentFilter();
1370 filter.addAction(Intent.ACTION_SCREEN_OFF);
1371 filter.addAction(Intent.ACTION_USER_PRESENT);
1372 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001373 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001374 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001375 mVisible = true;
1376 }
1377
1378 @Override
1379 public void onDetachedFromWindow() {
1380 super.onDetachedFromWindow();
1381 mVisible = false;
1382
Adam Cohend113e0c2010-11-11 10:48:05 -08001383 if (mAttached) {
1384 unregisterReceiver(mReceiver);
1385 mAttached = false;
1386 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001387 updateRunning();
1388 }
1389
1390 public void onWindowVisibilityChanged(int visibility) {
1391 mVisible = visibility == View.VISIBLE;
1392 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001393 // The following code used to be in onResume, but it turns out onResume is called when
1394 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1395 // is a more appropriate event to handle
1396 if (mVisible) {
1397 mAppsCustomizeTabHost.onWindowVisible();
1398 if (!mWorkspaceLoading) {
1399 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001400 // We want to let Launcher draw itself at least once before we force it to build
1401 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001402 // apps is nice and speedy.
1403 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001404 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001405 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001406 if (mStarted) return;
1407 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001408 // We delay the layer building a bit in order to give
1409 // other message processing a time to run. In particular
1410 // this avoids a delay in hiding the IME if it was
1411 // currently shown, because doing that may involve
1412 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001413 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001414 final ViewTreeObserver.OnDrawListener listener = this;
1415 mWorkspace.post(new Runnable() {
1416 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001417 if (mWorkspace != null &&
1418 mWorkspace.getViewTreeObserver() != null) {
1419 mWorkspace.getViewTreeObserver().
1420 removeOnDrawListener(listener);
1421 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001422 }
1423 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001424 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001425 }
1426 });
1427 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001428 // When Launcher comes back to foreground, a different Activity might be responsible for
1429 // the app market intent, so refresh the icon
1430 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001431 clearTypedText();
1432 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001433 }
1434
1435 private void sendAdvanceMessage(long delay) {
1436 mHandler.removeMessages(ADVANCE_MSG);
1437 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1438 mHandler.sendMessageDelayed(msg, delay);
1439 mAutoAdvanceSentTime = System.currentTimeMillis();
1440 }
1441
1442 private void updateRunning() {
1443 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1444 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1445 mAutoAdvanceRunning = autoAdvanceRunning;
1446 if (autoAdvanceRunning) {
1447 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1448 sendAdvanceMessage(delay);
1449 } else {
1450 if (!mWidgetsToAdvance.isEmpty()) {
1451 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1452 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1453 }
1454 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001455 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001456 }
1457 }
1458 }
1459
1460 private final Handler mHandler = new Handler() {
1461 @Override
1462 public void handleMessage(Message msg) {
1463 if (msg.what == ADVANCE_MSG) {
1464 int i = 0;
1465 for (View key: mWidgetsToAdvance.keySet()) {
1466 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1467 final int delay = mAdvanceStagger * i;
1468 if (v instanceof Advanceable) {
1469 postDelayed(new Runnable() {
1470 public void run() {
1471 ((Advanceable) v).advance();
1472 }
1473 }, delay);
1474 }
1475 i++;
1476 }
1477 sendAdvanceMessage(mAdvanceInterval);
1478 }
1479 }
1480 };
1481
1482 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001483 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001484 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1485 if (v instanceof Advanceable) {
1486 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001487 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001488 updateRunning();
1489 }
1490 }
1491
1492 void removeWidgetToAutoAdvance(View hostView) {
1493 if (mWidgetsToAdvance.containsKey(hostView)) {
1494 mWidgetsToAdvance.remove(hostView);
1495 updateRunning();
1496 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001497 }
1498
Joe Onorato9c1289c2009-08-17 11:03:03 -04001499 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001500 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501 launcherInfo.hostView = null;
1502 }
1503
Winson Chung93eef082012-03-23 15:59:27 -07001504 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1505 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1506 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001507 }
1508
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001509 public LauncherAppWidgetHost getAppWidgetHost() {
1510 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 }
Romain Guycbb89e42009-06-08 15:52:54 -07001512
Winson Chunga9abd0e2010-10-27 17:18:37 -07001513 public LauncherModel getModel() {
1514 return mModel;
1515 }
1516
Bjorn Bringertc459e522013-06-07 19:36:01 +01001517 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001518 getWindow().closeAllPanels();
1519
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001520 // Whatever we were doing is hereby canceled.
1521 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001522 }
1523
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 @Override
1525 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001526 long startTime = 0;
1527 if (DEBUG_RESUME_TIME) {
1528 startTime = System.currentTimeMillis();
1529 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 super.onNewIntent(intent);
1531
1532 // Close the menu
1533 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001534 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001535 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001536
Jamie Genniscb222e82012-10-23 15:44:26 -07001537 final boolean alreadyOnHome =
1538 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001539 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001540
Jamie Genniscb222e82012-10-23 15:44:26 -07001541 Runnable processIntent = new Runnable() {
1542 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001543 if (mWorkspace == null) {
1544 // Can be cases where mWorkspace is null, this prevents a NPE
1545 return;
1546 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001547 Folder openFolder = mWorkspace.getOpenFolder();
1548 // In all these cases, only animate if we're already on home
1549 mWorkspace.exitWidgetResizeMode();
1550 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1551 openFolder == null) {
1552 mWorkspace.moveToDefaultScreen(true);
1553 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001554
Jamie Genniscb222e82012-10-23 15:44:26 -07001555 closeFolder();
1556 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001557
Jamie Genniscb222e82012-10-23 15:44:26 -07001558 // If we are already on home, then just animate back to the workspace,
1559 // otherwise, just wait until onResume to set the state back to Workspace
1560 if (alreadyOnHome) {
1561 showWorkspace(true);
1562 } else {
1563 mOnResumeState = State.WORKSPACE;
1564 }
1565
1566 final View v = getWindow().peekDecorView();
1567 if (v != null && v.getWindowToken() != null) {
1568 InputMethodManager imm = (InputMethodManager)getSystemService(
1569 INPUT_METHOD_SERVICE);
1570 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1571 }
1572
1573 // Reset AllApps to its initial state
1574 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1575 mAppsCustomizeTabHost.reset();
1576 }
1577 }
1578 };
1579
1580 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1581 // Delay processing of the intent to allow the status bar animation to finish
1582 // first in order to avoid janky animations.
1583 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001584 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001585 // Process the intent immediately.
1586 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001587 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 }
Michael Jurka447bf842013-05-15 14:52:15 +02001590 if (DEBUG_RESUME_TIME) {
1591 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
1594
1595 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001596 public void onRestoreInstanceState(Bundle state) {
1597 super.onRestoreInstanceState(state);
1598 for (int page: mSynchronouslyBoundPages) {
1599 mWorkspace.restoreInstanceStateForChild(page);
1600 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 }
1602
1603 @Override
1604 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001605 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001606 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607
Michael Jurka883f55b2010-10-21 15:47:14 -07001608 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001609 // We close any open folder since it will not be re-opened, and we need to make sure
1610 // this state is reflected.
1611 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
Adam Cohendcd297f2013-06-18 13:13:40 -07001613 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001614 mWaitingForResult) {
1615 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001616 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001617 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1618 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001619 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1620 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1621 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 }
1623
1624 if (mFolderInfo != null && mWaitingForResult) {
1625 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1626 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1627 }
Michael Jurka946ad472010-07-09 18:05:18 -07001628
Winson Chung785d2eb2011-04-14 16:08:02 -07001629 // Save the current AppsCustomize tab
1630 if (mAppsCustomizeTabHost != null) {
1631 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1632 if (currentTabTag != null) {
1633 outState.putString("apps_customize_currentTab", currentTabTag);
1634 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001635 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1636 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 }
1639
1640 @Override
1641 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001643
Winson Chunge7a03942011-08-05 15:05:12 -07001644 // Remove all pending runnables
1645 mHandler.removeMessages(ADVANCE_MSG);
1646 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001647 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001648
Winson Chungcd2b0142011-06-08 16:02:26 -07001649 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001650 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001651 mModel.stopLoader();
1652 app.setLauncher(null);
1653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001655 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001657 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001659 mAppWidgetHost = null;
1660
1661 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662
1663 TextKeyListener.getInstance().release();
1664
Winson Chung81b52252012-08-27 15:34:29 -07001665 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1666 // to prevent leaking Launcher activities on orientation change.
1667 if (mModel != null) {
1668 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1669 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001670
1671 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001672 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001673
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001674 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001675 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1676 mWorkspace.removeAllViews();
1677 mWorkspace = null;
1678 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001679
Michael Jurka2ecf9952012-06-18 12:52:28 -07001680 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 }
1682
Adam Cohena9cf38f2011-05-02 15:36:58 -07001683 public DragController getDragController() {
1684 return mDragController;
1685 }
1686
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 @Override
1688 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001689 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 super.startActivityForResult(intent, requestCode);
1691 }
1692
Winson Chung70d72102011-08-12 11:24:35 -07001693 /**
1694 * Indicates that we want global search for this activity by setting the globalSearch
1695 * argument for {@link #startSearch} to true.
1696 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001698 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001700
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001701 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001702
Karl Rosaen138a0412009-04-23 19:00:21 -07001703 if (initialQuery == null) {
1704 // Use any text typed in the launcher as the initial query
1705 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 if (appSearchData == null) {
1708 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001709 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 }
Winson Chungadf0c182012-08-23 14:59:07 -07001711 Rect sourceBounds = new Rect();
1712 if (mSearchDropTargetBar != null) {
1713 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1714 }
Romain Guycbb89e42009-06-08 15:52:54 -07001715
Bjorn Bringertc459e522013-06-07 19:36:01 +01001716 startSearch(initialQuery, selectInitialQuery,
1717 appSearchData, sourceBounds);
1718 }
1719
1720 public void startSearch(String initialQuery,
1721 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001722 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001723 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001724 }
1725
1726 /**
1727 * Starts the global search activity. This code is a copied from SearchManager
1728 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001729 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001730 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001731 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001732 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1733 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1734 if (globalSearchActivity == null) {
1735 Log.w(TAG, "No global search activity found.");
1736 return;
1737 }
1738 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1739 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1740 intent.setComponent(globalSearchActivity);
1741 // Make sure that we have a Bundle to put source in
1742 if (appSearchData == null) {
1743 appSearchData = new Bundle();
1744 } else {
1745 appSearchData = new Bundle(appSearchData);
1746 }
1747 // Set source to package name of app that starts global search, if not set already.
1748 if (!appSearchData.containsKey("source")) {
1749 appSearchData.putString("source", getPackageName());
1750 }
1751 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1752 if (!TextUtils.isEmpty(initialQuery)) {
1753 intent.putExtra(SearchManager.QUERY, initialQuery);
1754 }
1755 if (selectInitialQuery) {
1756 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1757 }
1758 intent.setSourceBounds(sourceBounds);
1759 try {
1760 startActivity(intent);
1761 } catch (ActivityNotFoundException ex) {
1762 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1763 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 }
1765
Winson Chung70d72102011-08-12 11:24:35 -07001766 @Override
1767 public boolean onCreateOptionsMenu(Menu menu) {
1768 if (isWorkspaceLocked()) {
1769 return false;
1770 }
1771
1772 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001773
1774 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1775 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1776 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001777 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1778 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1779 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001780 String helpUrl = getString(R.string.help_url);
1781 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001782 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1783 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1784
Winson Chung70d72102011-08-12 11:24:35 -07001785 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1786 .setIcon(android.R.drawable.ic_menu_gallery)
1787 .setAlphabeticShortcut('W');
1788 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1789 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001790 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001791 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001792 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1793 .setIcon(android.R.drawable.ic_menu_preferences)
1794 .setIntent(settings)
1795 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001796 if (!helpUrl.isEmpty()) {
1797 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1798 .setIcon(android.R.drawable.ic_menu_help)
1799 .setIntent(help)
1800 .setAlphabeticShortcut('H');
1801 }
Winson Chung70d72102011-08-12 11:24:35 -07001802 return true;
1803 }
1804
1805 @Override
1806 public boolean onPrepareOptionsMenu(Menu menu) {
1807 super.onPrepareOptionsMenu(menu);
1808
1809 if (mAppsCustomizeTabHost.isTransitioning()) {
1810 return false;
1811 }
1812 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1813 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1814
1815 return true;
1816 }
1817
1818 @Override
1819 public boolean onOptionsItemSelected(MenuItem item) {
1820 switch (item.getItemId()) {
1821 case MENU_WALLPAPER_SETTINGS:
1822 startWallpaper();
1823 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001824 }
1825
1826 return super.onOptionsItemSelected(item);
1827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001829 @Override
1830 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001831 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001832 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001833 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001834 }
1835
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836 public boolean isWorkspaceLocked() {
1837 return mWorkspaceLoading || mWaitingForResult;
1838 }
1839
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001841 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001842 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001843 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1844 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001845 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001846 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001847 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001848
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001849 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1850 AppWidgetProviderInfo appWidgetInfo) {
1851 if (appWidgetInfo.configure != null) {
1852 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001853
Bjorn Bringert7984c942009-12-09 15:38:25 +00001854 // Launch over to configure widget, if needed
1855 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001856 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001857 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001858 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001860 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001861 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001862 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001863 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001864 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
1866 }
Romain Guycbb89e42009-06-08 15:52:54 -07001867
Adam Cohenfbba09b2011-07-18 21:43:05 -07001868 /**
1869 * Process a shortcut drop.
1870 *
1871 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001872 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001873 * @param cell The cell it should be added to, optional
1874 * @param position The location on the screen where it was dropped, optional
1875 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001876 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001877 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001878 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001879 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001880 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001881 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001882
1883 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001884 mPendingAddInfo.cellX = cell[0];
1885 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001886 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001887
1888 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1889 createShortcutIntent.setComponent(componentName);
1890 processShortcut(createShortcutIntent);
1891 }
1892
Adam Cohenfbba09b2011-07-18 21:43:05 -07001893 /**
1894 * Process a widget drop.
1895 *
1896 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001897 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001898 * @param cell The cell it should be added to, optional
1899 * @param position The location on the screen where it was dropped, optional
1900 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001901 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001902 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001903 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001904 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001905 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001906 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001907 mPendingAddInfo.minSpanX = info.minSpanX;
1908 mPendingAddInfo.minSpanY = info.minSpanY;
1909
Adam Cohenfbba09b2011-07-18 21:43:05 -07001910 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001911 mPendingAddInfo.cellX = cell[0];
1912 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001913 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001914 if (span != null) {
1915 mPendingAddInfo.spanX = span[0];
1916 mPendingAddInfo.spanY = span[1];
1917 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001918
Adam Cohened66b2b2012-01-23 17:28:51 -08001919 AppWidgetHostView hostView = info.boundWidget;
1920 int appWidgetId;
1921 if (hostView != null) {
1922 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001923 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001924 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001925 // In this case, we either need to start an activity to get permission to bind
1926 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001927 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001928 Bundle options = info.bindOptions;
1929
1930 boolean success = false;
1931 if (options != null) {
1932 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1933 info.componentName, options);
1934 } else {
1935 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1936 info.componentName);
1937 }
1938 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001939 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001940 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001941 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001942 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1943 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1944 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001945 // TODO: we need to make sure that this accounts for the options bundle.
1946 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001947 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1948 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001949 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001950 }
1951
Joe Onoratodeb98af2010-02-19 14:59:39 -08001952 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001953 // Handle case where user selected "Applications"
1954 String applicationName = getResources().getString(R.string.group_applications);
1955 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001956
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001957 if (applicationName != null && applicationName.equals(shortcutName)) {
1958 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1959 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001960
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001961 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1962 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001963 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001964 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001965 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001966 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 }
1969
Winson Chung24ab2f12010-09-16 14:10:47 -07001970 void processWallpaper(Intent intent) {
1971 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1972 }
1973
Adam Cohendcd297f2013-06-18 13:13:40 -07001974 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001975 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001976 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 folderInfo.title = getText(R.string.folder_name);
1978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001981 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001982 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983
1984 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001985 FolderIcon newFolder =
1986 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001988 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001989 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
Romain Guycbb89e42009-06-08 15:52:54 -07001991
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001993 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001994 }
1995
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001997 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001999 Intent chooser = Intent.createChooser(pickWallpaper,
2000 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08002001 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
2002 // Removed in Eclair MR1
2003// WallpaperManager wm = (WallpaperManager)
2004// getSystemService(Context.WALLPAPER_SERVICE);
2005// WallpaperInfo wi = wm.getWallpaperInfo();
2006// if (wi != null && wi.getSettingsActivity() != null) {
2007// LabeledIntent li = new LabeledIntent(getPackageName(),
2008// R.string.configure_wallpaper, 0);
2009// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
2010// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
2011// }
Mike Clerona0618e42009-10-22 13:55:21 -07002012 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 }
2014
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002015 /**
2016 * Registers various content observers. The current implementation registers
2017 * only a favorites observer to keep track of the favorites applications.
2018 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002019 private void registerContentObservers() {
2020 ContentResolver resolver = getContentResolver();
2021 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2022 true, mWidgetObserver);
2023 }
2024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 @Override
2026 public boolean dispatchKeyEvent(KeyEvent event) {
2027 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2028 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002030 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002031 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002032 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002033 dumpState();
2034 return true;
2035 }
2036 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002037 }
2038 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2039 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002040 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 return true;
2042 }
2043 }
2044
2045 return super.dispatchKeyEvent(event);
2046 }
2047
Joe Onorato88ec0992009-11-19 13:16:06 -08002048 @Override
2049 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002050 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002051 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002052 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002053 Folder openFolder = mWorkspace.getOpenFolder();
2054 if (openFolder.isEditingName()) {
2055 openFolder.dismissEditingName();
2056 } else {
2057 closeFolder();
2058 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002059 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002060 mWorkspace.exitWidgetResizeMode();
2061
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002062 // Back button is a no-op here, but give at least some feedback for the button press
2063 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002064 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002065 }
2066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002068 * Re-listen when widgets are reset.
2069 */
2070 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002071 if (mAppWidgetHost != null) {
2072 mAppWidgetHost.startListening();
2073 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002074 }
2075
2076 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 * Launches the intent referred by the clicked shortcut.
2078 *
2079 * @param v The view representing the clicked shortcut.
2080 */
2081 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002082 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2083 // view has detached (it's possible for this to happen if the view is removed mid touch).
2084 if (v.getWindowToken() == null) {
2085 return;
2086 }
2087
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002088 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002089 return;
2090 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002093 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002095 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002096
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002097 // Check for special shortcuts
2098 if (intent.getComponent() != null) {
2099 final String shortcutClass = intent.getComponent().getClassName();
2100
2101 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2102 showAllApps(true);
2103 return;
2104 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2105 MemoryDumpActivity.startDump(this);
2106 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002107 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2108 toggleShowWeightWatcher();
2109 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002110 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002111 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002112
2113 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002114 int[] pos = new int[2];
2115 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002116 intent.setSourceBounds(new Rect(pos[0], pos[1],
2117 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002118
2119 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002120
2121 if (success && v instanceof BubbleTextView) {
2122 mWaitingForResume = (BubbleTextView) v;
2123 mWaitingForResume.setStayPressed(true);
2124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002126 if (v instanceof FolderIcon) {
2127 FolderIcon fi = (FolderIcon) v;
2128 handleFolderClick(fi);
2129 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002130 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002131 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002132 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002133 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002134 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 }
2137 }
2138
Michael Jurka0e260592010-06-30 17:07:39 -07002139 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002140 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002141 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002142 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2143 showWorkspace(true);
2144 }
Michael Jurka0e260592010-06-30 17:07:39 -07002145 return false;
2146 }
2147
Michael Jurkaaf442092010-06-10 17:01:57 -07002148 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002149 * Event handler for the search button
2150 *
2151 * @param v The view that was clicked.
2152 */
2153 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002154 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2155
Amith Yamasania135ba82011-08-09 17:42:01 -07002156 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002157 }
2158
2159 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002160 * Event handler for the voice button
2161 *
2162 * @param v The view that was clicked.
2163 */
2164 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002165 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002166
Bjorn Bringertc459e522013-06-07 19:36:01 +01002167 startVoice();
2168 }
2169
2170 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002171 try {
2172 final SearchManager searchManager =
2173 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2174 ComponentName activityName = searchManager.getGlobalSearchActivity();
2175 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002176 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002177 if (activityName != null) {
2178 intent.setPackage(activityName.getPackageName());
2179 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002180 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002181 } catch (ActivityNotFoundException e) {
2182 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002183 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002184 startActivitySafely(null, intent, "onClickVoiceButton");
2185 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002186 }
2187
2188 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002189 * Event handler for the "grid" button that appears on the home screen, which
2190 * enters all apps mode.
2191 *
2192 * @param v The view that was clicked.
2193 */
2194 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002195 showAllApps(true);
2196 }
2197
2198 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002199 // Provide the same haptic feedback that the system offers for virtual keys.
2200 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002201 }
2202
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002203 public void onClickAppMarketButton(View v) {
2204 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002205 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002206 } else {
2207 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002208 }
2209 }
2210
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002211 void startApplicationDetailsActivity(ComponentName componentName) {
2212 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002213 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2214 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002215 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002216 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002217 }
2218
Michael Jurka1e2f4652013-07-08 18:03:46 -07002219 // returns true if the activity was started
2220 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2221 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002222 // System applications cannot be installed. For now, show a toast explaining that.
2223 // We may give them the option of disabling apps this way.
2224 int messageId = R.string.uninstall_system_app_text;
2225 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002226 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002227 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002228 String packageName = componentName.getPackageName();
2229 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002230 Intent intent = new Intent(
2231 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002232 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2233 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002234 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002235 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002236 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002237 }
2238
Michael Jurka86a720e2012-05-09 11:23:23 -07002239 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002243 // Only launch using the new animation if the shortcut has not opted out (this is a
2244 // private contract between launcher and may be ignored in the future).
2245 boolean useLaunchAnimation = (v != null) &&
2246 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2247 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002248 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2249 v.getMeasuredWidth(), v.getMeasuredHeight());
2250
2251 startActivity(intent, opts.toBundle());
2252 } else {
2253 startActivity(intent);
2254 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002255 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 } catch (SecurityException e) {
2257 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002258 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002260 "or use the exported attribute for this activity. "
2261 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002263 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002265
Michael Jurka86a720e2012-05-09 11:23:23 -07002266 boolean startActivitySafely(View v, Intent intent, Object tag) {
2267 boolean success = false;
2268 try {
2269 success = startActivity(v, intent, tag);
2270 } catch (ActivityNotFoundException e) {
2271 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2272 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2273 }
2274 return success;
2275 }
2276
Romain Guy8e633c52010-03-04 12:51:36 -08002277 void startActivityForResultSafely(Intent intent, int requestCode) {
2278 try {
2279 startActivityForResult(intent, requestCode);
2280 } catch (ActivityNotFoundException e) {
2281 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2282 } catch (SecurityException e) {
2283 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2284 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2285 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2286 "or use the exported attribute for this activity.", e);
2287 }
2288 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289
Adam Cohena9cf38f2011-05-02 15:36:58 -07002290 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002291 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002292 Folder openFolder = mWorkspace.getFolderForTag(info);
2293
2294 // If the folder info reports that the associated folder is open, then verify that
2295 // it is actually opened. There have been a few instances where this gets out of sync.
2296 if (info.opened && openFolder == null) {
2297 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002298 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002299 info.opened = false;
2300 }
2301
Adam Cohenfb91f302012-06-11 15:45:18 -07002302 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 // Close any open folder
2304 closeFolder();
2305 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002306 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 } else {
2308 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 int folderScreen;
2310 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002311 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 // .. and close it
2313 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002314 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 // Close any folder open on the current screen
2316 closeFolder();
2317 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002318 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 }
2320 }
2321 }
2322 }
2323
Adam Cohen268c4752012-06-06 17:47:33 -07002324 /**
2325 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2326 * in the DragLayer in the exact absolute location of the original FolderIcon.
2327 */
2328 private void copyFolderIconToImage(FolderIcon fi) {
2329 final int width = fi.getMeasuredWidth();
2330 final int height = fi.getMeasuredHeight();
2331
2332 // Lazy load ImageView, Bitmap and Canvas
2333 if (mFolderIconImageView == null) {
2334 mFolderIconImageView = new ImageView(this);
2335 }
2336 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2337 mFolderIconBitmap.getHeight() != height) {
2338 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2339 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2340 }
2341
2342 DragLayer.LayoutParams lp;
2343 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2344 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2345 } else {
2346 lp = new DragLayer.LayoutParams(width, height);
2347 }
2348
Adam Cohen307fe232012-08-16 17:55:58 -07002349 // The layout from which the folder is being opened may be scaled, adjust the starting
2350 // view size by this scale factor.
2351 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002352 lp.customPosition = true;
2353 lp.x = mRectForFolderAnimation.left;
2354 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002355 lp.width = (int) (scale * width);
2356 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002357
2358 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2359 fi.draw(mFolderIconCanvas);
2360 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002361 if (fi.getFolder() != null) {
2362 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2363 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002364 }
Adam Cohen268c4752012-06-06 17:47:33 -07002365 // Just in case this image view is still in the drag layer from a previous animation,
2366 // we remove it and re-add it.
2367 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2368 mDragLayer.removeView(mFolderIconImageView);
2369 }
2370 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002371 if (fi.getFolder() != null) {
2372 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002373 }
Adam Cohen268c4752012-06-06 17:47:33 -07002374 }
2375
Adam Cohen2801caf2011-05-13 20:57:39 -07002376 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002377 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002378 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2379 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2380 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2381
Adam Cohenc51934b2011-07-26 21:07:43 -07002382 FolderInfo info = (FolderInfo) fi.getTag();
2383 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2384 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002385 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2386 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002387 }
2388
Adam Cohen268c4752012-06-06 17:47:33 -07002389 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2390 copyFolderIconToImage(fi);
2391 fi.setVisibility(View.INVISIBLE);
2392
Michael Jurka2ecf9952012-06-18 12:52:28 -07002393 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002394 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002395 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2396 oa.start();
2397 }
2398
Adam Cohen268c4752012-06-06 17:47:33 -07002399 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002400 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002401 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2402 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2403 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2404
Adam Cohen268c4752012-06-06 17:47:33 -07002405 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002406
Adam Cohen268c4752012-06-06 17:47:33 -07002407 // We remove and re-draw the FolderIcon in-case it has changed
2408 mDragLayer.removeView(mFolderIconImageView);
2409 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002410 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002411 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002412 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002413 oa.addListener(new AnimatorListenerAdapter() {
2414 @Override
2415 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002416 if (cl != null) {
2417 cl.clearFolderLeaveBehind();
2418 // Remove the ImageView copy of the FolderIcon and make the original visible.
2419 mDragLayer.removeView(mFolderIconImageView);
2420 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002421 }
2422 }
2423 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002424 oa.start();
2425 }
2426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002428 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 * is animated relative to the specified View. If the View is null, no animation
2430 * is played.
2431 *
2432 * @param folderInfo The FolderInfo describing the folder to open.
2433 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002434 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002435 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002436 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437
Adam Cohena9cf38f2011-05-02 15:36:58 -07002438 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439
Adam Cohen4554ee12011-08-03 16:13:21 -07002440 // Just verify that the folder hasn't already been added to the DragLayer.
2441 // There was a one-off crash where the folder had a parent already.
2442 if (folder.getParent() == null) {
2443 mDragLayer.addView(folder);
2444 mDragController.addDropTarget((DropTarget) folder);
2445 } else {
2446 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2447 folder.getParent() + ").");
2448 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002449 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002450 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002451
2452 // Notify the accessibility manager that this folder "window" has appeared and occluded
2453 // the workspace items
2454 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2455 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002456 }
2457
2458 public void closeFolder() {
2459 Folder folder = mWorkspace.getOpenFolder();
2460 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002461 if (folder.isEditingName()) {
2462 folder.dismissEditingName();
2463 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002464 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002465
2466 // Dismiss the folder cling
2467 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002468 }
2469 }
2470
2471 void closeFolder(Folder folder) {
2472 folder.getInfo().opened = false;
2473
2474 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2475 if (parent != null) {
2476 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2477 shrinkAndFadeInFolderIcon(fi);
2478 }
2479 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002480
2481 // Notify the accessibility manager that this folder "window" has disappeard and no
2482 // longer occludeds the workspace items
2483 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 }
2485
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002487 if (!isDraggingEnabled()) return false;
2488 if (isWorkspaceLocked()) return false;
2489 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490
2491 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002492 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 }
2494
Michael Jurka0280c3b2010-09-17 15:00:07 -07002495 resetAddInfo();
2496 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002498 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 return true;
2500 }
2501
Winson Chung3d503fb2011-07-13 17:25:49 -07002502 // The hotseat touch handling does not go through Workspace, and we always allow long press
2503 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002504 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002505 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2506 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002507 if (itemUnderLongClick == null) {
2508 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002509 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2510 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002511 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002512 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2513 mWorkspace.startReordering();
2514 } else {
2515 startWallpaper();
2516 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002518 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002520 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 }
2522 }
2523 }
2524 return true;
2525 }
2526
Winson Chung3d503fb2011-07-13 17:25:49 -07002527 boolean isHotseatLayout(View layout) {
2528 return mHotseat != null && layout != null &&
2529 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2530 }
2531 Hotseat getHotseat() {
2532 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002533 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002534 SearchDropTargetBar getSearchBar() {
2535 return mSearchDropTargetBar;
2536 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002537
Winson Chung3d503fb2011-07-13 17:25:49 -07002538 /**
2539 * Returns the CellLayout of the specified container at the specified screen.
2540 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002541 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002542 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2543 if (mHotseat != null) {
2544 return mHotseat.getLayout();
2545 } else {
2546 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002547 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002548 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002549 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002550 }
2551 }
2552
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 Workspace getWorkspace() {
2554 return mWorkspace;
2555 }
2556
Daniel Sandler843e8602010-06-07 14:59:01 -04002557 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002558 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002559 }
2560
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 /**
2562 * Helper method for the cameraZoomIn/cameraZoomOut animations
2563 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002564 * @param scaleFactor The scale factor used for the zoom
2565 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002566 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002567 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002568 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002569 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002570
Winson Chung18f41f82012-05-09 13:28:10 -07002571 void disableWallpaperIfInAllApps() {
2572 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002573 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002574 if (mAppsCustomizeTabHost != null &&
2575 !mAppsCustomizeTabHost.isTransitioning()) {
2576 updateWallpaperVisibility(false);
2577 }
2578 }
2579 }
2580
Craig Mautner360310b2012-10-26 15:13:08 -07002581 private void setWorkspaceBackground(boolean workspace) {
2582 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002583 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002584 }
2585
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002586 void updateWallpaperVisibility(boolean visible) {
2587 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2588 int curflags = getWindow().getAttributes().flags
2589 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2590 if (wpflags != curflags) {
2591 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2592 }
Craig Mautner360310b2012-10-26 15:13:08 -07002593 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002594 }
2595
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002596 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2597 if (v instanceof LauncherTransitionable) {
2598 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2599 }
2600 }
2601
Michael Jurkabed61d22012-02-14 22:51:29 -08002602 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2603 if (v instanceof LauncherTransitionable) {
2604 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2605 }
Winson Chung70442722012-02-10 15:43:22 -08002606
2607 // Update the workspace transition step as well
2608 dispatchOnLauncherTransitionStep(v, 0f);
2609 }
2610
2611 private void dispatchOnLauncherTransitionStep(View v, float t) {
2612 if (v instanceof LauncherTransitionable) {
2613 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2614 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002615 }
2616
2617 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2618 if (v instanceof LauncherTransitionable) {
2619 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2620 }
Winson Chung70442722012-02-10 15:43:22 -08002621
2622 // Update the workspace transition step as well
2623 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002624 }
2625
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002626 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002627 * Things to test when changing the following seven functions.
2628 * - Home from workspace
2629 * - from center screen
2630 * - from other screens
2631 * - Home from all apps
2632 * - from center screen
2633 * - from other screens
2634 * - Back from all apps
2635 * - from center screen
2636 * - from other screens
2637 * - Launch app from workspace and quit
2638 * - with back
2639 * - with home
2640 * - Launch app from all apps and quit
2641 * - with back
2642 * - with home
2643 * - Go to a screen that's not the default, then all
2644 * apps, and launch and app, and go back
2645 * - with back
2646 * -with home
2647 * - On workspace, long press power and go back
2648 * - with back
2649 * - with home
2650 * - On all apps, long press power and go back
2651 * - with back
2652 * - with home
2653 * - On workspace, power off
2654 * - On all apps, power off
2655 * - Launch an app and turn off the screen while in that app
2656 * - Go back with home key
2657 * - Go back with back key TODO: make this not go to workspace
2658 * - From all apps
2659 * - From workspace
2660 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2661 * - From all apps
2662 * - From the center workspace
2663 * - From another workspace
2664 */
2665
2666 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002667 * Zoom the camera out from the workspace to reveal 'toView'.
2668 * Assumes that the view to show is anchored at either the very top or very bottom
2669 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002670 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002671 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002672 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002673 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002674 mStateAnimation.cancel();
2675 mStateAnimation = null;
2676 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002677 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002678
Winson Chungf0ea4d32011-06-06 14:27:16 -07002679 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2680 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2681 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002682 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002683 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002684 final int startDelay =
2685 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002686
Michael Jurkab3e22d92011-10-31 15:58:33 -07002687 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002688
Michael Jurkad74c9842011-07-10 12:44:21 -07002689 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002690 Animator workspaceAnim =
2691 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002692
2693 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002694 toView.setScaleX(scale);
2695 toView.setScaleY(scale);
2696 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2697 scaleAnim.
2698 scaleX(1f).scaleY(1f).
2699 setDuration(duration).
2700 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002701
Winson Chungf0ea4d32011-06-06 14:27:16 -07002702 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002703 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002704 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002705 .ofFloat(toView, "alpha", 0f, 1f)
2706 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002707 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002708 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2709 @Override
2710 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002711 if (animation == null) {
2712 throw new RuntimeException("animation is null");
2713 }
Winson Chung70442722012-02-10 15:43:22 -08002714 float t = (Float) animation.getAnimatedValue();
2715 dispatchOnLauncherTransitionStep(fromView, t);
2716 dispatchOnLauncherTransitionStep(toView, t);
2717 }
2718 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002719
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002720 // toView should appear right at the end of the workspace shrink
2721 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002722 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002723 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002724 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002725
2726 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002727 boolean animationCancelled = false;
2728
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002729 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002730 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002731 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002732 // Prepare the position
2733 toView.setTranslationX(0.0f);
2734 toView.setTranslationY(0.0f);
2735 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002736 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002737 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002738 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002739 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002740 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2741 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002742
Daniel Sandlere4f98912013-06-25 15:13:26 -04002743 if (mWorkspace != null
2744 && !springLoaded
2745 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002746 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002747 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002748 if (!animationCancelled) {
2749 updateWallpaperVisibility(false);
2750 }
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 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002756 }
2757
Adam Cohencff6af82011-09-13 14:51:53 -07002758 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002759 public void onAnimationCancel(Animator animation) {
2760 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002761 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002762 });
2763
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002764 if (workspaceAnim != null) {
2765 mStateAnimation.play(workspaceAnim);
2766 }
Michael Jurka1899a362011-11-03 13:50:45 -07002767
2768 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002769
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002770 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2771 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002772
2773 // If any of the objects being animated haven't been measured/laid out
2774 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002775 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002776 (mWorkspace.getMeasuredWidth() == 0) ||
2777 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002778 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002779 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002780
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002781 final AnimatorSet stateAnimation = mStateAnimation;
2782 final Runnable startAnimRunnable = new Runnable() {
2783 public void run() {
2784 // Check that mStateAnimation hasn't changed while
2785 // we waited for a layout/draw pass
2786 if (mStateAnimation != stateAnimation)
2787 return;
2788 setPivotsForZoom(toView, scale);
2789 dispatchOnLauncherTransitionStart(fromView, animated, false);
2790 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002791 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002792 }
2793 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002794 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002795 final ViewTreeObserver observer = toView.getViewTreeObserver();
2796 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2797 public void onGlobalLayout() {
2798 startAnimRunnable.run();
2799 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2800 }
2801 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002802 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002803 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002804 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002805 } else {
2806 toView.setTranslationX(0.0f);
2807 toView.setTranslationY(0.0f);
2808 toView.setScaleX(1.0f);
2809 toView.setScaleY(1.0f);
2810 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002811 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002812
Daniel Sandlere4f98912013-06-25 15:13:26 -04002813 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002814 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002815
2816 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002817 if (mSearchDropTargetBar != null) {
2818 mSearchDropTargetBar.hideSearchBar(false);
2819 }
Michael Jurkaabded662011-03-04 12:06:57 -08002820 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002821 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002822 dispatchOnLauncherTransitionStart(fromView, animated, false);
2823 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002824 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002825 dispatchOnLauncherTransitionStart(toView, animated, false);
2826 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002827 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002828 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002829 }
2830
2831 /**
2832 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002833 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002834 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002835 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002836 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2837 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002838
Michael Jurkab3e22d92011-10-31 15:58:33 -07002839 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002840 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002841 mStateAnimation.cancel();
2842 mStateAnimation = null;
2843 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002844 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002845
Winson Chungf0ea4d32011-06-06 14:27:16 -07002846 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002847 final int fadeOutDuration =
2848 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002849 final float scaleFactor = (float)
2850 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2851 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002852 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002853 Animator workspaceAnim = null;
2854
2855 if (toState == State.WORKSPACE) {
2856 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2857 workspaceAnim = mWorkspace.getChangeStateAnimation(
2858 Workspace.State.NORMAL, animated, stagger);
2859 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2860 workspaceAnim = mWorkspace.getChangeStateAnimation(
2861 Workspace.State.SPRING_LOADED, animated);
2862 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002863
Michael Jurkab3e22d92011-10-31 15:58:33 -07002864 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002865 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002866 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002867 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002868 final LauncherViewPropertyAnimator scaleAnim =
2869 new LauncherViewPropertyAnimator(fromView);
2870 scaleAnim.
2871 scaleX(scaleFactor).scaleY(scaleFactor).
2872 setDuration(duration).
2873 setInterpolator(new Workspace.ZoomInInterpolator());
2874
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002875 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002876 .ofFloat(fromView, "alpha", 1f, 0f)
2877 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002878 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002879 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2880 @Override
2881 public void onAnimationUpdate(ValueAnimator animation) {
2882 float t = 1f - (Float) animation.getAnimatedValue();
2883 dispatchOnLauncherTransitionStep(fromView, t);
2884 dispatchOnLauncherTransitionStep(toView, t);
2885 }
2886 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002887
Michael Jurka2ecf9952012-06-18 12:52:28 -07002888 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002889
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002890 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2891 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002892 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002893
2894 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002895 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002896 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002897 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002898 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002899 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2900 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002901 if (onCompleteRunnable != null) {
2902 onCompleteRunnable.run();
2903 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002904 mAppsCustomizeContent.updateCurrentPageScroll();
2905 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002906 }
2907 });
2908
Winson Chungf0ea4d32011-06-06 14:27:16 -07002909 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002910 if (workspaceAnim != null) {
2911 mStateAnimation.play(workspaceAnim);
2912 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002913 dispatchOnLauncherTransitionStart(fromView, animated, true);
2914 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002915 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002916 } else {
2917 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002918 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002919 dispatchOnLauncherTransitionStart(fromView, animated, true);
2920 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002921 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002922 dispatchOnLauncherTransitionStart(toView, animated, true);
2923 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002924 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002925 }
2926
Michael Jurkae326f182011-11-21 14:05:46 -08002927 @Override
2928 public void onTrimMemory(int level) {
2929 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002930 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002931 mAppsCustomizeTabHost.onTrimMemory();
2932 }
2933 }
2934
Winson Chung18f41f82012-05-09 13:28:10 -07002935 @Override
2936 public void onWindowFocusChanged(boolean hasFocus) {
2937 if (!hasFocus) {
2938 // When another window occludes launcher (like the notification shade, or recents),
2939 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2940 updateWallpaperVisibility(true);
2941 } else {
2942 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002943 mWorkspace.postDelayed(new Runnable() {
2944 @Override
2945 public void run() {
2946 disableWallpaperIfInAllApps();
2947 }
2948 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002949 }
2950 }
2951
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002952 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002953 showWorkspace(animated, null);
2954 }
2955
2956 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002957 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002958 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002959 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002960 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002961
Winson Chungc7d2b602012-05-16 17:02:20 -07002962 // Show the search bar (only animate if we were showing the drop target bar in spring
2963 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002964 if (mSearchDropTargetBar != null) {
2965 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2966 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002967
Michael Jurkab737ee62011-11-15 15:57:22 -08002968 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002969 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002970
2971 // Set focus to the AppsCustomize button
2972 if (mAllAppsButton != null) {
2973 mAllAppsButton.requestFocus();
2974 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002975 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002976
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002977 // Change the state *after* we've called all the transition code
2978 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002979
Winson Chung5fb63472011-02-02 17:03:37 -08002980 // Resume the auto-advance of widgets
2981 mUserPresent = true;
2982 updateRunning();
2983
alanv1d4fde62012-10-17 13:15:47 -07002984 // Send an accessibility event to announce the context change
2985 getWindow().getDecorView()
2986 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002987
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002988 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002989 }
2990
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002991 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002992 }
2993
Michael Jurkab3e22d92011-10-31 15:58:33 -07002994 void showAllApps(boolean animated) {
2995 if (mState != State.WORKSPACE) return;
2996
2997 showAppsCustomizeHelper(animated, false);
2998 mAppsCustomizeTabHost.requestFocus();
2999
Michael Jurkab3e22d92011-10-31 15:58:33 -07003000 // Change the state *after* we've called all the transition code
3001 mState = State.APPS_CUSTOMIZE;
3002
3003 // Pause the auto-advance of widgets until we are out of AllApps
3004 mUserPresent = false;
3005 updateRunning();
3006 closeFolder();
3007
3008 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003009 getWindow().getDecorView()
3010 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003011 }
3012
Adam Cohen7777d962011-08-18 18:58:38 -07003013 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003014 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003015 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08003016 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07003017 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003018 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003019 }
Adam Cohen7777d962011-08-18 18:58:38 -07003020
Adam Cohened66b2b2012-01-23 17:28:51 -08003021 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3022 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003023 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3024
Winson Chunge7a03942011-08-05 15:05:12 -07003025 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003026 @Override
3027 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003028 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003029 // Before we show workspace, hide all apps again because
3030 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3031 // clean up our state transition functions
3032 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003033 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003034 } else {
3035 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003036 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003037 }
3038 }, (extendedDelay ?
3039 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3040 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3041 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003042
Michael Jurkad3ef3062010-11-23 16:23:58 -08003043 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003044 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003045 final boolean animated = true;
3046 final boolean springLoaded = true;
3047 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003048 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003049 }
3050 // Otherwise, we are not in spring loaded mode, so don't do anything.
3051 }
3052
Michael Jurkab737ee62011-11-15 15:57:22 -08003053 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003054 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003055 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003056 }
3057 }
3058
3059 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003060 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003061 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003062 if (mDividerAnimator != null) {
3063 mDividerAnimator.cancel();
3064 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003065 mDividerAnimator = null;
3066 }
3067 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003068 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003069 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003070 int duration = 0;
3071 if (mSearchDropTargetBar != null) {
3072 duration = mSearchDropTargetBar.getTransitionInDuration();
3073 }
3074 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003075 mDividerAnimator.start();
3076 }
3077 }
3078 }
3079
Michael Jurkab3e22d92011-10-31 15:58:33 -07003080 void lockAllApps() {
3081 // TODO
3082 }
3083
3084 void unlockAllApps() {
3085 // TODO
3086 }
3087
Winson Chungf0ea4d32011-06-06 14:27:16 -07003088 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003089 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003090 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003091 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003092 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003093 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003094 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003095 int duration = 0;
3096 if (mSearchDropTargetBar != null) {
3097 duration = mSearchDropTargetBar.getTransitionInDuration();
3098 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003099 mHotseat.animate().alpha(1f).setDuration(duration);
3100 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003101 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003102 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003103 }
3104 }
3105 }
3106
3107 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003108 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003109 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003110 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003111 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003112 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003113 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003114 int duration = 0;
3115 if (mSearchDropTargetBar != null) {
3116 duration = mSearchDropTargetBar.getTransitionOutDuration();
3117 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003118 mHotseat.animate().alpha(0f).setDuration(duration);
3119 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003120 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003121 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003122 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003123 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003124 }
3125
Patrick Dubroy5f445422011-02-18 14:35:21 -08003126 /**
3127 * Add an item from all apps or customize onto the given workspace screen.
3128 * If layout is null, add to the current screen.
3129 */
3130 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003131 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003132 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003133 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003134 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003135
Winson Chungdff8ebb2011-09-08 17:25:31 -07003136 /** Maps the current orientation to an index for referencing orientation correct global icons */
3137 private int getCurrentOrientationIndexForGlobalIcons() {
3138 // default - 0, landscape - 1
3139 switch (getResources().getConfiguration().orientation) {
3140 case Configuration.ORIENTATION_LANDSCAPE:
3141 return 1;
3142 default:
3143 return 0;
3144 }
3145 }
3146
Michael Jurkaae65ee32012-04-30 11:45:54 -07003147 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003148 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003149 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003150 // Look for the toolbar icon specified in the activity meta-data
3151 Bundle metaData = packageManager.getActivityInfo(
3152 activityName, PackageManager.GET_META_DATA).metaData;
3153 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003154 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003155 if (iconResId != 0) {
3156 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003157 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003158 }
3159 }
3160 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003161 // This can happen if the activity defines an invalid drawable
3162 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3163 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003164 } catch (Resources.NotFoundException nfe) {
3165 // This can happen if the activity defines an invalid drawable
3166 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3167 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003168 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003169 return null;
3170 }
3171
3172 // if successful in getting icon, return it; otherwise, set button to use default drawable
3173 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003174 int buttonId, ComponentName activityName, int fallbackDrawableId,
3175 String toolbarResourceName) {
3176 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003177 Resources r = getResources();
3178 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3179 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003180
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003181 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003182 // If we were unable to find the icon via the meta-data, use a generic one
3183 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003184 toolbarIcon = r.getDrawable(fallbackDrawableId);
3185 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003186 if (button != null) {
3187 button.setCompoundDrawables(toolbarIcon, null, null, null);
3188 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003189 return null;
3190 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003191 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003192 if (button != null) {
3193 button.setCompoundDrawables(toolbarIcon, null, null, null);
3194 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003195 return toolbarIcon.getConstantState();
3196 }
3197 }
3198
3199 // if successful in getting icon, return it; otherwise, set button to use default drawable
3200 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003201 int buttonId, ComponentName activityName, int fallbackDrawableId,
3202 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003203 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003204 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003205
Michael Jurka19e0fc52011-07-22 18:00:21 -07003206 if (button != null) {
3207 // If we were unable to find the icon via the meta-data, use a
3208 // generic one
3209 if (toolbarIcon == null) {
3210 button.setImageResource(fallbackDrawableId);
3211 } else {
3212 button.setImageDrawable(toolbarIcon);
3213 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003214 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003215
3216 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3217
Michael Jurka0423dcf2010-10-05 14:56:18 -07003218 }
3219
Winson Chung1b884092012-06-08 17:13:02 -07003220 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003221 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003222 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003223 }
3224
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003225 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003226 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003227 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003228 }
3229
Winson Chungbb185bd2011-11-21 12:31:42 -08003230 private void invalidatePressedFocusedStates(View container, View button) {
3231 if (container instanceof HolographicLinearLayout) {
3232 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3233 layout.invalidatePressedFocusedStates();
3234 } else if (button instanceof HolographicImageView) {
3235 HolographicImageView view = (HolographicImageView) button;
3236 view.invalidatePressedFocusedStates();
3237 }
3238 }
3239
Winson Chungc51db6a2011-10-05 11:44:49 -07003240 private boolean updateGlobalSearchIcon() {
3241 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003242 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003243 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003244 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003245
Winson Chung1cad91e2011-05-25 17:41:01 -07003246 final SearchManager searchManager =
3247 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3248 ComponentName activityName = searchManager.getGlobalSearchActivity();
3249 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003250 int coi = getCurrentOrientationIndexForGlobalIcons();
3251 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003252 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3253 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3254 if (sGlobalSearchIcon[coi] == null) {
3255 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3256 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3257 TOOLBAR_ICON_METADATA_NAME);
3258 }
3259
Winson Chungc51db6a2011-10-05 11:44:49 -07003260 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3261 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003262 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003263 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003264 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003265 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003266 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3267 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3268 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003269 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003270 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003271 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003272 }
3273 }
3274
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003275 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003276 final View searchButtonContainer = findViewById(R.id.search_button_container);
3277 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003278 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003279 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003280 }
3281
Winson Chungc51db6a2011-10-05 11:44:49 -07003282 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003283 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003284 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003285
Winson Chungc51db6a2011-10-05 11:44:49 -07003286 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003287 final SearchManager searchManager =
3288 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3289 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3290
3291 ComponentName activityName = null;
3292 if (globalSearchActivity != null) {
3293 // Check if the global search activity handles voice search
3294 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3295 intent.setPackage(globalSearchActivity.getPackageName());
3296 activityName = intent.resolveActivity(getPackageManager());
3297 }
3298
3299 if (activityName == null) {
3300 // Fallback: check if an activity other than the global search activity
3301 // resolves this
3302 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3303 activityName = intent.resolveActivity(getPackageManager());
3304 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003305 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003306 int coi = getCurrentOrientationIndexForGlobalIcons();
3307 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003308 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3309 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3310 if (sVoiceSearchIcon[coi] == null) {
3311 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3312 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3313 TOOLBAR_ICON_METADATA_NAME);
3314 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003315 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003316 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003317 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003318 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003319 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003320 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003321 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003322 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003323 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003324 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003325 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003326 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003327
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003328 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003329 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3330 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003331 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003332 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003333 }
3334
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003335 public void setVoiceButtonProxyVisible(boolean visible) {
3336 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3337 if (voiceButtonProxy != null) {
3338 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3339 }
3340 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003341 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003342 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003343 */
3344 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003345 final View marketButton = findViewById(R.id.market_button);
3346 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3347 // Find the app market activity by resolving an intent.
3348 // (If multiple app markets are installed, it will return the ResolverActivity.)
3349 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003350 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003351 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003352 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003353 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003354 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3355 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003356 marketButton.setVisibility(View.VISIBLE);
3357 } else {
3358 // We should hide and disable the view so that we don't try and restore the visibility
3359 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3360 marketButton.setVisibility(View.GONE);
3361 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003362 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003363 }
3364
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003365 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003366 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3367 Resources r = getResources();
3368 Drawable marketIconDrawable = d.newDrawable(r);
3369 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3370 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3371 marketIconDrawable.setBounds(0, 0, w, h);
3372
3373 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003374 }
3375
Michael Jurkad7c28052012-04-27 15:43:36 -07003376 @Override
3377 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003378 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003379 final List<CharSequence> text = event.getText();
3380 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003381 // Populate event with a fake title based on the current state.
3382 if (mState == State.APPS_CUSTOMIZE) {
3383 text.add(getString(R.string.all_apps_button_label));
3384 } else {
3385 text.add(getString(R.string.all_apps_home_button_label));
3386 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003387 return result;
3388 }
3389
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003390 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003391 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003392 */
3393 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3394 @Override
3395 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003396 closeSystemDialogs();
3397 }
3398 }
3399
3400 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003401 * Receives notifications whenever the appwidgets are reset.
3402 */
3403 private class AppWidgetResetObserver extends ContentObserver {
3404 public AppWidgetResetObserver() {
3405 super(new Handler());
3406 }
3407
3408 @Override
3409 public void onChange(boolean selfChange) {
3410 onAppWidgetReset();
3411 }
3412 }
3413
3414 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003415 * If the activity is currently paused, signal that we need to run the passed Runnable
3416 * in onResume.
3417 *
3418 * This needs to be called from incoming places where resources might have been loaded
3419 * while we are paused. That is becaues the Configuration might be wrong
3420 * when we're not running, and if it comes back to what it was when we
3421 * were paused, we are not restarted.
3422 *
3423 * Implementation of the method from LauncherModel.Callbacks.
3424 *
3425 * @return true if we are currently paused. The caller might be able to
3426 * skip some work in that case since we will come back again.
3427 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003428 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003429 if (mPaused) {
3430 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003431 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003432 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003433 }
3434 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003435 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003436 return true;
3437 } else {
3438 return false;
3439 }
3440 }
3441
Michael Jurkac402cd92013-05-20 15:49:32 +02003442 private boolean waitUntilResume(Runnable run) {
3443 return waitUntilResume(run, false);
3444 }
3445
Michael Jurka1e2f4652013-07-08 18:03:46 -07003446 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003447 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003448 }
3449
3450 public void removeOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003451 mOnResumeCallbacks.remove(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003452 }
3453
Michael Jurka7607c2f2013-04-03 14:33:19 -07003454 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003455 * If the activity is currently paused, signal that we need to re-run the loader
3456 * in onResume.
3457 *
3458 * This needs to be called from incoming places where resources might have been loaded
3459 * while we are paused. That is becaues the Configuration might be wrong
3460 * when we're not running, and if it comes back to what it was when we
3461 * were paused, we are not restarted.
3462 *
3463 * Implementation of the method from LauncherModel.Callbacks.
3464 *
3465 * @return true if we are currently paused. The caller might be able to
3466 * skip some work in that case since we will come back again.
3467 */
3468 public boolean setLoadOnResume() {
3469 if (mPaused) {
3470 Log.i(TAG, "setLoadOnResume");
3471 mOnResumeNeedsLoad = true;
3472 return true;
3473 } else {
3474 return false;
3475 }
3476 }
3477
3478 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003480 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003482 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003483 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003484 } else {
3485 return SCREEN_COUNT / 2;
3486 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003488
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 /**
3490 * Refreshes the shortcuts shown on the workspace.
3491 *
3492 * Implementation of the method from LauncherModel.Callbacks.
3493 */
3494 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003495 // If we're starting binding all over again, clear any bind calls we'd postponed in
3496 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3497 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003498 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003499
Michael Jurka7607c2f2013-04-03 14:33:19 -07003500 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003501 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003502 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003503 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 int count = workspace.getChildCount();
3505 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003506 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003507 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3508 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003509 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003510 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003511 if (mHotseat != null) {
3512 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003513 }
3514 }
3515
Adam Cohendcd297f2013-06-18 13:13:40 -07003516 @Override
3517 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003518 bindAddScreens(orderedScreenIds);
3519 mWorkspace.addExtraEmptyScreen();
3520 }
3521
3522 @Override
3523 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003524 int count = orderedScreenIds.size();
3525 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003526 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003527 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003528 }
3529
Adam Cohen39a06042013-07-19 14:30:12 -07003530 private boolean shouldShowWeightWatcher() {
3531 String spKey = LauncherAppState.getSharedPreferencesKey();
3532 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3533 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3534
3535 return show;
3536 }
3537
3538 private void toggleShowWeightWatcher() {
3539 String spKey = LauncherAppState.getSharedPreferencesKey();
3540 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3541 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3542
3543 show = !show;
3544
3545 SharedPreferences.Editor editor = sp.edit();
3546 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3547 editor.commit();
3548
3549 if (mWeightWatcher != null) {
3550 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3551 }
3552 }
3553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003554 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 * Bind the items start-end from the list.
3556 *
3557 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003558 */
Winson Chung64359a52013-07-08 17:17:08 -07003559 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3560 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003561 if (waitUntilResume(new Runnable() {
3562 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003563 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003564 }
3565 })) {
3566 return;
3567 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003568
Winson Chungf0c6ae02012-03-21 16:10:31 -07003569 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3570 Set<String> newApps = new HashSet<String>();
3571 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3572
Winson Chung64359a52013-07-08 17:17:08 -07003573 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3574 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003575 Workspace workspace = mWorkspace;
3576 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003578
3579 // Short circuit if we are loading dock items for a configuration which has no dock
3580 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3581 mHotseat == null) {
3582 continue;
3583 }
3584
Joe Onorato9c1289c2009-08-17 11:03:03 -04003585 switch (item.itemType) {
3586 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3587 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003588 ShortcutInfo info = (ShortcutInfo) item;
3589 String uri = info.intent.toUri(0).toString();
3590 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003591
Winson Chung64359a52013-07-08 17:17:08 -07003592 /*
3593 * TODO: FIX collision case
3594 */
Winson Chungce376632013-07-11 16:12:41 -07003595 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3596 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3597 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3598 throw new RuntimeException("OCCUPIED");
3599 }
Winson Chung64359a52013-07-08 17:17:08 -07003600 }
3601
Adam Cohendcd297f2013-06-18 13:13:40 -07003602 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3603 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003604 boolean animateIconUp = false;
3605 synchronized (newApps) {
3606 if (newApps.contains(uri)) {
3607 animateIconUp = newApps.remove(uri);
3608 }
3609 }
Winson Chung64359a52013-07-08 17:17:08 -07003610 if (forceAnimateIcons) {
3611 // Animate all the applications up now
3612 shortcut.setAlpha(0f);
3613 shortcut.setScaleX(0f);
3614 shortcut.setScaleY(0f);
3615 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3616 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003617 // Prepare the view to be animated up
3618 shortcut.setAlpha(0f);
3619 shortcut.setScaleX(0f);
3620 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003621 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003622 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3623 mNewShortcutAnimateViews.add(shortcut);
3624 }
3625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003627 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003628 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003629 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003630 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003631 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3632 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003634 default:
3635 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003637 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003638
Winson Chung64359a52013-07-08 17:17:08 -07003639 if (forceAnimateIcons) {
3640 // We post the animation slightly delayed to prevent slowdowns when we are loading
3641 // right after we return to launcher.
3642 mWorkspace.postDelayed(new Runnable() {
3643 public void run() {
3644 anim.playTogether(bounceAnims);
3645 anim.start();
3646 }
3647 }, NEW_APPS_ANIMATION_DELAY);
3648 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003650 }
3651
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 /**
3653 * Implementation of the method from LauncherModel.Callbacks.
3654 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003655 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3656 if (waitUntilResume(new Runnable() {
3657 public void run() {
3658 bindFolders(folders);
3659 }
3660 })) {
3661 return;
3662 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003663 sFolders.clear();
3664 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003665 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003666
3667 /**
3668 * Add the views for a widget to the workspace.
3669 *
3670 * Implementation of the method from LauncherModel.Callbacks.
3671 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003672 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3673 if (waitUntilResume(new Runnable() {
3674 public void run() {
3675 bindAppWidget(item);
3676 }
3677 })) {
3678 return;
3679 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003680
Daniel Sandler843e8602010-06-07 14:59:01 -04003681 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3682 if (DEBUG_WIDGETS) {
3683 Log.d(TAG, "bindAppWidget: " + item);
3684 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003685 final Workspace workspace = mWorkspace;
3686
3687 final int appWidgetId = item.appWidgetId;
3688 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003689 if (DEBUG_WIDGETS) {
3690 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3691 }
3692
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3694
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003696 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697
Adam Cohendcd297f2013-06-18 13:13:40 -07003698 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003700 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3701
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 workspace.requestLayout();
3703
Daniel Sandler843e8602010-06-07 14:59:01 -04003704 if (DEBUG_WIDGETS) {
3705 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3706 + (SystemClock.uptimeMillis()-start) + "ms");
3707 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003708 }
3709
Adam Cohen1462de32012-07-24 22:34:36 -07003710 public void onPageBoundSynchronously(int page) {
3711 mSynchronouslyBoundPages.add(page);
3712 }
3713
Joe Onorato9c1289c2009-08-17 11:03:03 -04003714 /**
3715 * Callback saying that there aren't any more items to bind.
3716 *
3717 * Implementation of the method from LauncherModel.Callbacks.
3718 */
Adam Cohene25af792013-06-06 23:08:25 -07003719 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003720 if (waitUntilResume(new Runnable() {
3721 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003722 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003723 }
3724 })) {
3725 return;
3726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 if (mSavedState != null) {
3728 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003729 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 mSavedState = null;
3732 }
3733
Winson Chung98ca0f72013-07-29 12:58:51 -07003734 // Create the custom content page here before onLayout to prevent flashing
3735 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3736 mWorkspace.createCustomContentPage();
3737 }
3738
Adam Cohen1462de32012-07-24 22:34:36 -07003739 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003741 // If we received the result of any pending adds while the loader was running (e.g. the
3742 // widget configuration forced an orientation change), process them now.
3743 for (int i = 0; i < sPendingAddList.size(); i++) {
3744 completeAdd(sPendingAddList.get(i));
3745 }
3746 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003747
Winson Chungc51db6a2011-10-05 11:44:49 -07003748 // Update the market app icon as necessary (the other icons will be managed in response to
3749 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003750 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003751
Winson Chungf0c6ae02012-03-21 16:10:31 -07003752 // Animate up any icons as necessary
3753 if (mVisible || mWorkspaceLoading) {
3754 Runnable newAppsRunnable = new Runnable() {
3755 @Override
3756 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003757 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003758 }
3759 };
Winson Chunga2413752012-04-03 14:22:34 -07003760
Adam Cohendcd297f2013-06-18 13:13:40 -07003761 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3762 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003763 if (canRunNewAppsAnimation()) {
3764 // If the user has not interacted recently, then either snap to the new page to show
3765 // the new-apps animation or just run them if they are to appear on the current page
3766 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003767 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003768 } else {
3769 runNewAppsAnimation(false);
3770 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003771 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003772 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003773 // are on another page (or just normally if they are added to the current page)
3774 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003775 }
3776 }
3777
3778 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003779 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003780 mWorkspace.stripDuplicateApps();
3781 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003782 }
Adam Cohen99894d92013-06-14 11:22:59 -07003783
Adam Cohen66a01fd2013-06-11 12:48:00 -07003784 mWorkspace.post(new Runnable() {
3785 @Override
3786 public void run() {
3787 onFinishBindingItems();
3788 }
3789 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003790 }
3791
Winson Chunga2413752012-04-03 14:22:34 -07003792 private boolean canRunNewAppsAnimation() {
3793 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3794 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3795 }
3796
Winson Chungc9168342013-06-26 14:54:55 -07003797 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3798 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3799 PropertyValuesHolder.ofFloat("alpha", 1f),
3800 PropertyValuesHolder.ofFloat("scaleX", 1f),
3801 PropertyValuesHolder.ofFloat("scaleY", 1f));
3802 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3803 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3804 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3805 return bounceAnim;
3806 }
3807
Winson Chungf0c6ae02012-03-21 16:10:31 -07003808 /**
3809 * Runs a new animation that scales up icons that were added while Launcher was in the
3810 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003811 *
3812 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003813 */
Winson Chunga2413752012-04-03 14:22:34 -07003814 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003815 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003816 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003817
3818 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003819 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3820 @Override
3821 public int compare(View a, View b) {
3822 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3823 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3824 int cellCountX = LauncherModel.getCellCountX();
3825 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3826 }
3827 });
Winson Chunga2413752012-04-03 14:22:34 -07003828
3829 // Animate each of the views in place (or show them immediately if requested)
3830 if (immediate) {
3831 for (View v : mNewShortcutAnimateViews) {
3832 v.setAlpha(1f);
3833 v.setScaleX(1f);
3834 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003835 }
Winson Chunga2413752012-04-03 14:22:34 -07003836 } else {
3837 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3838 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003839 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003840 }
3841 anim.playTogether(bounceAnims);
3842 anim.addListener(new AnimatorListenerAdapter() {
3843 @Override
3844 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003845 if (mWorkspace != null) {
3846 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3847 }
Winson Chunga2413752012-04-03 14:22:34 -07003848 }
3849 });
3850 anim.start();
3851 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003852
3853 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003854 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003855 mNewShortcutAnimateViews.clear();
3856 new Thread("clearNewAppsThread") {
3857 public void run() {
3858 mSharedPrefs.edit()
3859 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3860 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3861 .commit();
3862 }
3863 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003864 }
3865
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003866 @Override
3867 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003868 boolean searchVisible = updateGlobalSearchIcon();
3869 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003870 if (mSearchDropTargetBar != null) {
3871 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3872 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003873 }
3874
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003875 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003876 * Add the icons for all apps.
3877 *
3878 * Implementation of the method from LauncherModel.Callbacks.
3879 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003880 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003881 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3882 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003883 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003884 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003885 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003886 }
3887
3888 /**
3889 * A package was updated.
3890 *
3891 * Implementation of the method from LauncherModel.Callbacks.
3892 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003893 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3894 if (waitUntilResume(new Runnable() {
3895 public void run() {
3896 bindAppsUpdated(apps);
3897 }
3898 })) {
3899 return;
3900 }
3901
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003902 if (mWorkspace != null) {
3903 mWorkspace.updateShortcuts(apps);
3904 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003905 }
3906
3907 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003908 * A package was uninstalled. We take both the super set of packageNames
3909 * in addition to specific applications to remove, the reason being that
3910 * this can be called when a package is updated as well. In that scenario,
3911 * we only remove specific components from the workspace, where as
3912 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003913 *
3914 * Implementation of the method from LauncherModel.Callbacks.
3915 */
Winson Chung83892cc2013-05-01 16:53:33 -07003916 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3917 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003918 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003919 if (waitUntilResume(new Runnable() {
3920 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003921 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003922 }
3923 })) {
3924 return;
3925 }
3926
Winson Chung64359a52013-07-08 17:17:08 -07003927 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003928 mWorkspace.removeItemsByPackageName(packageNames);
3929 } else {
3930 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003931 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003932
Winson Chunga1820962011-10-03 16:31:06 -07003933 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003934 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935 }
Joe Onoratobe386092009-11-17 17:32:16 -08003936
3937 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003938 * A number of packages were updated.
3939 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003940 private ArrayList<Object> mWidgetsAndShortcuts;
3941 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003942 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003943 bindPackagesUpdated(mWidgetsAndShortcuts);
3944 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003945 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003946 };
3947
3948 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3949 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3950 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003951 return;
3952 }
3953
Winson Chung64359a52013-07-08 17:17:08 -07003954 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003955 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003956 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003957 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003958 }
3959
Winson Chung400438b2011-01-16 17:53:48 -08003960 private int mapConfigurationOriActivityInfoOri(int configOri) {
3961 final Display d = getWindowManager().getDefaultDisplay();
3962 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3963 switch (d.getRotation()) {
3964 case Surface.ROTATION_0:
3965 case Surface.ROTATION_180:
3966 // We are currently in the same basic orientation as the natural orientation
3967 naturalOri = configOri;
3968 break;
3969 case Surface.ROTATION_90:
3970 case Surface.ROTATION_270:
3971 // We are currently in the other basic orientation to the natural orientation
3972 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3973 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3974 break;
3975 }
3976
3977 int[] oriMap = {
3978 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3979 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3980 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3981 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3982 };
3983 // Since the map starts at portrait, we need to offset if this device's natural orientation
3984 // is landscape.
3985 int indexOffset = 0;
3986 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3987 indexOffset = 1;
3988 }
3989 return oriMap[(d.getRotation() + indexOffset) % 4];
3990 }
Adam Cohen446e9402011-09-15 18:21:21 -07003991
Winson Chung4b919f82012-05-01 10:44:08 -07003992 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003993 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003994 getResources().getBoolean(R.bool.allow_rotation);
3995 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003996 }
Winson Chung4b919f82012-05-01 10:44:08 -07003997 public void lockScreenOrientation() {
3998 if (isRotationEnabled()) {
3999 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4000 .getConfiguration().orientation));
4001 }
4002 }
4003 public void unlockScreenOrientation(boolean immediate) {
4004 if (isRotationEnabled()) {
4005 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004006 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004007 } else {
4008 mHandler.postDelayed(new Runnable() {
4009 public void run() {
4010 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4011 }
4012 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004013 }
Winson Chung4b919f82012-05-01 10:44:08 -07004014 }
Winson Chung400438b2011-01-16 17:53:48 -08004015 }
4016
Winson Chung82f55532011-08-09 14:14:23 -07004017 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004018 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004019 if (DISABLE_CLINGS) {
4020 return false;
4021 }
4022
Brett Chabot2a9e2282011-08-23 15:03:13 -07004023 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004024 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004025
Michael Jurkae233a8b2013-03-19 13:49:20 +01004026 // Restricted secondary users (child mode) will potentially have very few apps
4027 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004028// boolean supportsLimitedUsers =
4029// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4030// Account[] accounts = AccountManager.get(this).getAccounts();
4031// if (supportsLimitedUsers && accounts.length == 0) {
4032// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4033// Bundle restrictions = um.getUserRestrictions();
4034// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4035// return false;
4036// }
4037// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004038 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004039 }
Michael Jurka22143132012-10-04 17:42:38 +02004040
Winson Chung7d7541e2011-09-16 20:14:36 -07004041 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004042 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004043 if (cling != null) {
4044 cling.init(this, positionData);
4045 cling.setVisibility(View.VISIBLE);
4046 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4047 if (animate) {
4048 cling.buildLayer();
4049 cling.setAlpha(0f);
4050 cling.animate()
4051 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004052 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004053 .setDuration(SHOW_CLING_DURATION)
4054 .setStartDelay(delay)
4055 .start();
4056 } else {
4057 cling.setAlpha(1f);
4058 }
Michael Jurka22143132012-10-04 17:42:38 +02004059 cling.setFocusableInTouchMode(true);
4060 cling.post(new Runnable() {
4061 public void run() {
4062 cling.setFocusable(true);
4063 cling.requestFocus();
4064 }
4065 });
4066 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4067 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004068 }
4069 return cling;
4070 }
Michael Jurka22143132012-10-04 17:42:38 +02004071
Winson Chung7d7541e2011-09-16 20:14:36 -07004072 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004073 // To catch cases where siblings of top-level views are made invisible, just check whether
4074 // the cling is directly set to GONE before dismissing it.
4075 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004076 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004077 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004078 anim.addListener(new AnimatorListenerAdapter() {
4079 public void onAnimationEnd(Animator animation) {
4080 cling.setVisibility(View.GONE);
4081 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004082 // We should update the shared preferences on a background thread
4083 new Thread("dismissClingThread") {
4084 public void run() {
4085 SharedPreferences.Editor editor = mSharedPrefs.edit();
4086 editor.putBoolean(flag, true);
4087 editor.commit();
4088 }
4089 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004090 };
4091 });
4092 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004093 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004094 }
4095 }
Michael Jurka22143132012-10-04 17:42:38 +02004096
Winson Chung9d9d74f2011-09-19 11:49:12 -07004097 private void removeCling(int id) {
4098 final View cling = findViewById(id);
4099 if (cling != null) {
4100 final ViewGroup parent = (ViewGroup) cling.getParent();
4101 parent.post(new Runnable() {
4102 @Override
4103 public void run() {
4104 parent.removeView(cling);
4105 }
4106 });
Michael Jurka22143132012-10-04 17:42:38 +02004107 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004108 }
4109 }
Michael Jurka974c3862012-05-22 22:00:31 -07004110
4111 private boolean skipCustomClingIfNoAccounts() {
4112 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4113 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4114 if (customCling) {
4115 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004116 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004117 Account[] accounts = am.getAccountsByType("com.google");
4118 return accounts.length == 0;
4119 }
4120 return false;
4121 }
4122
Winson Chung7d7541e2011-09-16 20:14:36 -07004123 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004125 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004126 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4127 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004128 // If we're not using the default workspace layout, replace workspace cling
4129 // with a custom workspace cling (usually specified in an overlay)
4130 // For now, only do this on tablets
4131 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004132 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004133 // Use a custom cling
4134 View cling = findViewById(R.id.workspace_cling);
4135 ViewGroup clingParent = (ViewGroup) cling.getParent();
4136 int clingIndex = clingParent.indexOfChild(cling);
4137 clingParent.removeViewAt(clingIndex);
4138 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4139 clingParent.addView(customCling, clingIndex);
4140 customCling.setId(R.id.workspace_cling);
4141 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004142 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004143 } else {
4144 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004145 }
4146 }
4147 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004148 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004149 if (isClingsEnabled() &&
4150 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004151 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004152 } else {
4153 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004154 }
4155 }
4156 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004157 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004158 if (isClingsEnabled() &&
4159 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4160 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004161 } else {
4162 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004163 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004164 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004165 }
4166 public boolean isFolderClingVisible() {
4167 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004168 if (cling != null) {
4169 return cling.getVisibility() == View.VISIBLE;
4170 }
4171 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004172 }
Winson Chung82f55532011-08-09 14:14:23 -07004173 public void dismissWorkspaceCling(View v) {
4174 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004175 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004176 }
4177 public void dismissAllAppsCling(View v) {
4178 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004179 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4180 }
4181 public void dismissFolderCling(View v) {
4182 Cling cling = (Cling) findViewById(R.id.folder_cling);
4183 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004184 }
4185
Winson Chung80baf5a2010-08-09 16:03:15 -07004186 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004187 * Prints out out state for debugging.
4188 */
4189 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004190 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004191 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004192 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4193 Log.d(TAG, "mRestoring=" + mRestoring);
4194 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4195 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004196 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004197 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004198
Winson Chung785d2eb2011-04-14 16:08:02 -07004199 if (mAppsCustomizeContent != null) {
4200 mAppsCustomizeContent.dumpState();
4201 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004202 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004203 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004204
4205 @Override
4206 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4207 super.dump(prefix, fd, writer, args);
4208 writer.println(" ");
4209 writer.println("Debug logs: ");
4210 for (int i = 0; i < sDumpLogs.size(); i++) {
4211 writer.println(" " + sDumpLogs.get(i));
4212 }
4213 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004214
4215 public static void dumpDebugLogsToConsole() {
4216 Log.d(TAG, "");
4217 Log.d(TAG, "*********************");
4218 Log.d(TAG, "Launcher debug logs: ");
4219 for (int i = 0; i < sDumpLogs.size(); i++) {
4220 Log.d(TAG, " " + sDumpLogs.get(i));
4221 }
4222 Log.d(TAG, "*********************");
4223 Log.d(TAG, "");
4224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004225}
Michael Jurka2763be32011-02-24 11:19:57 -08004226
Michael Jurkaabded662011-03-04 12:06:57 -08004227interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004228 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004229 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004230 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004231 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004232 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004233}