blob: e84c708ae92e13650f6c0da7a724cf85f02dd8ec [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 }
Winson Chunge4e50662012-01-23 14:45:13 -0800805
806 // Reset the pressed state of icons that were locked in the press state while activities
807 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800808 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800809 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800810 mWaitingForResume.setStayPressed(false);
811 }
Winson Chunge4e50662012-01-23 14:45:13 -0800812 if (mAppsCustomizeContent != null) {
813 // Resets the previous all apps icon press state
814 mAppsCustomizeContent.resetDrawableState();
815 }
Adam Cohen06dff352012-06-01 17:17:08 -0700816 // It is possible that widgets can receive updates while launcher is not in the foreground.
817 // Consequently, the widgets will be inflated in the orientation of the foreground activity
818 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
819 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700820 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700821
822 // Again, as with the above scenario, it's possible that one or more of the global icons
823 // were updated in the wrong orientation.
824 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200825 if (DEBUG_RESUME_TIME) {
826 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
827 }
Adam Cohen06dff352012-06-01 17:17:08 -0700828 }
829
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700831 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700832 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
833 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
834 // when Launcher resumes and we are still in AllApps.
835 updateWallpaperVisibility(true);
836
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700837 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700838 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800839 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700840 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700841 }
Romain Guycbb89e42009-06-08 15:52:54 -0700842
Adam Cohen66a01fd2013-06-11 12:48:00 -0700843 protected void onFinishBindingItems() {
844 }
845
Adam Cohenbffe7452013-07-22 18:21:45 -0700846 QSBScroller mQsbScroller = new QSBScroller() {
847 int scrollY = 0;
848
849 @Override
850 public void setScrollY(int scroll) {
851 scrollY = scroll;
852
853 if (mWorkspace.isOnOrMovingToCustomContent()) {
854 mSearchDropTargetBar.setTranslationY(- scrollY);
855 }
856 }
857 };
858
859 public void resetQSBScroll() {
860 mSearchDropTargetBar.animate().translationY(0).start();
861 }
862
863 public interface CustomContentCallbacks {
864 // Custom content is completely shown
865 public void onShow();
866
867 // Custom content is completely hidden
868 public void onHide();
869 }
870
871 public interface QSBScroller {
872 public void setScrollY(int scrollY);
873 }
874
Adam Cohen66a01fd2013-06-11 12:48:00 -0700875 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700876 public QSBScroller addToCustomContentPage(View customContent) {
877 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700878 }
879
Winson Chung98ca0f72013-07-29 12:58:51 -0700880 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700881 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700882 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700883 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700884 }
885
Adam Cohenedb40762013-07-18 16:45:45 -0700886 // The custom content needs to offset its content to account for the QSB
887 public int getTopOffsetForCustomContent() {
888 return mWorkspace.getPaddingTop();
889 }
890
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700891 @Override
892 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893 // Flag the loader to stop early before switching
894 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700895 if (mAppsCustomizeContent != null) {
896 mAppsCustomizeContent.surrender();
897 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800898 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700899 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700900
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800901 // We can't hide the IME if it was forced open. So don't bother
902 /*
903 @Override
904 public void onWindowFocusChanged(boolean hasFocus) {
905 super.onWindowFocusChanged(hasFocus);
906
907 if (hasFocus) {
908 final InputMethodManager inputManager = (InputMethodManager)
909 getSystemService(Context.INPUT_METHOD_SERVICE);
910 WindowManager.LayoutParams lp = getWindow().getAttributes();
911 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
912 android.os.Handler()) {
913 protected void onReceiveResult(int resultCode, Bundle resultData) {
914 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
915 }
916 });
917 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
918 }
919 }
920 */
921
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 private boolean acceptFilter() {
923 final InputMethodManager inputManager = (InputMethodManager)
924 getSystemService(Context.INPUT_METHOD_SERVICE);
925 return !inputManager.isFullscreenMode();
926 }
927
928 @Override
929 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700930 final int uniChar = event.getUnicodeChar();
931 final boolean handled = super.onKeyDown(keyCode, event);
932 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
933 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
935 keyCode, event);
936 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700937 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700938 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700939 // showSearchDialog()
940 // If there are multiple keystrokes before the search dialog takes focus,
941 // onSearchRequested() will be called for every keystroke,
942 // but it is idempotent, so it's fine.
943 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945 }
946
Joe Onorato8a9625e2010-01-28 15:55:35 -0800947 // Eat the long press event so the keyboard doesn't come up.
948 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
949 return true;
950 }
951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 return handled;
953 }
954
Karl Rosaen138a0412009-04-23 19:00:21 -0700955 private String getTypedText() {
956 return mDefaultKeySsb.toString();
957 }
958
959 private void clearTypedText() {
960 mDefaultKeySsb.clear();
961 mDefaultKeySsb.clearSpans();
962 Selection.setSelection(mDefaultKeySsb, 0);
963 }
964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700966 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
967 * State
968 */
969 private static State intToState(int stateOrdinal) {
970 State state = State.WORKSPACE;
971 final State[] stateValues = State.values();
972 for (int i = 0; i < stateValues.length; i++) {
973 if (stateValues[i].ordinal() == stateOrdinal) {
974 state = stateValues[i];
975 break;
976 }
977 }
978 return state;
979 }
980
981 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 * Restores the previous state, if it exists.
983 *
984 * @param savedState The previous state.
985 */
986 private void restoreState(Bundle savedState) {
987 if (savedState == null) {
988 return;
989 }
990
Michael Jurka883f55b2010-10-21 15:47:14 -0700991 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700992 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700993 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800994 }
995
Winson Chungf0c6ae02012-03-21 16:10:31 -0700996 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700998 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000
Winson Chung3d503fb2011-07-13 17:25:49 -07001001 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001003
Winson Chung3d503fb2011-07-13 17:25:49 -07001004 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1005 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001007 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1008 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001009 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1010 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1011 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001012 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 mRestoring = true;
1014 }
1015
1016 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1017 if (renameFolder) {
1018 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001019 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 mRestoring = true;
1021 }
Winson Chunga12a2502010-12-20 14:41:35 -08001022
Winson Chung785d2eb2011-04-14 16:08:02 -07001023 // Restore the AppsCustomize tab
1024 if (mAppsCustomizeTabHost != null) {
1025 String curTab = savedState.getString("apps_customize_currentTab");
1026 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001027 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001028 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001029 mAppsCustomizeContent.loadAssociatedPages(
1030 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001031 }
1032
Winson Chung5afbf7b2011-07-25 11:53:08 -07001033 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1034 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037
1038 /**
1039 * Finds all the views we need and configure them properly.
1040 */
1041 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001042 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001043
Craig Mautner360310b2012-10-26 15:13:08 -07001044 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001045 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1046 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001047 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001048
1049 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1050 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051
Winson Chung4c98d922011-05-31 16:50:48 -07001052 // Setup the drag layer
1053 mDragLayer.setup(this, dragController);
1054
Winson Chung3d503fb2011-07-13 17:25:49 -07001055 // Setup the hotseat
1056 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1057 if (mHotseat != null) {
1058 mHotseat.setup(this);
1059 }
1060
Winson Chung4c98d922011-05-31 16:50:48 -07001061 // Setup the workspace
1062 mWorkspace.setHapticFeedbackEnabled(false);
1063 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001064 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001065 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001066
Winson Chungf0ea4d32011-06-06 14:27:16 -07001067 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001068 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001069
Winson Chungf0ea4d32011-06-06 14:27:16 -07001070 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001071 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001072 mAppsCustomizeContent = (AppsCustomizePagedView)
1073 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1074 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001075
Winson Chung3d503fb2011-07-13 17:25:49 -07001076 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001077 dragController.setDragScoller(mWorkspace);
1078 dragController.setScrollView(mDragLayer);
1079 dragController.setMoveTarget(mWorkspace);
1080 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001081 if (mSearchDropTargetBar != null) {
1082 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001083 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001084
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001085 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001086 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001087 mWeightWatcher = new WeightWatcher(this);
1088 mWeightWatcher.setAlpha(0.5f);
1089 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001090 new FrameLayout.LayoutParams(
1091 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001092 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001093 Gravity.BOTTOM)
1094 );
Adam Cohen39a06042013-07-19 14:30:12 -07001095
1096 boolean show = shouldShowWeightWatcher();
1097 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001098 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
1100
1101 /**
1102 * Creates a view representing a shortcut.
1103 *
1104 * @param info The data structure describing the shortcut.
1105 *
1106 * @return A View inflated from R.layout.application.
1107 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001108 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001110 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112
1113 /**
1114 * Creates a view representing a shortcut inflated from the specified resource.
1115 *
1116 * @param layoutResId The id of the XML layout used to create the shortcut.
1117 * @param parent The group the shortcut belongs to.
1118 * @param info The data structure describing the shortcut.
1119 *
1120 * @return A View inflated from layoutResId.
1121 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001122 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001123 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1124 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 return favorite;
1127 }
1128
1129 /**
1130 * Add an application shortcut to the workspace.
1131 *
1132 * @param data The intent describing the application.
1133 * @param cellInfo The position on screen where to create the shortcut.
1134 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001135 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001136 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001137 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001138
Adam Cohenfbba09b2011-07-18 21:43:05 -07001139 // First we check if we already know the exact location where we want to add this item.
1140 if (cellX >= 0 && cellY >= 0) {
1141 cellXY[0] = cellX;
1142 cellXY[1] = cellY;
1143 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001144 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001145 return;
1146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001148 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001149
Romain Guy73b979d2009-06-09 12:57:21 -07001150 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001151 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001153 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001154 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001155 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001156 } else {
1157 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 }
1159 }
Romain Guycbb89e42009-06-08 15:52:54 -07001160
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 /**
1162 * Add a shortcut to the workspace.
1163 *
1164 * @param data The intent describing the shortcut.
1165 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001167 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001168 int cellY) {
1169 int[] cellXY = mTmpAddItemCellCoordinates;
1170 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001171 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001172
Michael Jurkad3ef3062010-11-23 16:23:58 -08001173 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001174
Adam Cohen558baaf2011-08-15 15:22:57 -07001175 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001176 if (info == null) {
1177 return;
1178 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001179 final View view = createShortcut(info);
1180
Adam Cohenfbba09b2011-07-18 21:43:05 -07001181 // First we check if we already know the exact location where we want to add this item.
1182 if (cellX >= 0 && cellY >= 0) {
1183 cellXY[0] = cellX;
1184 cellXY[1] = cellY;
1185 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001186
1187 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001188 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001189 true, null,null)) {
1190 return;
1191 }
1192 DragObject dragObject = new DragObject();
1193 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001194 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1195 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001196 return;
1197 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001198 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001199 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001200 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001201 foundCellSpan = (result != null);
1202 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001203 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001204 }
1205
1206 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001207 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001208 return;
1209 }
1210
Adam Cohendcd297f2013-06-18 13:13:40 -07001211 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212
1213 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001214 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001215 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 }
1217 }
1218
Adam Cohen2f093b62012-04-30 18:59:53 -07001219 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1220 int minHeight) {
1221 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001222 // We want to account for the extra amount of padding that we are adding to the widget
1223 // to ensure that it gets the full amount of space that it has requested
1224 int requiredWidth = minWidth + padding.left + padding.right;
1225 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001226 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001227 }
1228
Adam Cohen2f093b62012-04-30 18:59:53 -07001229 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1230 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001231 }
1232
Adam Cohen2f093b62012-04-30 18:59:53 -07001233 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1234 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001235 }
1236
Adam Cohen2f093b62012-04-30 18:59:53 -07001237 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1238 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001239 }
1240
Adam Cohen2f093b62012-04-30 18:59:53 -07001241 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1242 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001243 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001244 }
1245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001247 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001249 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001250 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001252 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001253 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1254 if (appWidgetInfo == null) {
1255 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1256 }
Romain Guycbb89e42009-06-08 15:52:54 -07001257
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001258 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001259 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001260
Adam Cohen2f093b62012-04-30 18:59:53 -07001261 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1262 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001265 // We have saved the position to which the widget was dragged-- this really only matters
1266 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001267 int[] cellXY = mTmpAddItemCellCoordinates;
1268 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001269 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001270 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001271 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1272 cellXY[0] = mPendingAddInfo.cellX;
1273 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001274 spanXY[0] = mPendingAddInfo.spanX;
1275 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001276 foundCellSpan = true;
1277 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001278 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001279 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001280 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1281 spanXY[1], cellXY, finalSpan);
1282 spanXY[0] = finalSpan[0];
1283 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001284 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001285 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001286 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001287 }
1288
Michael Jurka0280c3b2010-09-17 15:00:07 -07001289 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001290 if (appWidgetId != -1) {
1291 // Deleting an app widget ID is a void call but writes to disk before returning
1292 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001293 new Thread("deleteAppWidgetId") {
1294 public void run() {
1295 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1296 }
1297 }.start();
1298 }
Winson Chung93eef082012-03-23 15:59:27 -07001299 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001300 return;
1301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001303 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001304 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1305 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001306 launcherInfo.spanX = spanXY[0];
1307 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001308 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1309 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001312 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313
1314 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001315 if (hostView == null) {
1316 // Perform actual inflation because we're live
1317 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1318 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1319 } else {
1320 // The AppWidgetHostView has already been inflated and instantiated
1321 launcherInfo.hostView = hostView;
1322 }
Romain Guycbb89e42009-06-08 15:52:54 -07001323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001325 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001326 launcherInfo.notifyWidgetSizeChanged(this);
1327
Adam Cohendcd297f2013-06-18 13:13:40 -07001328 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001330
1331 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001333 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
Romain Guycbb89e42009-06-08 15:52:54 -07001335
Adam Cohended9f8d2010-11-03 13:25:16 -07001336 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1337 @Override
1338 public void onReceive(Context context, Intent intent) {
1339 final String action = intent.getAction();
1340 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1341 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001342 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001343 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001344
Winson Chungc100e8e2011-08-09 16:02:43 -07001345 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001346 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001347 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1348 mAppsCustomizeTabHost.reset();
1349 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001350 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001351 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1352 mUserPresent = true;
1353 updateRunning();
1354 }
1355 }
1356 };
1357
1358 @Override
1359 public void onAttachedToWindow() {
1360 super.onAttachedToWindow();
1361
1362 // Listen for broadcasts related to user-presence
1363 final IntentFilter filter = new IntentFilter();
1364 filter.addAction(Intent.ACTION_SCREEN_OFF);
1365 filter.addAction(Intent.ACTION_USER_PRESENT);
1366 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001367 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001368 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001369 mVisible = true;
1370 }
1371
1372 @Override
1373 public void onDetachedFromWindow() {
1374 super.onDetachedFromWindow();
1375 mVisible = false;
1376
Adam Cohend113e0c2010-11-11 10:48:05 -08001377 if (mAttached) {
1378 unregisterReceiver(mReceiver);
1379 mAttached = false;
1380 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001381 updateRunning();
1382 }
1383
1384 public void onWindowVisibilityChanged(int visibility) {
1385 mVisible = visibility == View.VISIBLE;
1386 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001387 // The following code used to be in onResume, but it turns out onResume is called when
1388 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1389 // is a more appropriate event to handle
1390 if (mVisible) {
1391 mAppsCustomizeTabHost.onWindowVisible();
1392 if (!mWorkspaceLoading) {
1393 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001394 // We want to let Launcher draw itself at least once before we force it to build
1395 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001396 // apps is nice and speedy.
1397 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001398 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001399 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001400 if (mStarted) return;
1401 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001402 // We delay the layer building a bit in order to give
1403 // other message processing a time to run. In particular
1404 // this avoids a delay in hiding the IME if it was
1405 // currently shown, because doing that may involve
1406 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001407 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001408 final ViewTreeObserver.OnDrawListener listener = this;
1409 mWorkspace.post(new Runnable() {
1410 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001411 if (mWorkspace != null &&
1412 mWorkspace.getViewTreeObserver() != null) {
1413 mWorkspace.getViewTreeObserver().
1414 removeOnDrawListener(listener);
1415 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001416 }
1417 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001418 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001419 }
1420 });
1421 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001422 // When Launcher comes back to foreground, a different Activity might be responsible for
1423 // the app market intent, so refresh the icon
1424 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001425 clearTypedText();
1426 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001427 }
1428
1429 private void sendAdvanceMessage(long delay) {
1430 mHandler.removeMessages(ADVANCE_MSG);
1431 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1432 mHandler.sendMessageDelayed(msg, delay);
1433 mAutoAdvanceSentTime = System.currentTimeMillis();
1434 }
1435
1436 private void updateRunning() {
1437 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1438 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1439 mAutoAdvanceRunning = autoAdvanceRunning;
1440 if (autoAdvanceRunning) {
1441 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1442 sendAdvanceMessage(delay);
1443 } else {
1444 if (!mWidgetsToAdvance.isEmpty()) {
1445 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1446 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1447 }
1448 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001449 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001450 }
1451 }
1452 }
1453
1454 private final Handler mHandler = new Handler() {
1455 @Override
1456 public void handleMessage(Message msg) {
1457 if (msg.what == ADVANCE_MSG) {
1458 int i = 0;
1459 for (View key: mWidgetsToAdvance.keySet()) {
1460 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1461 final int delay = mAdvanceStagger * i;
1462 if (v instanceof Advanceable) {
1463 postDelayed(new Runnable() {
1464 public void run() {
1465 ((Advanceable) v).advance();
1466 }
1467 }, delay);
1468 }
1469 i++;
1470 }
1471 sendAdvanceMessage(mAdvanceInterval);
1472 }
1473 }
1474 };
1475
1476 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001477 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001478 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1479 if (v instanceof Advanceable) {
1480 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001481 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001482 updateRunning();
1483 }
1484 }
1485
1486 void removeWidgetToAutoAdvance(View hostView) {
1487 if (mWidgetsToAdvance.containsKey(hostView)) {
1488 mWidgetsToAdvance.remove(hostView);
1489 updateRunning();
1490 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001491 }
1492
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001494 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001495 launcherInfo.hostView = null;
1496 }
1497
Winson Chung93eef082012-03-23 15:59:27 -07001498 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1499 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1500 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001501 }
1502
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001503 public LauncherAppWidgetHost getAppWidgetHost() {
1504 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 }
Romain Guycbb89e42009-06-08 15:52:54 -07001506
Winson Chunga9abd0e2010-10-27 17:18:37 -07001507 public LauncherModel getModel() {
1508 return mModel;
1509 }
1510
Bjorn Bringertc459e522013-06-07 19:36:01 +01001511 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001512 getWindow().closeAllPanels();
1513
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001514 // Whatever we were doing is hereby canceled.
1515 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001516 }
1517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 @Override
1519 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001520 long startTime = 0;
1521 if (DEBUG_RESUME_TIME) {
1522 startTime = System.currentTimeMillis();
1523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 super.onNewIntent(intent);
1525
1526 // Close the menu
1527 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001528 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001529 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001530
Jamie Genniscb222e82012-10-23 15:44:26 -07001531 final boolean alreadyOnHome =
1532 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001533 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001534
Jamie Genniscb222e82012-10-23 15:44:26 -07001535 Runnable processIntent = new Runnable() {
1536 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001537 if (mWorkspace == null) {
1538 // Can be cases where mWorkspace is null, this prevents a NPE
1539 return;
1540 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001541 Folder openFolder = mWorkspace.getOpenFolder();
1542 // In all these cases, only animate if we're already on home
1543 mWorkspace.exitWidgetResizeMode();
1544 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1545 openFolder == null) {
1546 mWorkspace.moveToDefaultScreen(true);
1547 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001548
Jamie Genniscb222e82012-10-23 15:44:26 -07001549 closeFolder();
1550 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001551
Jamie Genniscb222e82012-10-23 15:44:26 -07001552 // If we are already on home, then just animate back to the workspace,
1553 // otherwise, just wait until onResume to set the state back to Workspace
1554 if (alreadyOnHome) {
1555 showWorkspace(true);
1556 } else {
1557 mOnResumeState = State.WORKSPACE;
1558 }
1559
1560 final View v = getWindow().peekDecorView();
1561 if (v != null && v.getWindowToken() != null) {
1562 InputMethodManager imm = (InputMethodManager)getSystemService(
1563 INPUT_METHOD_SERVICE);
1564 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1565 }
1566
1567 // Reset AllApps to its initial state
1568 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1569 mAppsCustomizeTabHost.reset();
1570 }
1571 }
1572 };
1573
1574 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1575 // Delay processing of the intent to allow the status bar animation to finish
1576 // first in order to avoid janky animations.
1577 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001578 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001579 // Process the intent immediately.
1580 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001581 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
Michael Jurka447bf842013-05-15 14:52:15 +02001584 if (DEBUG_RESUME_TIME) {
1585 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
1588
1589 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001590 public void onRestoreInstanceState(Bundle state) {
1591 super.onRestoreInstanceState(state);
1592 for (int page: mSynchronouslyBoundPages) {
1593 mWorkspace.restoreInstanceStateForChild(page);
1594 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
1596
1597 @Override
1598 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001599 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001600 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601
Michael Jurka883f55b2010-10-21 15:47:14 -07001602 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001603 // We close any open folder since it will not be re-opened, and we need to make sure
1604 // this state is reflected.
1605 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606
Adam Cohendcd297f2013-06-18 13:13:40 -07001607 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001608 mWaitingForResult) {
1609 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001610 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001611 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1612 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001613 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1614 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1615 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 }
1617
1618 if (mFolderInfo != null && mWaitingForResult) {
1619 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1620 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1621 }
Michael Jurka946ad472010-07-09 18:05:18 -07001622
Winson Chung785d2eb2011-04-14 16:08:02 -07001623 // Save the current AppsCustomize tab
1624 if (mAppsCustomizeTabHost != null) {
1625 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1626 if (currentTabTag != null) {
1627 outState.putString("apps_customize_currentTab", currentTabTag);
1628 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001629 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1630 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001631 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 }
1633
1634 @Override
1635 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001637
Winson Chunge7a03942011-08-05 15:05:12 -07001638 // Remove all pending runnables
1639 mHandler.removeMessages(ADVANCE_MSG);
1640 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001641 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001642
Winson Chungcd2b0142011-06-08 16:02:26 -07001643 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001644 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001645 mModel.stopLoader();
1646 app.setLauncher(null);
1647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001649 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001651 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001653 mAppWidgetHost = null;
1654
1655 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656
1657 TextKeyListener.getInstance().release();
1658
Winson Chung81b52252012-08-27 15:34:29 -07001659 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1660 // to prevent leaking Launcher activities on orientation change.
1661 if (mModel != null) {
1662 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1663 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001664
1665 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001666 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001667
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001668 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001669 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1670 mWorkspace.removeAllViews();
1671 mWorkspace = null;
1672 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001673
Michael Jurka2ecf9952012-06-18 12:52:28 -07001674 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 }
1676
Adam Cohena9cf38f2011-05-02 15:36:58 -07001677 public DragController getDragController() {
1678 return mDragController;
1679 }
1680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 @Override
1682 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001683 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 super.startActivityForResult(intent, requestCode);
1685 }
1686
Winson Chung70d72102011-08-12 11:24:35 -07001687 /**
1688 * Indicates that we want global search for this activity by setting the globalSearch
1689 * argument for {@link #startSearch} to true.
1690 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001692 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001694
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001695 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001696
Karl Rosaen138a0412009-04-23 19:00:21 -07001697 if (initialQuery == null) {
1698 // Use any text typed in the launcher as the initial query
1699 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001700 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 if (appSearchData == null) {
1702 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001703 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
Winson Chungadf0c182012-08-23 14:59:07 -07001705 Rect sourceBounds = new Rect();
1706 if (mSearchDropTargetBar != null) {
1707 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1708 }
Romain Guycbb89e42009-06-08 15:52:54 -07001709
Bjorn Bringertc459e522013-06-07 19:36:01 +01001710 startSearch(initialQuery, selectInitialQuery,
1711 appSearchData, sourceBounds);
1712 }
1713
1714 public void startSearch(String initialQuery,
1715 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001716 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001717 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001718 }
1719
1720 /**
1721 * Starts the global search activity. This code is a copied from SearchManager
1722 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001723 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001724 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001725 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001726 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1727 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1728 if (globalSearchActivity == null) {
1729 Log.w(TAG, "No global search activity found.");
1730 return;
1731 }
1732 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1733 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1734 intent.setComponent(globalSearchActivity);
1735 // Make sure that we have a Bundle to put source in
1736 if (appSearchData == null) {
1737 appSearchData = new Bundle();
1738 } else {
1739 appSearchData = new Bundle(appSearchData);
1740 }
1741 // Set source to package name of app that starts global search, if not set already.
1742 if (!appSearchData.containsKey("source")) {
1743 appSearchData.putString("source", getPackageName());
1744 }
1745 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1746 if (!TextUtils.isEmpty(initialQuery)) {
1747 intent.putExtra(SearchManager.QUERY, initialQuery);
1748 }
1749 if (selectInitialQuery) {
1750 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1751 }
1752 intent.setSourceBounds(sourceBounds);
1753 try {
1754 startActivity(intent);
1755 } catch (ActivityNotFoundException ex) {
1756 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 }
1759
Winson Chung70d72102011-08-12 11:24:35 -07001760 @Override
1761 public boolean onCreateOptionsMenu(Menu menu) {
1762 if (isWorkspaceLocked()) {
1763 return false;
1764 }
1765
1766 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001767
1768 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1769 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1770 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001771 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1772 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1773 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001774 String helpUrl = getString(R.string.help_url);
1775 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001776 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1777 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1778
Winson Chung70d72102011-08-12 11:24:35 -07001779 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1780 .setIcon(android.R.drawable.ic_menu_gallery)
1781 .setAlphabeticShortcut('W');
1782 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1783 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001784 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001785 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001786 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1787 .setIcon(android.R.drawable.ic_menu_preferences)
1788 .setIntent(settings)
1789 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001790 if (!helpUrl.isEmpty()) {
1791 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1792 .setIcon(android.R.drawable.ic_menu_help)
1793 .setIntent(help)
1794 .setAlphabeticShortcut('H');
1795 }
Winson Chung70d72102011-08-12 11:24:35 -07001796 return true;
1797 }
1798
1799 @Override
1800 public boolean onPrepareOptionsMenu(Menu menu) {
1801 super.onPrepareOptionsMenu(menu);
1802
1803 if (mAppsCustomizeTabHost.isTransitioning()) {
1804 return false;
1805 }
1806 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1807 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1808
1809 return true;
1810 }
1811
1812 @Override
1813 public boolean onOptionsItemSelected(MenuItem item) {
1814 switch (item.getItemId()) {
1815 case MENU_WALLPAPER_SETTINGS:
1816 startWallpaper();
1817 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001818 }
1819
1820 return super.onOptionsItemSelected(item);
1821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001823 @Override
1824 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001825 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001826 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001827 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001828 }
1829
Joe Onorato9c1289c2009-08-17 11:03:03 -04001830 public boolean isWorkspaceLocked() {
1831 return mWorkspaceLoading || mWaitingForResult;
1832 }
1833
Michael Jurka0280c3b2010-09-17 15:00:07 -07001834 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001835 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001836 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001837 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1838 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001839 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001840 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001841 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001842
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001843 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1844 AppWidgetProviderInfo appWidgetInfo) {
1845 if (appWidgetInfo.configure != null) {
1846 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001847
Bjorn Bringert7984c942009-12-09 15:38:25 +00001848 // Launch over to configure widget, if needed
1849 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001850 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001851 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001852 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001854 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001855 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001856 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001857 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001858 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
1860 }
Romain Guycbb89e42009-06-08 15:52:54 -07001861
Adam Cohenfbba09b2011-07-18 21:43:05 -07001862 /**
1863 * Process a shortcut drop.
1864 *
1865 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001866 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001867 * @param cell The cell it should be added to, optional
1868 * @param position The location on the screen where it was dropped, optional
1869 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001870 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001871 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001872 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001873 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001874 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001875 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001876
1877 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001878 mPendingAddInfo.cellX = cell[0];
1879 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001880 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001881
1882 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1883 createShortcutIntent.setComponent(componentName);
1884 processShortcut(createShortcutIntent);
1885 }
1886
Adam Cohenfbba09b2011-07-18 21:43:05 -07001887 /**
1888 * Process a widget drop.
1889 *
1890 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001891 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001892 * @param cell The cell it should be added to, optional
1893 * @param position The location on the screen where it was dropped, optional
1894 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001895 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001896 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001897 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001898 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001899 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001900 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001901 mPendingAddInfo.minSpanX = info.minSpanX;
1902 mPendingAddInfo.minSpanY = info.minSpanY;
1903
Adam Cohenfbba09b2011-07-18 21:43:05 -07001904 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001905 mPendingAddInfo.cellX = cell[0];
1906 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001907 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001908 if (span != null) {
1909 mPendingAddInfo.spanX = span[0];
1910 mPendingAddInfo.spanY = span[1];
1911 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001912
Adam Cohened66b2b2012-01-23 17:28:51 -08001913 AppWidgetHostView hostView = info.boundWidget;
1914 int appWidgetId;
1915 if (hostView != null) {
1916 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001917 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001918 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001919 // In this case, we either need to start an activity to get permission to bind
1920 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001921 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001922 Bundle options = info.bindOptions;
1923
1924 boolean success = false;
1925 if (options != null) {
1926 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1927 info.componentName, options);
1928 } else {
1929 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1930 info.componentName);
1931 }
1932 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001933 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001934 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001935 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001936 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1937 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1938 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001939 // TODO: we need to make sure that this accounts for the options bundle.
1940 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001941 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1942 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001943 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001944 }
1945
Joe Onoratodeb98af2010-02-19 14:59:39 -08001946 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001947 // Handle case where user selected "Applications"
1948 String applicationName = getResources().getString(R.string.group_applications);
1949 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001950
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001951 if (applicationName != null && applicationName.equals(shortcutName)) {
1952 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1953 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001954
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001955 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1956 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001957 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001958 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001959 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001960 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
1963
Winson Chung24ab2f12010-09-16 14:10:47 -07001964 void processWallpaper(Intent intent) {
1965 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1966 }
1967
Adam Cohendcd297f2013-06-18 13:13:40 -07001968 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001969 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001970 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 folderInfo.title = getText(R.string.folder_name);
1972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001974 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001975 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001976 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977
1978 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001979 FolderIcon newFolder =
1980 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001981 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001982 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001983 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
Romain Guycbb89e42009-06-08 15:52:54 -07001985
Joe Onorato9c1289c2009-08-17 11:03:03 -04001986 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001987 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001988 }
1989
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001991 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001993 Intent chooser = Intent.createChooser(pickWallpaper,
1994 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001995 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1996 // Removed in Eclair MR1
1997// WallpaperManager wm = (WallpaperManager)
1998// getSystemService(Context.WALLPAPER_SERVICE);
1999// WallpaperInfo wi = wm.getWallpaperInfo();
2000// if (wi != null && wi.getSettingsActivity() != null) {
2001// LabeledIntent li = new LabeledIntent(getPackageName(),
2002// R.string.configure_wallpaper, 0);
2003// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
2004// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
2005// }
Mike Clerona0618e42009-10-22 13:55:21 -07002006 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002009 /**
2010 * Registers various content observers. The current implementation registers
2011 * only a favorites observer to keep track of the favorites applications.
2012 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002013 private void registerContentObservers() {
2014 ContentResolver resolver = getContentResolver();
2015 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2016 true, mWidgetObserver);
2017 }
2018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 @Override
2020 public boolean dispatchKeyEvent(KeyEvent event) {
2021 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2022 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002024 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002025 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002026 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002027 dumpState();
2028 return true;
2029 }
2030 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002031 }
2032 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2033 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002034 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 return true;
2036 }
2037 }
2038
2039 return super.dispatchKeyEvent(event);
2040 }
2041
Joe Onorato88ec0992009-11-19 13:16:06 -08002042 @Override
2043 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002044 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002045 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002046 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002047 Folder openFolder = mWorkspace.getOpenFolder();
2048 if (openFolder.isEditingName()) {
2049 openFolder.dismissEditingName();
2050 } else {
2051 closeFolder();
2052 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002053 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002054 mWorkspace.exitWidgetResizeMode();
2055
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002056 // Back button is a no-op here, but give at least some feedback for the button press
2057 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002058 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002059 }
2060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002062 * Re-listen when widgets are reset.
2063 */
2064 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002065 if (mAppWidgetHost != null) {
2066 mAppWidgetHost.startListening();
2067 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002068 }
2069
2070 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 * Launches the intent referred by the clicked shortcut.
2072 *
2073 * @param v The view representing the clicked shortcut.
2074 */
2075 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002076 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2077 // view has detached (it's possible for this to happen if the view is removed mid touch).
2078 if (v.getWindowToken() == null) {
2079 return;
2080 }
2081
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002082 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002083 return;
2084 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002087 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002089 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002090
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002091 // Check for special shortcuts
2092 if (intent.getComponent() != null) {
2093 final String shortcutClass = intent.getComponent().getClassName();
2094
2095 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2096 showAllApps(true);
2097 return;
2098 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2099 MemoryDumpActivity.startDump(this);
2100 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002101 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2102 toggleShowWeightWatcher();
2103 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002104 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002105 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002106
2107 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002108 int[] pos = new int[2];
2109 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002110 intent.setSourceBounds(new Rect(pos[0], pos[1],
2111 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002112
2113 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002114
2115 if (success && v instanceof BubbleTextView) {
2116 mWaitingForResume = (BubbleTextView) v;
2117 mWaitingForResume.setStayPressed(true);
2118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002120 if (v instanceof FolderIcon) {
2121 FolderIcon fi = (FolderIcon) v;
2122 handleFolderClick(fi);
2123 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002124 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002125 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002126 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002127 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002128 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 }
2131 }
2132
Michael Jurka0e260592010-06-30 17:07:39 -07002133 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002134 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002135 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002136 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2137 showWorkspace(true);
2138 }
Michael Jurka0e260592010-06-30 17:07:39 -07002139 return false;
2140 }
2141
Michael Jurkaaf442092010-06-10 17:01:57 -07002142 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002143 * Event handler for the search button
2144 *
2145 * @param v The view that was clicked.
2146 */
2147 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002148 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2149
Amith Yamasania135ba82011-08-09 17:42:01 -07002150 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002151 }
2152
2153 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002154 * Event handler for the voice button
2155 *
2156 * @param v The view that was clicked.
2157 */
2158 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002159 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002160
Bjorn Bringertc459e522013-06-07 19:36:01 +01002161 startVoice();
2162 }
2163
2164 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002165 try {
2166 final SearchManager searchManager =
2167 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2168 ComponentName activityName = searchManager.getGlobalSearchActivity();
2169 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002170 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002171 if (activityName != null) {
2172 intent.setPackage(activityName.getPackageName());
2173 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002174 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002175 } catch (ActivityNotFoundException e) {
2176 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002177 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002178 startActivitySafely(null, intent, "onClickVoiceButton");
2179 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002180 }
2181
2182 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002183 * Event handler for the "grid" button that appears on the home screen, which
2184 * enters all apps mode.
2185 *
2186 * @param v The view that was clicked.
2187 */
2188 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002189 showAllApps(true);
2190 }
2191
2192 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002193 // Provide the same haptic feedback that the system offers for virtual keys.
2194 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002195 }
2196
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002197 public void onClickAppMarketButton(View v) {
2198 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002199 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002200 } else {
2201 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002202 }
2203 }
2204
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002205 void startApplicationDetailsActivity(ComponentName componentName) {
2206 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002207 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2208 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002209 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002210 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002211 }
2212
Michael Jurka1e2f4652013-07-08 18:03:46 -07002213 // returns true if the activity was started
2214 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2215 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002216 // System applications cannot be installed. For now, show a toast explaining that.
2217 // We may give them the option of disabling apps this way.
2218 int messageId = R.string.uninstall_system_app_text;
2219 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002220 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002221 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002222 String packageName = componentName.getPackageName();
2223 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002224 Intent intent = new Intent(
2225 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002226 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2227 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002228 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002229 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002230 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002231 }
2232
Michael Jurka86a720e2012-05-09 11:23:23 -07002233 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002237 // Only launch using the new animation if the shortcut has not opted out (this is a
2238 // private contract between launcher and may be ignored in the future).
2239 boolean useLaunchAnimation = (v != null) &&
2240 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2241 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002242 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2243 v.getMeasuredWidth(), v.getMeasuredHeight());
2244
2245 startActivity(intent, opts.toBundle());
2246 } else {
2247 startActivity(intent);
2248 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002249 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 } catch (SecurityException e) {
2251 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002252 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002254 "or use the exported attribute for this activity. "
2255 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002257 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002259
Michael Jurka86a720e2012-05-09 11:23:23 -07002260 boolean startActivitySafely(View v, Intent intent, Object tag) {
2261 boolean success = false;
2262 try {
2263 success = startActivity(v, intent, tag);
2264 } catch (ActivityNotFoundException e) {
2265 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2266 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2267 }
2268 return success;
2269 }
2270
Romain Guy8e633c52010-03-04 12:51:36 -08002271 void startActivityForResultSafely(Intent intent, int requestCode) {
2272 try {
2273 startActivityForResult(intent, requestCode);
2274 } catch (ActivityNotFoundException e) {
2275 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2276 } catch (SecurityException e) {
2277 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2278 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2279 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2280 "or use the exported attribute for this activity.", e);
2281 }
2282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283
Adam Cohena9cf38f2011-05-02 15:36:58 -07002284 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002285 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002286 Folder openFolder = mWorkspace.getFolderForTag(info);
2287
2288 // If the folder info reports that the associated folder is open, then verify that
2289 // it is actually opened. There have been a few instances where this gets out of sync.
2290 if (info.opened && openFolder == null) {
2291 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002292 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002293 info.opened = false;
2294 }
2295
Adam Cohenfb91f302012-06-11 15:45:18 -07002296 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 // Close any open folder
2298 closeFolder();
2299 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002300 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 } else {
2302 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 int folderScreen;
2304 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002305 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002306 // .. and close it
2307 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002308 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 // Close any folder open on the current screen
2310 closeFolder();
2311 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002312 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 }
2314 }
2315 }
2316 }
2317
Adam Cohen268c4752012-06-06 17:47:33 -07002318 /**
2319 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2320 * in the DragLayer in the exact absolute location of the original FolderIcon.
2321 */
2322 private void copyFolderIconToImage(FolderIcon fi) {
2323 final int width = fi.getMeasuredWidth();
2324 final int height = fi.getMeasuredHeight();
2325
2326 // Lazy load ImageView, Bitmap and Canvas
2327 if (mFolderIconImageView == null) {
2328 mFolderIconImageView = new ImageView(this);
2329 }
2330 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2331 mFolderIconBitmap.getHeight() != height) {
2332 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2333 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2334 }
2335
2336 DragLayer.LayoutParams lp;
2337 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2338 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2339 } else {
2340 lp = new DragLayer.LayoutParams(width, height);
2341 }
2342
Adam Cohen307fe232012-08-16 17:55:58 -07002343 // The layout from which the folder is being opened may be scaled, adjust the starting
2344 // view size by this scale factor.
2345 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002346 lp.customPosition = true;
2347 lp.x = mRectForFolderAnimation.left;
2348 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002349 lp.width = (int) (scale * width);
2350 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002351
2352 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2353 fi.draw(mFolderIconCanvas);
2354 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002355 if (fi.getFolder() != null) {
2356 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2357 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002358 }
Adam Cohen268c4752012-06-06 17:47:33 -07002359 // Just in case this image view is still in the drag layer from a previous animation,
2360 // we remove it and re-add it.
2361 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2362 mDragLayer.removeView(mFolderIconImageView);
2363 }
2364 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002365 if (fi.getFolder() != null) {
2366 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002367 }
Adam Cohen268c4752012-06-06 17:47:33 -07002368 }
2369
Adam Cohen2801caf2011-05-13 20:57:39 -07002370 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002371 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002372 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2373 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2374 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2375
Adam Cohenc51934b2011-07-26 21:07:43 -07002376 FolderInfo info = (FolderInfo) fi.getTag();
2377 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2378 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002379 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2380 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002381 }
2382
Adam Cohen268c4752012-06-06 17:47:33 -07002383 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2384 copyFolderIconToImage(fi);
2385 fi.setVisibility(View.INVISIBLE);
2386
Michael Jurka2ecf9952012-06-18 12:52:28 -07002387 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002388 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002389 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2390 oa.start();
2391 }
2392
Adam Cohen268c4752012-06-06 17:47:33 -07002393 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002394 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002395 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2396 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2397 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2398
Adam Cohen268c4752012-06-06 17:47:33 -07002399 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002400
Adam Cohen268c4752012-06-06 17:47:33 -07002401 // We remove and re-draw the FolderIcon in-case it has changed
2402 mDragLayer.removeView(mFolderIconImageView);
2403 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002404 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002405 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002406 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002407 oa.addListener(new AnimatorListenerAdapter() {
2408 @Override
2409 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002410 if (cl != null) {
2411 cl.clearFolderLeaveBehind();
2412 // Remove the ImageView copy of the FolderIcon and make the original visible.
2413 mDragLayer.removeView(mFolderIconImageView);
2414 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002415 }
2416 }
2417 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002418 oa.start();
2419 }
2420
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002422 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 * is animated relative to the specified View. If the View is null, no animation
2424 * is played.
2425 *
2426 * @param folderInfo The FolderInfo describing the folder to open.
2427 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002428 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002429 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002430 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431
Adam Cohena9cf38f2011-05-02 15:36:58 -07002432 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433
Adam Cohen4554ee12011-08-03 16:13:21 -07002434 // Just verify that the folder hasn't already been added to the DragLayer.
2435 // There was a one-off crash where the folder had a parent already.
2436 if (folder.getParent() == null) {
2437 mDragLayer.addView(folder);
2438 mDragController.addDropTarget((DropTarget) folder);
2439 } else {
2440 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2441 folder.getParent() + ").");
2442 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002443 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002444 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002445
2446 // Notify the accessibility manager that this folder "window" has appeared and occluded
2447 // the workspace items
2448 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2449 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002450 }
2451
2452 public void closeFolder() {
2453 Folder folder = mWorkspace.getOpenFolder();
2454 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002455 if (folder.isEditingName()) {
2456 folder.dismissEditingName();
2457 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002458 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002459
2460 // Dismiss the folder cling
2461 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002462 }
2463 }
2464
2465 void closeFolder(Folder folder) {
2466 folder.getInfo().opened = false;
2467
2468 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2469 if (parent != null) {
2470 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2471 shrinkAndFadeInFolderIcon(fi);
2472 }
2473 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002474
2475 // Notify the accessibility manager that this folder "window" has disappeard and no
2476 // longer occludeds the workspace items
2477 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 }
2479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002481 if (!isDraggingEnabled()) return false;
2482 if (isWorkspaceLocked()) return false;
2483 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484
2485 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002486 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 }
2488
Michael Jurka0280c3b2010-09-17 15:00:07 -07002489 resetAddInfo();
2490 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002492 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 return true;
2494 }
2495
Winson Chung3d503fb2011-07-13 17:25:49 -07002496 // The hotseat touch handling does not go through Workspace, and we always allow long press
2497 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002498 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002499 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2500 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002501 if (itemUnderLongClick == null) {
2502 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002503 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2504 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002505 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002506 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2507 mWorkspace.startReordering();
2508 } else {
2509 startWallpaper();
2510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002512 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002514 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 }
2516 }
2517 }
2518 return true;
2519 }
2520
Winson Chung3d503fb2011-07-13 17:25:49 -07002521 boolean isHotseatLayout(View layout) {
2522 return mHotseat != null && layout != null &&
2523 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2524 }
2525 Hotseat getHotseat() {
2526 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002527 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002528 SearchDropTargetBar getSearchBar() {
2529 return mSearchDropTargetBar;
2530 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002531
Winson Chung3d503fb2011-07-13 17:25:49 -07002532 /**
2533 * Returns the CellLayout of the specified container at the specified screen.
2534 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002535 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002536 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2537 if (mHotseat != null) {
2538 return mHotseat.getLayout();
2539 } else {
2540 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002541 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002542 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002543 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002544 }
2545 }
2546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 Workspace getWorkspace() {
2548 return mWorkspace;
2549 }
2550
Daniel Sandler843e8602010-06-07 14:59:01 -04002551 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002552 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002553 }
2554
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002555 /**
2556 * Helper method for the cameraZoomIn/cameraZoomOut animations
2557 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002558 * @param scaleFactor The scale factor used for the zoom
2559 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002560 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002562 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002564
Winson Chung18f41f82012-05-09 13:28:10 -07002565 void disableWallpaperIfInAllApps() {
2566 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002567 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002568 if (mAppsCustomizeTabHost != null &&
2569 !mAppsCustomizeTabHost.isTransitioning()) {
2570 updateWallpaperVisibility(false);
2571 }
2572 }
2573 }
2574
Craig Mautner360310b2012-10-26 15:13:08 -07002575 private void setWorkspaceBackground(boolean workspace) {
2576 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002577 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002578 }
2579
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002580 void updateWallpaperVisibility(boolean visible) {
2581 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2582 int curflags = getWindow().getAttributes().flags
2583 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2584 if (wpflags != curflags) {
2585 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2586 }
Craig Mautner360310b2012-10-26 15:13:08 -07002587 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002588 }
2589
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002590 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2591 if (v instanceof LauncherTransitionable) {
2592 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2593 }
2594 }
2595
Michael Jurkabed61d22012-02-14 22:51:29 -08002596 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2597 if (v instanceof LauncherTransitionable) {
2598 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2599 }
Winson Chung70442722012-02-10 15:43:22 -08002600
2601 // Update the workspace transition step as well
2602 dispatchOnLauncherTransitionStep(v, 0f);
2603 }
2604
2605 private void dispatchOnLauncherTransitionStep(View v, float t) {
2606 if (v instanceof LauncherTransitionable) {
2607 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2608 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002609 }
2610
2611 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2612 if (v instanceof LauncherTransitionable) {
2613 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2614 }
Winson Chung70442722012-02-10 15:43:22 -08002615
2616 // Update the workspace transition step as well
2617 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002618 }
2619
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002620 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002621 * Things to test when changing the following seven functions.
2622 * - Home from workspace
2623 * - from center screen
2624 * - from other screens
2625 * - Home from all apps
2626 * - from center screen
2627 * - from other screens
2628 * - Back from all apps
2629 * - from center screen
2630 * - from other screens
2631 * - Launch app from workspace and quit
2632 * - with back
2633 * - with home
2634 * - Launch app from all apps and quit
2635 * - with back
2636 * - with home
2637 * - Go to a screen that's not the default, then all
2638 * apps, and launch and app, and go back
2639 * - with back
2640 * -with home
2641 * - On workspace, long press power and go back
2642 * - with back
2643 * - with home
2644 * - On all apps, long press power and go back
2645 * - with back
2646 * - with home
2647 * - On workspace, power off
2648 * - On all apps, power off
2649 * - Launch an app and turn off the screen while in that app
2650 * - Go back with home key
2651 * - Go back with back key TODO: make this not go to workspace
2652 * - From all apps
2653 * - From workspace
2654 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2655 * - From all apps
2656 * - From the center workspace
2657 * - From another workspace
2658 */
2659
2660 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002661 * Zoom the camera out from the workspace to reveal 'toView'.
2662 * Assumes that the view to show is anchored at either the very top or very bottom
2663 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002664 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002665 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002666 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002667 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002668 mStateAnimation.cancel();
2669 mStateAnimation = null;
2670 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002671 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002672
Winson Chungf0ea4d32011-06-06 14:27:16 -07002673 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2674 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2675 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002676 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002677 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002678 final int startDelay =
2679 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002680
Michael Jurkab3e22d92011-10-31 15:58:33 -07002681 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002682
Michael Jurkad74c9842011-07-10 12:44:21 -07002683 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002684 Animator workspaceAnim =
2685 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002686
2687 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002688 toView.setScaleX(scale);
2689 toView.setScaleY(scale);
2690 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2691 scaleAnim.
2692 scaleX(1f).scaleY(1f).
2693 setDuration(duration).
2694 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002695
Winson Chungf0ea4d32011-06-06 14:27:16 -07002696 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002697 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002698 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002699 .ofFloat(toView, "alpha", 0f, 1f)
2700 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002701 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002702 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2703 @Override
2704 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002705 if (animation == null) {
2706 throw new RuntimeException("animation is null");
2707 }
Winson Chung70442722012-02-10 15:43:22 -08002708 float t = (Float) animation.getAnimatedValue();
2709 dispatchOnLauncherTransitionStep(fromView, t);
2710 dispatchOnLauncherTransitionStep(toView, t);
2711 }
2712 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002713
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002714 // toView should appear right at the end of the workspace shrink
2715 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002716 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002717 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002718 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002719
2720 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002721 boolean animationCancelled = false;
2722
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002723 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002724 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002725 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002726 // Prepare the position
2727 toView.setTranslationX(0.0f);
2728 toView.setTranslationY(0.0f);
2729 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002730 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002731 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002732 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002733 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002734 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2735 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002736
Daniel Sandlere4f98912013-06-25 15:13:26 -04002737 if (mWorkspace != null
2738 && !springLoaded
2739 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002740 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002741 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002742 if (!animationCancelled) {
2743 updateWallpaperVisibility(false);
2744 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002745
2746 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002747 if (mSearchDropTargetBar != null) {
2748 mSearchDropTargetBar.hideSearchBar(false);
2749 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002750 }
2751
Adam Cohencff6af82011-09-13 14:51:53 -07002752 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002753 public void onAnimationCancel(Animator animation) {
2754 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002755 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002756 });
2757
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002758 if (workspaceAnim != null) {
2759 mStateAnimation.play(workspaceAnim);
2760 }
Michael Jurka1899a362011-11-03 13:50:45 -07002761
2762 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002763
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002764 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2765 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002766
2767 // If any of the objects being animated haven't been measured/laid out
2768 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002769 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002770 (mWorkspace.getMeasuredWidth() == 0) ||
2771 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002772 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002773 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002774
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002775 final AnimatorSet stateAnimation = mStateAnimation;
2776 final Runnable startAnimRunnable = new Runnable() {
2777 public void run() {
2778 // Check that mStateAnimation hasn't changed while
2779 // we waited for a layout/draw pass
2780 if (mStateAnimation != stateAnimation)
2781 return;
2782 setPivotsForZoom(toView, scale);
2783 dispatchOnLauncherTransitionStart(fromView, animated, false);
2784 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002785 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002786 }
2787 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002788 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002789 final ViewTreeObserver observer = toView.getViewTreeObserver();
2790 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2791 public void onGlobalLayout() {
2792 startAnimRunnable.run();
2793 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2794 }
2795 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002796 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002797 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002798 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002799 } else {
2800 toView.setTranslationX(0.0f);
2801 toView.setTranslationY(0.0f);
2802 toView.setScaleX(1.0f);
2803 toView.setScaleY(1.0f);
2804 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002805 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002806
Daniel Sandlere4f98912013-06-25 15:13:26 -04002807 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002808 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002809
2810 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002811 if (mSearchDropTargetBar != null) {
2812 mSearchDropTargetBar.hideSearchBar(false);
2813 }
Michael Jurkaabded662011-03-04 12:06:57 -08002814 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002815 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002816 dispatchOnLauncherTransitionStart(fromView, animated, false);
2817 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002818 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002819 dispatchOnLauncherTransitionStart(toView, animated, false);
2820 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002821 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002822 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002823 }
2824
2825 /**
2826 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002827 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002828 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002829 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002830 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2831 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002832
Michael Jurkab3e22d92011-10-31 15:58:33 -07002833 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002834 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002835 mStateAnimation.cancel();
2836 mStateAnimation = null;
2837 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002838 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002839
Winson Chungf0ea4d32011-06-06 14:27:16 -07002840 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002841 final int fadeOutDuration =
2842 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002843 final float scaleFactor = (float)
2844 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2845 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002846 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002847 Animator workspaceAnim = null;
2848
2849 if (toState == State.WORKSPACE) {
2850 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2851 workspaceAnim = mWorkspace.getChangeStateAnimation(
2852 Workspace.State.NORMAL, animated, stagger);
2853 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2854 workspaceAnim = mWorkspace.getChangeStateAnimation(
2855 Workspace.State.SPRING_LOADED, animated);
2856 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002857
Michael Jurkab3e22d92011-10-31 15:58:33 -07002858 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002859 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002860 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002861 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002862 final LauncherViewPropertyAnimator scaleAnim =
2863 new LauncherViewPropertyAnimator(fromView);
2864 scaleAnim.
2865 scaleX(scaleFactor).scaleY(scaleFactor).
2866 setDuration(duration).
2867 setInterpolator(new Workspace.ZoomInInterpolator());
2868
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002869 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002870 .ofFloat(fromView, "alpha", 1f, 0f)
2871 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002872 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002873 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2874 @Override
2875 public void onAnimationUpdate(ValueAnimator animation) {
2876 float t = 1f - (Float) animation.getAnimatedValue();
2877 dispatchOnLauncherTransitionStep(fromView, t);
2878 dispatchOnLauncherTransitionStep(toView, t);
2879 }
2880 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002881
Michael Jurka2ecf9952012-06-18 12:52:28 -07002882 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002883
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002884 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2885 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002886 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002887
2888 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002889 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002890 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002891 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002892 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002893 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2894 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002895 if (onCompleteRunnable != null) {
2896 onCompleteRunnable.run();
2897 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002898 mAppsCustomizeContent.updateCurrentPageScroll();
2899 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002900 }
2901 });
2902
Winson Chungf0ea4d32011-06-06 14:27:16 -07002903 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002904 if (workspaceAnim != null) {
2905 mStateAnimation.play(workspaceAnim);
2906 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002907 dispatchOnLauncherTransitionStart(fromView, animated, true);
2908 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002909 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002910 } else {
2911 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002912 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002913 dispatchOnLauncherTransitionStart(fromView, animated, true);
2914 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002915 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002916 dispatchOnLauncherTransitionStart(toView, animated, true);
2917 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002918 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002919 }
2920
Michael Jurkae326f182011-11-21 14:05:46 -08002921 @Override
2922 public void onTrimMemory(int level) {
2923 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002924 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002925 mAppsCustomizeTabHost.onTrimMemory();
2926 }
2927 }
2928
Winson Chung18f41f82012-05-09 13:28:10 -07002929 @Override
2930 public void onWindowFocusChanged(boolean hasFocus) {
2931 if (!hasFocus) {
2932 // When another window occludes launcher (like the notification shade, or recents),
2933 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2934 updateWallpaperVisibility(true);
2935 } else {
2936 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002937 mWorkspace.postDelayed(new Runnable() {
2938 @Override
2939 public void run() {
2940 disableWallpaperIfInAllApps();
2941 }
2942 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002943 }
2944 }
2945
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002946 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002947 showWorkspace(animated, null);
2948 }
2949
2950 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002951 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002952 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002953 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002954 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002955
Winson Chungc7d2b602012-05-16 17:02:20 -07002956 // Show the search bar (only animate if we were showing the drop target bar in spring
2957 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002958 if (mSearchDropTargetBar != null) {
2959 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2960 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002961
Michael Jurkab737ee62011-11-15 15:57:22 -08002962 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002963 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002964
2965 // Set focus to the AppsCustomize button
2966 if (mAllAppsButton != null) {
2967 mAllAppsButton.requestFocus();
2968 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002969 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002970
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002971 // Change the state *after* we've called all the transition code
2972 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002973
Winson Chung5fb63472011-02-02 17:03:37 -08002974 // Resume the auto-advance of widgets
2975 mUserPresent = true;
2976 updateRunning();
2977
alanv1d4fde62012-10-17 13:15:47 -07002978 // Send an accessibility event to announce the context change
2979 getWindow().getDecorView()
2980 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002981
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002982 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002983 }
2984
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002985 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002986 }
2987
Michael Jurkab3e22d92011-10-31 15:58:33 -07002988 void showAllApps(boolean animated) {
2989 if (mState != State.WORKSPACE) return;
2990
2991 showAppsCustomizeHelper(animated, false);
2992 mAppsCustomizeTabHost.requestFocus();
2993
Michael Jurkab3e22d92011-10-31 15:58:33 -07002994 // Change the state *after* we've called all the transition code
2995 mState = State.APPS_CUSTOMIZE;
2996
2997 // Pause the auto-advance of widgets until we are out of AllApps
2998 mUserPresent = false;
2999 updateRunning();
3000 closeFolder();
3001
3002 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003003 getWindow().getDecorView()
3004 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003005 }
3006
Adam Cohen7777d962011-08-18 18:58:38 -07003007 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003008 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003009 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08003010 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07003011 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003012 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003013 }
Adam Cohen7777d962011-08-18 18:58:38 -07003014
Adam Cohened66b2b2012-01-23 17:28:51 -08003015 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3016 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003017 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3018
Winson Chunge7a03942011-08-05 15:05:12 -07003019 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003020 @Override
3021 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003022 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003023 // Before we show workspace, hide all apps again because
3024 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3025 // clean up our state transition functions
3026 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003027 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003028 } else {
3029 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003030 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003031 }
3032 }, (extendedDelay ?
3033 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3034 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3035 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003036
Michael Jurkad3ef3062010-11-23 16:23:58 -08003037 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003038 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003039 final boolean animated = true;
3040 final boolean springLoaded = true;
3041 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003042 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003043 }
3044 // Otherwise, we are not in spring loaded mode, so don't do anything.
3045 }
3046
Michael Jurkab737ee62011-11-15 15:57:22 -08003047 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003048 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003049 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003050 }
3051 }
3052
3053 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003054 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003055 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003056 if (mDividerAnimator != null) {
3057 mDividerAnimator.cancel();
3058 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003059 mDividerAnimator = null;
3060 }
3061 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003062 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003063 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003064 int duration = 0;
3065 if (mSearchDropTargetBar != null) {
3066 duration = mSearchDropTargetBar.getTransitionInDuration();
3067 }
3068 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003069 mDividerAnimator.start();
3070 }
3071 }
3072 }
3073
Michael Jurkab3e22d92011-10-31 15:58:33 -07003074 void lockAllApps() {
3075 // TODO
3076 }
3077
3078 void unlockAllApps() {
3079 // TODO
3080 }
3081
Winson Chungf0ea4d32011-06-06 14:27:16 -07003082 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003083 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003084 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003085 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003086 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003087 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003088 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003089 int duration = 0;
3090 if (mSearchDropTargetBar != null) {
3091 duration = mSearchDropTargetBar.getTransitionInDuration();
3092 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003093 mHotseat.animate().alpha(1f).setDuration(duration);
3094 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003095 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003096 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003097 }
3098 }
3099 }
3100
3101 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003102 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003103 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003104 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003105 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003106 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003107 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003108 int duration = 0;
3109 if (mSearchDropTargetBar != null) {
3110 duration = mSearchDropTargetBar.getTransitionOutDuration();
3111 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003112 mHotseat.animate().alpha(0f).setDuration(duration);
3113 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003114 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003115 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003116 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003117 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003118 }
3119
Patrick Dubroy5f445422011-02-18 14:35:21 -08003120 /**
3121 * Add an item from all apps or customize onto the given workspace screen.
3122 * If layout is null, add to the current screen.
3123 */
3124 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003125 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003126 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003127 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003128 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003129
Winson Chungdff8ebb2011-09-08 17:25:31 -07003130 /** Maps the current orientation to an index for referencing orientation correct global icons */
3131 private int getCurrentOrientationIndexForGlobalIcons() {
3132 // default - 0, landscape - 1
3133 switch (getResources().getConfiguration().orientation) {
3134 case Configuration.ORIENTATION_LANDSCAPE:
3135 return 1;
3136 default:
3137 return 0;
3138 }
3139 }
3140
Michael Jurkaae65ee32012-04-30 11:45:54 -07003141 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003142 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003143 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003144 // Look for the toolbar icon specified in the activity meta-data
3145 Bundle metaData = packageManager.getActivityInfo(
3146 activityName, PackageManager.GET_META_DATA).metaData;
3147 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003148 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003149 if (iconResId != 0) {
3150 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003151 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003152 }
3153 }
3154 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003155 // This can happen if the activity defines an invalid drawable
3156 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3157 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003158 } catch (Resources.NotFoundException nfe) {
3159 // This can happen if the activity defines an invalid drawable
3160 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3161 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003162 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003163 return null;
3164 }
3165
3166 // if successful in getting icon, return it; otherwise, set button to use default drawable
3167 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003168 int buttonId, ComponentName activityName, int fallbackDrawableId,
3169 String toolbarResourceName) {
3170 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003171 Resources r = getResources();
3172 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3173 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003174
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003175 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003176 // If we were unable to find the icon via the meta-data, use a generic one
3177 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003178 toolbarIcon = r.getDrawable(fallbackDrawableId);
3179 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003180 if (button != null) {
3181 button.setCompoundDrawables(toolbarIcon, null, null, null);
3182 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003183 return null;
3184 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003185 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 toolbarIcon.getConstantState();
3190 }
3191 }
3192
3193 // if successful in getting icon, return it; otherwise, set button to use default drawable
3194 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003195 int buttonId, ComponentName activityName, int fallbackDrawableId,
3196 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003197 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003198 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003199
Michael Jurka19e0fc52011-07-22 18:00:21 -07003200 if (button != null) {
3201 // If we were unable to find the icon via the meta-data, use a
3202 // generic one
3203 if (toolbarIcon == null) {
3204 button.setImageResource(fallbackDrawableId);
3205 } else {
3206 button.setImageDrawable(toolbarIcon);
3207 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003208 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003209
3210 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3211
Michael Jurka0423dcf2010-10-05 14:56:18 -07003212 }
3213
Winson Chung1b884092012-06-08 17:13:02 -07003214 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003215 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003216 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003217 }
3218
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003219 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003220 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003221 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003222 }
3223
Winson Chungbb185bd2011-11-21 12:31:42 -08003224 private void invalidatePressedFocusedStates(View container, View button) {
3225 if (container instanceof HolographicLinearLayout) {
3226 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3227 layout.invalidatePressedFocusedStates();
3228 } else if (button instanceof HolographicImageView) {
3229 HolographicImageView view = (HolographicImageView) button;
3230 view.invalidatePressedFocusedStates();
3231 }
3232 }
3233
Winson Chungc51db6a2011-10-05 11:44:49 -07003234 private boolean updateGlobalSearchIcon() {
3235 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003236 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003237 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003238 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003239
Winson Chung1cad91e2011-05-25 17:41:01 -07003240 final SearchManager searchManager =
3241 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3242 ComponentName activityName = searchManager.getGlobalSearchActivity();
3243 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003244 int coi = getCurrentOrientationIndexForGlobalIcons();
3245 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003246 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3247 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3248 if (sGlobalSearchIcon[coi] == null) {
3249 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3250 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3251 TOOLBAR_ICON_METADATA_NAME);
3252 }
3253
Winson Chungc51db6a2011-10-05 11:44:49 -07003254 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3255 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003256 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003257 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003258 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003259 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003260 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3261 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3262 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003263 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003264 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003265 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003266 }
3267 }
3268
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003269 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003270 final View searchButtonContainer = findViewById(R.id.search_button_container);
3271 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003272 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003273 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003274 }
3275
Winson Chungc51db6a2011-10-05 11:44:49 -07003276 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003277 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003278 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003279
Winson Chungc51db6a2011-10-05 11:44:49 -07003280 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003281 final SearchManager searchManager =
3282 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3283 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3284
3285 ComponentName activityName = null;
3286 if (globalSearchActivity != null) {
3287 // Check if the global search activity handles voice search
3288 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3289 intent.setPackage(globalSearchActivity.getPackageName());
3290 activityName = intent.resolveActivity(getPackageManager());
3291 }
3292
3293 if (activityName == null) {
3294 // Fallback: check if an activity other than the global search activity
3295 // resolves this
3296 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3297 activityName = intent.resolveActivity(getPackageManager());
3298 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003299 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003300 int coi = getCurrentOrientationIndexForGlobalIcons();
3301 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003302 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3303 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3304 if (sVoiceSearchIcon[coi] == null) {
3305 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3306 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3307 TOOLBAR_ICON_METADATA_NAME);
3308 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003309 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003310 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003311 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003312 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003313 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003314 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003315 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003316 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003317 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003318 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003319 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003320 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003321
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003322 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003323 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3324 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003325 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003326 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003327 }
3328
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003329 public void setVoiceButtonProxyVisible(boolean visible) {
3330 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3331 if (voiceButtonProxy != null) {
3332 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3333 }
3334 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003335 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003336 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003337 */
3338 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003339 final View marketButton = findViewById(R.id.market_button);
3340 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3341 // Find the app market activity by resolving an intent.
3342 // (If multiple app markets are installed, it will return the ResolverActivity.)
3343 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003344 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003345 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003346 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003347 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003348 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3349 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003350 marketButton.setVisibility(View.VISIBLE);
3351 } else {
3352 // We should hide and disable the view so that we don't try and restore the visibility
3353 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3354 marketButton.setVisibility(View.GONE);
3355 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003356 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003357 }
3358
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003359 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003360 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3361 Resources r = getResources();
3362 Drawable marketIconDrawable = d.newDrawable(r);
3363 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3364 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3365 marketIconDrawable.setBounds(0, 0, w, h);
3366
3367 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003368 }
3369
Michael Jurkad7c28052012-04-27 15:43:36 -07003370 @Override
3371 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003372 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003373 final List<CharSequence> text = event.getText();
3374 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003375 // Populate event with a fake title based on the current state.
3376 if (mState == State.APPS_CUSTOMIZE) {
3377 text.add(getString(R.string.all_apps_button_label));
3378 } else {
3379 text.add(getString(R.string.all_apps_home_button_label));
3380 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003381 return result;
3382 }
3383
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003384 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003385 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003386 */
3387 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3388 @Override
3389 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003390 closeSystemDialogs();
3391 }
3392 }
3393
3394 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003395 * Receives notifications whenever the appwidgets are reset.
3396 */
3397 private class AppWidgetResetObserver extends ContentObserver {
3398 public AppWidgetResetObserver() {
3399 super(new Handler());
3400 }
3401
3402 @Override
3403 public void onChange(boolean selfChange) {
3404 onAppWidgetReset();
3405 }
3406 }
3407
3408 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003409 * If the activity is currently paused, signal that we need to run the passed Runnable
3410 * in onResume.
3411 *
3412 * This needs to be called from incoming places where resources might have been loaded
3413 * while we are paused. That is becaues the Configuration might be wrong
3414 * when we're not running, and if it comes back to what it was when we
3415 * were paused, we are not restarted.
3416 *
3417 * Implementation of the method from LauncherModel.Callbacks.
3418 *
3419 * @return true if we are currently paused. The caller might be able to
3420 * skip some work in that case since we will come back again.
3421 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003422 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003423 if (mPaused) {
3424 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003425 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003426 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003427 }
3428 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003429 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003430 return true;
3431 } else {
3432 return false;
3433 }
3434 }
3435
Michael Jurkac402cd92013-05-20 15:49:32 +02003436 private boolean waitUntilResume(Runnable run) {
3437 return waitUntilResume(run, false);
3438 }
3439
Michael Jurka1e2f4652013-07-08 18:03:46 -07003440 public void addOnResumeCallback(Runnable run) {
3441 mBindOnResumeCallbacks.add(run);
3442 }
3443
3444 public void removeOnResumeCallback(Runnable run) {
3445 mBindOnResumeCallbacks.remove(run);
3446 }
3447
Michael Jurka7607c2f2013-04-03 14:33:19 -07003448 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003449 * If the activity is currently paused, signal that we need to re-run the loader
3450 * in onResume.
3451 *
3452 * This needs to be called from incoming places where resources might have been loaded
3453 * while we are paused. That is becaues the Configuration might be wrong
3454 * when we're not running, and if it comes back to what it was when we
3455 * were paused, we are not restarted.
3456 *
3457 * Implementation of the method from LauncherModel.Callbacks.
3458 *
3459 * @return true if we are currently paused. The caller might be able to
3460 * skip some work in that case since we will come back again.
3461 */
3462 public boolean setLoadOnResume() {
3463 if (mPaused) {
3464 Log.i(TAG, "setLoadOnResume");
3465 mOnResumeNeedsLoad = true;
3466 return true;
3467 } else {
3468 return false;
3469 }
3470 }
3471
3472 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003474 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003476 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003477 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003478 } else {
3479 return SCREEN_COUNT / 2;
3480 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003482
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 /**
3484 * Refreshes the shortcuts shown on the workspace.
3485 *
3486 * Implementation of the method from LauncherModel.Callbacks.
3487 */
3488 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003489 // If we're starting binding all over again, clear any bind calls we'd postponed in
3490 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3491 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003492 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003493
Michael Jurka7607c2f2013-04-03 14:33:19 -07003494 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003495 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003496 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003497 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 int count = workspace.getChildCount();
3499 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003500 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003501 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3502 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003503 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003504 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003505 if (mHotseat != null) {
3506 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003507 }
3508 }
3509
Adam Cohendcd297f2013-06-18 13:13:40 -07003510 @Override
3511 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003512 bindAddScreens(orderedScreenIds);
3513 mWorkspace.addExtraEmptyScreen();
3514 }
3515
3516 @Override
3517 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003518 int count = orderedScreenIds.size();
3519 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003520 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003521 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003522 }
3523
Adam Cohen39a06042013-07-19 14:30:12 -07003524 private boolean shouldShowWeightWatcher() {
3525 String spKey = LauncherAppState.getSharedPreferencesKey();
3526 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3527 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3528
3529 return show;
3530 }
3531
3532 private void toggleShowWeightWatcher() {
3533 String spKey = LauncherAppState.getSharedPreferencesKey();
3534 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3535 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3536
3537 show = !show;
3538
3539 SharedPreferences.Editor editor = sp.edit();
3540 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3541 editor.commit();
3542
3543 if (mWeightWatcher != null) {
3544 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3545 }
3546 }
3547
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003548 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003549 * Bind the items start-end from the list.
3550 *
3551 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003552 */
Winson Chung64359a52013-07-08 17:17:08 -07003553 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3554 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003555 if (waitUntilResume(new Runnable() {
3556 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003557 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003558 }
3559 })) {
3560 return;
3561 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003562
Winson Chungf0c6ae02012-03-21 16:10:31 -07003563 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3564 Set<String> newApps = new HashSet<String>();
3565 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3566
Winson Chung64359a52013-07-08 17:17:08 -07003567 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3568 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003569 Workspace workspace = mWorkspace;
3570 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003572
3573 // Short circuit if we are loading dock items for a configuration which has no dock
3574 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3575 mHotseat == null) {
3576 continue;
3577 }
3578
Joe Onorato9c1289c2009-08-17 11:03:03 -04003579 switch (item.itemType) {
3580 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3581 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003582 ShortcutInfo info = (ShortcutInfo) item;
3583 String uri = info.intent.toUri(0).toString();
3584 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003585
Winson Chung64359a52013-07-08 17:17:08 -07003586 /*
3587 * TODO: FIX collision case
3588 */
Winson Chungce376632013-07-11 16:12:41 -07003589 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3590 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3591 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3592 throw new RuntimeException("OCCUPIED");
3593 }
Winson Chung64359a52013-07-08 17:17:08 -07003594 }
3595
Adam Cohendcd297f2013-06-18 13:13:40 -07003596 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3597 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003598 boolean animateIconUp = false;
3599 synchronized (newApps) {
3600 if (newApps.contains(uri)) {
3601 animateIconUp = newApps.remove(uri);
3602 }
3603 }
Winson Chung64359a52013-07-08 17:17:08 -07003604 if (forceAnimateIcons) {
3605 // Animate all the applications up now
3606 shortcut.setAlpha(0f);
3607 shortcut.setScaleX(0f);
3608 shortcut.setScaleY(0f);
3609 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3610 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003611 // Prepare the view to be animated up
3612 shortcut.setAlpha(0f);
3613 shortcut.setScaleX(0f);
3614 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003615 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003616 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3617 mNewShortcutAnimateViews.add(shortcut);
3618 }
3619 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003621 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003622 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003623 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003624 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003625 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3626 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003627 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003629 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003630
Winson Chung64359a52013-07-08 17:17:08 -07003631 if (forceAnimateIcons) {
3632 // We post the animation slightly delayed to prevent slowdowns when we are loading
3633 // right after we return to launcher.
3634 mWorkspace.postDelayed(new Runnable() {
3635 public void run() {
3636 anim.playTogether(bounceAnims);
3637 anim.start();
3638 }
3639 }, NEW_APPS_ANIMATION_DELAY);
3640 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003641 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003642 }
3643
Joe Onorato9c1289c2009-08-17 11:03:03 -04003644 /**
3645 * Implementation of the method from LauncherModel.Callbacks.
3646 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003647 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3648 if (waitUntilResume(new Runnable() {
3649 public void run() {
3650 bindFolders(folders);
3651 }
3652 })) {
3653 return;
3654 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003655 sFolders.clear();
3656 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003657 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003658
3659 /**
3660 * Add the views for a widget to the workspace.
3661 *
3662 * Implementation of the method from LauncherModel.Callbacks.
3663 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003664 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3665 if (waitUntilResume(new Runnable() {
3666 public void run() {
3667 bindAppWidget(item);
3668 }
3669 })) {
3670 return;
3671 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003672
Daniel Sandler843e8602010-06-07 14:59:01 -04003673 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3674 if (DEBUG_WIDGETS) {
3675 Log.d(TAG, "bindAppWidget: " + item);
3676 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003677 final Workspace workspace = mWorkspace;
3678
3679 final int appWidgetId = item.appWidgetId;
3680 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003681 if (DEBUG_WIDGETS) {
3682 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3683 }
3684
Joe Onorato9c1289c2009-08-17 11:03:03 -04003685 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3686
Joe Onorato9c1289c2009-08-17 11:03:03 -04003687 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003688 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689
Adam Cohendcd297f2013-06-18 13:13:40 -07003690 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003692 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3693
Joe Onorato9c1289c2009-08-17 11:03:03 -04003694 workspace.requestLayout();
3695
Daniel Sandler843e8602010-06-07 14:59:01 -04003696 if (DEBUG_WIDGETS) {
3697 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3698 + (SystemClock.uptimeMillis()-start) + "ms");
3699 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 }
3701
Adam Cohen1462de32012-07-24 22:34:36 -07003702 public void onPageBoundSynchronously(int page) {
3703 mSynchronouslyBoundPages.add(page);
3704 }
3705
Joe Onorato9c1289c2009-08-17 11:03:03 -04003706 /**
3707 * Callback saying that there aren't any more items to bind.
3708 *
3709 * Implementation of the method from LauncherModel.Callbacks.
3710 */
Adam Cohene25af792013-06-06 23:08:25 -07003711 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003712 if (waitUntilResume(new Runnable() {
3713 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003714 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003715 }
3716 })) {
3717 return;
3718 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 if (mSavedState != null) {
3720 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003721 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003722 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 mSavedState = null;
3724 }
3725
Winson Chung98ca0f72013-07-29 12:58:51 -07003726 // Create the custom content page here before onLayout to prevent flashing
3727 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3728 mWorkspace.createCustomContentPage();
3729 }
3730
Adam Cohen1462de32012-07-24 22:34:36 -07003731 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003733 // If we received the result of any pending adds while the loader was running (e.g. the
3734 // widget configuration forced an orientation change), process them now.
3735 for (int i = 0; i < sPendingAddList.size(); i++) {
3736 completeAdd(sPendingAddList.get(i));
3737 }
3738 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739
Winson Chungc51db6a2011-10-05 11:44:49 -07003740 // Update the market app icon as necessary (the other icons will be managed in response to
3741 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003742 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003743
Winson Chungf0c6ae02012-03-21 16:10:31 -07003744 // Animate up any icons as necessary
3745 if (mVisible || mWorkspaceLoading) {
3746 Runnable newAppsRunnable = new Runnable() {
3747 @Override
3748 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003749 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003750 }
3751 };
Winson Chunga2413752012-04-03 14:22:34 -07003752
Adam Cohendcd297f2013-06-18 13:13:40 -07003753 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3754 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003755 if (canRunNewAppsAnimation()) {
3756 // If the user has not interacted recently, then either snap to the new page to show
3757 // the new-apps animation or just run them if they are to appear on the current page
3758 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003759 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003760 } else {
3761 runNewAppsAnimation(false);
3762 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003763 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003764 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003765 // are on another page (or just normally if they are added to the current page)
3766 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003767 }
3768 }
3769
3770 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003771 if (upgradePath) {
3772 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003773 mWorkspace.stripDuplicateApps();
3774 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003775 }
Adam Cohen99894d92013-06-14 11:22:59 -07003776
Adam Cohen66a01fd2013-06-11 12:48:00 -07003777 mWorkspace.post(new Runnable() {
3778 @Override
3779 public void run() {
3780 onFinishBindingItems();
3781 }
3782 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003783 }
3784
Winson Chunga2413752012-04-03 14:22:34 -07003785 private boolean canRunNewAppsAnimation() {
3786 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3787 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3788 }
3789
Winson Chungc9168342013-06-26 14:54:55 -07003790 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3791 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3792 PropertyValuesHolder.ofFloat("alpha", 1f),
3793 PropertyValuesHolder.ofFloat("scaleX", 1f),
3794 PropertyValuesHolder.ofFloat("scaleY", 1f));
3795 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3796 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3797 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3798 return bounceAnim;
3799 }
3800
Winson Chungf0c6ae02012-03-21 16:10:31 -07003801 /**
3802 * Runs a new animation that scales up icons that were added while Launcher was in the
3803 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003804 *
3805 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003806 */
Winson Chunga2413752012-04-03 14:22:34 -07003807 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003808 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003809 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003810
3811 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003812 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3813 @Override
3814 public int compare(View a, View b) {
3815 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3816 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3817 int cellCountX = LauncherModel.getCellCountX();
3818 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3819 }
3820 });
Winson Chunga2413752012-04-03 14:22:34 -07003821
3822 // Animate each of the views in place (or show them immediately if requested)
3823 if (immediate) {
3824 for (View v : mNewShortcutAnimateViews) {
3825 v.setAlpha(1f);
3826 v.setScaleX(1f);
3827 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003828 }
Winson Chunga2413752012-04-03 14:22:34 -07003829 } else {
3830 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3831 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003832 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003833 }
3834 anim.playTogether(bounceAnims);
3835 anim.addListener(new AnimatorListenerAdapter() {
3836 @Override
3837 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003838 if (mWorkspace != null) {
3839 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3840 }
Winson Chunga2413752012-04-03 14:22:34 -07003841 }
3842 });
3843 anim.start();
3844 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003845
3846 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003847 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003848 mNewShortcutAnimateViews.clear();
3849 new Thread("clearNewAppsThread") {
3850 public void run() {
3851 mSharedPrefs.edit()
3852 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3853 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3854 .commit();
3855 }
3856 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003857 }
3858
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003859 @Override
3860 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003861 boolean searchVisible = updateGlobalSearchIcon();
3862 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003863 if (mSearchDropTargetBar != null) {
3864 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3865 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003866 }
3867
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003868 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 * Add the icons for all apps.
3870 *
3871 * Implementation of the method from LauncherModel.Callbacks.
3872 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003873 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003874 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3875 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003876 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003877 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879 }
3880
3881 /**
3882 * A package was updated.
3883 *
3884 * Implementation of the method from LauncherModel.Callbacks.
3885 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003886 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3887 if (waitUntilResume(new Runnable() {
3888 public void run() {
3889 bindAppsUpdated(apps);
3890 }
3891 })) {
3892 return;
3893 }
3894
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003895 if (mWorkspace != null) {
3896 mWorkspace.updateShortcuts(apps);
3897 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003898 }
3899
3900 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003901 * A package was uninstalled. We take both the super set of packageNames
3902 * in addition to specific applications to remove, the reason being that
3903 * this can be called when a package is updated as well. In that scenario,
3904 * we only remove specific components from the workspace, where as
3905 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003906 *
3907 * Implementation of the method from LauncherModel.Callbacks.
3908 */
Winson Chung83892cc2013-05-01 16:53:33 -07003909 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3910 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003911 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003912 if (waitUntilResume(new Runnable() {
3913 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003914 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003915 }
3916 })) {
3917 return;
3918 }
3919
Winson Chung64359a52013-07-08 17:17:08 -07003920 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003921 mWorkspace.removeItemsByPackageName(packageNames);
3922 } else {
3923 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003924 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003925
Winson Chunga1820962011-10-03 16:31:06 -07003926 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003927 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003928 }
Joe Onoratobe386092009-11-17 17:32:16 -08003929
3930 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003931 * A number of packages were updated.
3932 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003933 private ArrayList<Object> mWidgetsAndShortcuts;
3934 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003935 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003936 bindPackagesUpdated(mWidgetsAndShortcuts);
3937 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003938 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003939 };
3940
3941 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3942 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3943 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003944 return;
3945 }
3946
Winson Chung64359a52013-07-08 17:17:08 -07003947 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003948 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003949 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003950 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003951 }
3952
Winson Chung400438b2011-01-16 17:53:48 -08003953 private int mapConfigurationOriActivityInfoOri(int configOri) {
3954 final Display d = getWindowManager().getDefaultDisplay();
3955 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3956 switch (d.getRotation()) {
3957 case Surface.ROTATION_0:
3958 case Surface.ROTATION_180:
3959 // We are currently in the same basic orientation as the natural orientation
3960 naturalOri = configOri;
3961 break;
3962 case Surface.ROTATION_90:
3963 case Surface.ROTATION_270:
3964 // We are currently in the other basic orientation to the natural orientation
3965 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3966 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3967 break;
3968 }
3969
3970 int[] oriMap = {
3971 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3972 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3973 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3974 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3975 };
3976 // Since the map starts at portrait, we need to offset if this device's natural orientation
3977 // is landscape.
3978 int indexOffset = 0;
3979 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3980 indexOffset = 1;
3981 }
3982 return oriMap[(d.getRotation() + indexOffset) % 4];
3983 }
Adam Cohen446e9402011-09-15 18:21:21 -07003984
Winson Chung4b919f82012-05-01 10:44:08 -07003985 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003986 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003987 getResources().getBoolean(R.bool.allow_rotation);
3988 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003989 }
Winson Chung4b919f82012-05-01 10:44:08 -07003990 public void lockScreenOrientation() {
3991 if (isRotationEnabled()) {
3992 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3993 .getConfiguration().orientation));
3994 }
3995 }
3996 public void unlockScreenOrientation(boolean immediate) {
3997 if (isRotationEnabled()) {
3998 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003999 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004000 } else {
4001 mHandler.postDelayed(new Runnable() {
4002 public void run() {
4003 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4004 }
4005 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004006 }
Winson Chung4b919f82012-05-01 10:44:08 -07004007 }
Winson Chung400438b2011-01-16 17:53:48 -08004008 }
4009
Winson Chung82f55532011-08-09 14:14:23 -07004010 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004011 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004012 if (DISABLE_CLINGS) {
4013 return false;
4014 }
4015
Brett Chabot2a9e2282011-08-23 15:03:13 -07004016 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004017 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004018
Michael Jurkae233a8b2013-03-19 13:49:20 +01004019 // Restricted secondary users (child mode) will potentially have very few apps
4020 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004021// boolean supportsLimitedUsers =
4022// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4023// Account[] accounts = AccountManager.get(this).getAccounts();
4024// if (supportsLimitedUsers && accounts.length == 0) {
4025// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4026// Bundle restrictions = um.getUserRestrictions();
4027// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4028// return false;
4029// }
4030// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004031 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004032 }
Michael Jurka22143132012-10-04 17:42:38 +02004033
Winson Chung7d7541e2011-09-16 20:14:36 -07004034 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004035 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004036 if (cling != null) {
4037 cling.init(this, positionData);
4038 cling.setVisibility(View.VISIBLE);
4039 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4040 if (animate) {
4041 cling.buildLayer();
4042 cling.setAlpha(0f);
4043 cling.animate()
4044 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004045 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004046 .setDuration(SHOW_CLING_DURATION)
4047 .setStartDelay(delay)
4048 .start();
4049 } else {
4050 cling.setAlpha(1f);
4051 }
Michael Jurka22143132012-10-04 17:42:38 +02004052 cling.setFocusableInTouchMode(true);
4053 cling.post(new Runnable() {
4054 public void run() {
4055 cling.setFocusable(true);
4056 cling.requestFocus();
4057 }
4058 });
4059 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4060 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004061 }
4062 return cling;
4063 }
Michael Jurka22143132012-10-04 17:42:38 +02004064
Winson Chung7d7541e2011-09-16 20:14:36 -07004065 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004066 // To catch cases where siblings of top-level views are made invisible, just check whether
4067 // the cling is directly set to GONE before dismissing it.
4068 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004069 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004070 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004071 anim.addListener(new AnimatorListenerAdapter() {
4072 public void onAnimationEnd(Animator animation) {
4073 cling.setVisibility(View.GONE);
4074 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004075 // We should update the shared preferences on a background thread
4076 new Thread("dismissClingThread") {
4077 public void run() {
4078 SharedPreferences.Editor editor = mSharedPrefs.edit();
4079 editor.putBoolean(flag, true);
4080 editor.commit();
4081 }
4082 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004083 };
4084 });
4085 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004086 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004087 }
4088 }
Michael Jurka22143132012-10-04 17:42:38 +02004089
Winson Chung9d9d74f2011-09-19 11:49:12 -07004090 private void removeCling(int id) {
4091 final View cling = findViewById(id);
4092 if (cling != null) {
4093 final ViewGroup parent = (ViewGroup) cling.getParent();
4094 parent.post(new Runnable() {
4095 @Override
4096 public void run() {
4097 parent.removeView(cling);
4098 }
4099 });
Michael Jurka22143132012-10-04 17:42:38 +02004100 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004101 }
4102 }
Michael Jurka974c3862012-05-22 22:00:31 -07004103
4104 private boolean skipCustomClingIfNoAccounts() {
4105 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4106 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4107 if (customCling) {
4108 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004109 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004110 Account[] accounts = am.getAccountsByType("com.google");
4111 return accounts.length == 0;
4112 }
4113 return false;
4114 }
4115
Winson Chung7d7541e2011-09-16 20:14:36 -07004116 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004117 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004118 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004119 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4120 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004121 // If we're not using the default workspace layout, replace workspace cling
4122 // with a custom workspace cling (usually specified in an overlay)
4123 // For now, only do this on tablets
4124 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004125 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004126 // Use a custom cling
4127 View cling = findViewById(R.id.workspace_cling);
4128 ViewGroup clingParent = (ViewGroup) cling.getParent();
4129 int clingIndex = clingParent.indexOfChild(cling);
4130 clingParent.removeViewAt(clingIndex);
4131 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4132 clingParent.addView(customCling, clingIndex);
4133 customCling.setId(R.id.workspace_cling);
4134 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004135 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004136 } else {
4137 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004138 }
4139 }
4140 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004141 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004142 if (isClingsEnabled() &&
4143 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004144 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004145 } else {
4146 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004147 }
4148 }
4149 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004150 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004151 if (isClingsEnabled() &&
4152 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4153 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004154 } else {
4155 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004156 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004157 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004158 }
4159 public boolean isFolderClingVisible() {
4160 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004161 if (cling != null) {
4162 return cling.getVisibility() == View.VISIBLE;
4163 }
4164 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004165 }
Winson Chung82f55532011-08-09 14:14:23 -07004166 public void dismissWorkspaceCling(View v) {
4167 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004168 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004169 }
4170 public void dismissAllAppsCling(View v) {
4171 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004172 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4173 }
4174 public void dismissFolderCling(View v) {
4175 Cling cling = (Cling) findViewById(R.id.folder_cling);
4176 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004177 }
4178
Winson Chung80baf5a2010-08-09 16:03:15 -07004179 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004180 * Prints out out state for debugging.
4181 */
4182 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004183 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004184 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004185 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4186 Log.d(TAG, "mRestoring=" + mRestoring);
4187 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4188 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004189 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004190 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004191
Winson Chung785d2eb2011-04-14 16:08:02 -07004192 if (mAppsCustomizeContent != null) {
4193 mAppsCustomizeContent.dumpState();
4194 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004195 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004196 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004197
4198 @Override
4199 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4200 super.dump(prefix, fd, writer, args);
4201 writer.println(" ");
4202 writer.println("Debug logs: ");
4203 for (int i = 0; i < sDumpLogs.size(); i++) {
4204 writer.println(" " + sDumpLogs.get(i));
4205 }
4206 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004207
4208 public static void dumpDebugLogsToConsole() {
4209 Log.d(TAG, "");
4210 Log.d(TAG, "*********************");
4211 Log.d(TAG, "Launcher debug logs: ");
4212 for (int i = 0; i < sDumpLogs.size(); i++) {
4213 Log.d(TAG, " " + sDumpLogs.get(i));
4214 }
4215 Log.d(TAG, "*********************");
4216 Log.d(TAG, "");
4217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004218}
Michael Jurka2763be32011-02-24 11:19:57 -08004219
Michael Jurkaabded662011-03-04 12:06:57 -08004220interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004221 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004222 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004223 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004224 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004225 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004226}