blob: c3ea1ef8ac55e220039ff26c3a2befe5d98443c7 [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
Daniel Sandlerff02d492013-08-05 02:12:05 -0400348 private Stats mStats;
349
Michael Jurka0a457bf2012-11-19 14:05:05 -0800350 private static boolean isPropertyEnabled(String propertyName) {
351 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700352 }
353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 @Override
355 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700356 if (DEBUG_STRICT_MODE) {
357 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
358 .detectDiskReads()
359 .detectDiskWrites()
360 .detectNetwork() // or .detectAll() for all detectable problems
361 .penaltyLog()
362 .build());
363 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
364 .detectLeakedSqlLiteObjects()
365 .detectLeakedClosableObjects()
366 .penaltyLog()
367 .penaltyDeath()
368 .build());
369 }
370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400372
373 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
374 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400375 LauncherAppState app = LauncherAppState.getInstance();
376 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700377 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800378 mModel = app.setLauncher(this);
379 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400380 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700382
Daniel Sandlerff02d492013-08-05 02:12:05 -0400383 mStats = new Stats(this);
384
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700385 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700386 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
387 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700388
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700389 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
390 // this also ensures that any synchronous binding below doesn't re-trigger another
391 // LauncherModel load.
392 mPaused = false;
393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200395 android.os.Debug.startMethodTracing(
396 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 }
398
399 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 setContentView(R.layout.launcher);
401 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700402 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800404 registerContentObservers();
405
Joe Onorato7c312c12009-08-13 21:36:53 -0700406 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 mSavedState = savedInstanceState;
409 restoreState(mSavedState);
410
Winson Chunga12a2502010-12-20 14:41:35 -0800411 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700412 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200413 mAppsCustomizeContent.onPackagesUpdated(
414 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700415 }
Winson Chunga12a2502010-12-20 14:41:35 -0800416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 if (PROFILE_STARTUP) {
418 android.os.Debug.stopMethodTracing();
419 }
420
421 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700422 if (sPausedFromUserAction) {
423 // If the user leaves launcher, then we should just load items asynchronously when
424 // they return.
425 mModel.startLoader(true, -1);
426 } else {
427 // We only load the page synchronously if the user rotates (or triggers a
428 // configuration change) while launcher is in the foreground
429 mModel.startLoader(true, mWorkspace.getCurrentPage());
430 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
432
433 // For handling default keys
434 mDefaultKeySsb = new SpannableStringBuilder();
435 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800436
437 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
438 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800439
Adam Cohenf9426d52012-06-04 17:26:21 -0700440 updateGlobalIcons();
441
442 // On large interfaces, we want the screen to auto-rotate based on the current orientation
443 unlockScreenOrientation(true);
444 }
445
Winson Chung4a2afa32012-07-19 14:53:05 -0700446 protected void onUserLeaveHint() {
447 super.onUserLeaveHint();
448 sPausedFromUserAction = true;
449 }
450
Winson Chung98ca0f72013-07-29 12:58:51 -0700451 /** To be overriden by subclasses to hint to Launcher that we have custom content */
452 protected boolean hasCustomContentToLeft() {
453 return false;
454 }
455
Adam Cohenf9426d52012-06-04 17:26:21 -0700456 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700457 boolean searchVisible = false;
458 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800459 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700460 int coi = getCurrentOrientationIndexForGlobalIcons();
461 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
462 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700463 updateAppMarketIcon();
464 searchVisible = updateGlobalSearchIcon();
465 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700466 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700467 if (sGlobalSearchIcon[coi] != null) {
468 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700469 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700470 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700471 if (sVoiceSearchIcon[coi] != null) {
472 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700473 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700474 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700475 if (sAppMarketIcon[coi] != null) {
476 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800477 }
Winson Chungadf0c182012-08-23 14:59:07 -0700478 if (mSearchDropTargetBar != null) {
479 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
Romain Guycbb89e42009-06-08 15:52:54 -0700482
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484 if (sLocaleConfiguration == null) {
485 new AsyncTask<Void, Void, LocaleConfiguration>() {
486 @Override
487 protected LocaleConfiguration doInBackground(Void... unused) {
488 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
489 readConfiguration(Launcher.this, localeConfiguration);
490 return localeConfiguration;
491 }
492
493 @Override
494 protected void onPostExecute(LocaleConfiguration result) {
495 sLocaleConfiguration = result;
496 checkForLocaleChange(); // recursive, but now with a locale configuration
497 }
498 }.execute();
499 return;
500 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700501
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 final Configuration configuration = getResources().getConfiguration();
503
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700504 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 final String locale = configuration.locale.toString();
506
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700507 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 final int mcc = configuration.mcc;
509
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700510 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 final int mnc = configuration.mnc;
512
Romain Guy84f296c2009-11-04 15:00:44 -0800513 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514
Romain Guy84f296c2009-11-04 15:00:44 -0800515 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700516 sLocaleConfiguration.locale = locale;
517 sLocaleConfiguration.mcc = mcc;
518 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700519
Joe Onorato0589f0f2010-02-08 13:44:00 -0800520 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700521
522 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
523 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700524 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700525 public void run() {
526 writeConfiguration(Launcher.this, localeConfiguration);
527 }
528 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700529 }
530 }
531
532 private static class LocaleConfiguration {
533 public String locale;
534 public int mcc = -1;
535 public int mnc = -1;
536 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700537
Romain Guy98d01652009-06-30 16:21:04 -0700538 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
539 DataInputStream in = null;
540 try {
541 in = new DataInputStream(context.openFileInput(PREFERENCES));
542 configuration.locale = in.readUTF();
543 configuration.mcc = in.readInt();
544 configuration.mnc = in.readInt();
545 } catch (FileNotFoundException e) {
546 // Ignore
547 } catch (IOException e) {
548 // Ignore
549 } finally {
550 if (in != null) {
551 try {
552 in.close();
553 } catch (IOException e) {
554 // Ignore
555 }
556 }
557 }
558 }
559
560 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
561 DataOutputStream out = null;
562 try {
563 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
564 out.writeUTF(configuration.locale);
565 out.writeInt(configuration.mcc);
566 out.writeInt(configuration.mnc);
567 out.flush();
568 } catch (FileNotFoundException e) {
569 // Ignore
570 } catch (IOException e) {
571 //noinspection ResultOfMethodCallIgnored
572 context.getFileStreamPath(PREFERENCES).delete();
573 } finally {
574 if (out != null) {
575 try {
576 out.close();
577 } catch (IOException e) {
578 // Ignore
579 }
580 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 }
582 }
583
Bjorn Bringertc459e522013-06-07 19:36:01 +0100584 public LayoutInflater getInflater() {
585 return mInflater;
586 }
587
Adam Cohen716b51e2011-06-30 12:09:54 -0700588 public DragLayer getDragLayer() {
589 return mDragLayer;
590 }
591
Winson Chung36a62fe2012-05-06 18:04:42 -0700592 boolean isDraggingEnabled() {
593 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
594 // that is subsequently removed from the workspace in startBinding().
595 return !mModel.isLoadingWorkspace();
596 }
597
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 static int getScreen() {
599 synchronized (sLock) {
600 return sScreen;
601 }
602 }
603
604 static void setScreen(int screen) {
605 synchronized (sLock) {
606 sScreen = screen;
607 }
608 }
609
Winson Chung557d6ed2011-07-08 15:34:52 -0700610 /**
611 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
612 * a configuration step, this allows the proper animations to run after other transitions.
613 */
614 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700615 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800616 switch (args.requestCode) {
617 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700618 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700619 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800620 break;
621 case REQUEST_PICK_SHORTCUT:
622 processShortcut(args.intent);
623 break;
624 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700625 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700626 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700627 result = true;
628 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800629 case REQUEST_CREATE_APPWIDGET:
630 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700631 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700632 result = true;
633 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800634 case REQUEST_PICK_WALLPAPER:
635 // We just wanted the activity result here so we can clear mWaitingForResult
636 break;
637 }
Michael Jurka27614d22012-04-02 06:40:22 -0700638 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
639 // if you turned the screen off and then back while in All Apps, Launcher would not
640 // return to the workspace. Clearing mAddInfo.container here fixes this issue
641 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700642 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800643 }
644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700646 protected void onActivityResult(
647 final int requestCode, final int resultCode, final Intent data) {
648 if (requestCode == REQUEST_BIND_APPWIDGET) {
649 int appWidgetId = data != null ?
650 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
651 if (resultCode == RESULT_CANCELED) {
652 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
653 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700654 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700655 }
656 return;
657 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700658 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800659 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
660 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700661 mWaitingForResult = false;
662
Adam Cohened66b2b2012-01-23 17:28:51 -0800663 // We have special handling for widgets
664 if (isWidgetDrop) {
665 int appWidgetId = data != null ?
666 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700667 if (appWidgetId < 0) {
668 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
669 "widget configuration activity.");
670 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
671 } else {
672 completeTwoStageWidgetDrop(resultCode, appWidgetId);
673 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800674 return;
675 }
676
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 // The pattern used here is that a user PICKs a specific application,
678 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
681 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700682 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 final PendingAddArguments args = new PendingAddArguments();
684 args.requestCode = requestCode;
685 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700686 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700687 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700688 args.cellX = mPendingAddInfo.cellX;
689 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800690 if (isWorkspaceLocked()) {
691 sPendingAddList.add(args);
692 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700693 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800696 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700697 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800698 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
699 null);
700 }
701
702 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800705 Runnable onCompleteRunnable = null;
706 int animationType = 0;
707
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700708 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800709 if (resultCode == RESULT_OK) {
710 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
711 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700712 mPendingAddWidgetInfo);
713 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800714 onCompleteRunnable = new Runnable() {
715 @Override
716 public void run() {
717 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800719 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
720 null);
721 }
722 };
723 } else if (resultCode == RESULT_CANCELED) {
724 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
725 onCompleteRunnable = new Runnable() {
726 @Override
727 public void run() {
728 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
729 null);
730 }
731 };
732 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700733 if (mDragLayer.getAnimatedView() != null) {
734 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
735 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
736 animationType, boundWidget, true);
737 } else {
738 // The animated view may be null in the case of a rotation during widget configuration
739 onCompleteRunnable.run();
740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
742
743 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700744 protected void onStop() {
745 super.onStop();
746 FirstFrameAnimatorHelper.setIsVisible(false);
747 }
748
749 @Override
750 protected void onStart() {
751 super.onStart();
752 FirstFrameAnimatorHelper.setIsVisible(true);
753 }
754
755 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200757 long startTime = 0;
758 if (DEBUG_RESUME_TIME) {
759 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400760 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200761 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700763
Winson Chung4a2afa32012-07-19 14:53:05 -0700764 // Restore the previous launcher state
765 if (mOnResumeState == State.WORKSPACE) {
766 showWorkspace(false);
767 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
768 showAllApps(false);
769 }
770 mOnResumeState = State.NONE;
771
Craig Mautner360310b2012-10-26 15:13:08 -0700772 // Background was set to gradient in onPause(), restore to black if in all apps.
773 setWorkspaceBackground(mState == State.WORKSPACE);
774
Winson Chungde0fb8f2012-05-08 14:37:08 -0700775 // Process any items that were added while Launcher was away
776 InstallShortcutReceiver.flushInstallQueue(this);
777
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800778 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700779 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700780 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400781 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700782 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400783 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700784 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700786 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200787 // We might have postponed some bind calls until onResume (see waitUntilResume) --
788 // execute them here
789 long startTimeCallbacks = 0;
790 if (DEBUG_RESUME_TIME) {
791 startTimeCallbacks = System.currentTimeMillis();
792 }
793
794 if (mAppsCustomizeContent != null) {
795 mAppsCustomizeContent.setBulkBind(true);
796 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700797 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
798 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200799 }
800 if (mAppsCustomizeContent != null) {
801 mAppsCustomizeContent.setBulkBind(false);
802 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700803 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200804 if (DEBUG_RESUME_TIME) {
805 Log.d(TAG, "Time spent processing callbacks in onResume: " +
806 (System.currentTimeMillis() - startTimeCallbacks));
807 }
Michael Jurka447bf842013-05-15 14:52:15 +0200808 }
Winson Chunge4e50662012-01-23 14:45:13 -0800809
810 // Reset the pressed state of icons that were locked in the press state while activities
811 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800812 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800813 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800814 mWaitingForResume.setStayPressed(false);
815 }
Winson Chunge4e50662012-01-23 14:45:13 -0800816 if (mAppsCustomizeContent != null) {
817 // Resets the previous all apps icon press state
818 mAppsCustomizeContent.resetDrawableState();
819 }
Adam Cohen06dff352012-06-01 17:17:08 -0700820 // It is possible that widgets can receive updates while launcher is not in the foreground.
821 // Consequently, the widgets will be inflated in the orientation of the foreground activity
822 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
823 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700824 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700825
826 // Again, as with the above scenario, it's possible that one or more of the global icons
827 // were updated in the wrong orientation.
828 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200829 if (DEBUG_RESUME_TIME) {
830 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
831 }
Adam Cohen06dff352012-06-01 17:17:08 -0700832 }
833
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700835 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700836 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
837 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
838 // when Launcher resumes and we are still in AllApps.
839 updateWallpaperVisibility(true);
840
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700841 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700842 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800843 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700844 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700845 }
Romain Guycbb89e42009-06-08 15:52:54 -0700846
Adam Cohen66a01fd2013-06-11 12:48:00 -0700847 protected void onFinishBindingItems() {
848 }
849
Adam Cohenbffe7452013-07-22 18:21:45 -0700850 QSBScroller mQsbScroller = new QSBScroller() {
851 int scrollY = 0;
852
853 @Override
854 public void setScrollY(int scroll) {
855 scrollY = scroll;
856
857 if (mWorkspace.isOnOrMovingToCustomContent()) {
858 mSearchDropTargetBar.setTranslationY(- scrollY);
859 }
860 }
861 };
862
863 public void resetQSBScroll() {
864 mSearchDropTargetBar.animate().translationY(0).start();
865 }
866
867 public interface CustomContentCallbacks {
868 // Custom content is completely shown
869 public void onShow();
870
871 // Custom content is completely hidden
872 public void onHide();
873 }
874
875 public interface QSBScroller {
876 public void setScrollY(int scrollY);
877 }
878
Adam Cohen66a01fd2013-06-11 12:48:00 -0700879 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700880 public QSBScroller addToCustomContentPage(View customContent) {
881 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700882 }
883
Winson Chung98ca0f72013-07-29 12:58:51 -0700884 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700885 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700886 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700887 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700888 }
889
Adam Cohenedb40762013-07-18 16:45:45 -0700890 // The custom content needs to offset its content to account for the QSB
891 public int getTopOffsetForCustomContent() {
892 return mWorkspace.getPaddingTop();
893 }
894
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700895 @Override
896 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897 // Flag the loader to stop early before switching
898 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700899 if (mAppsCustomizeContent != null) {
900 mAppsCustomizeContent.surrender();
901 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800902 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700903 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700904
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800905 // We can't hide the IME if it was forced open. So don't bother
906 /*
907 @Override
908 public void onWindowFocusChanged(boolean hasFocus) {
909 super.onWindowFocusChanged(hasFocus);
910
911 if (hasFocus) {
912 final InputMethodManager inputManager = (InputMethodManager)
913 getSystemService(Context.INPUT_METHOD_SERVICE);
914 WindowManager.LayoutParams lp = getWindow().getAttributes();
915 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
916 android.os.Handler()) {
917 protected void onReceiveResult(int resultCode, Bundle resultData) {
918 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
919 }
920 });
921 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
922 }
923 }
924 */
925
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 private boolean acceptFilter() {
927 final InputMethodManager inputManager = (InputMethodManager)
928 getSystemService(Context.INPUT_METHOD_SERVICE);
929 return !inputManager.isFullscreenMode();
930 }
931
932 @Override
933 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700934 final int uniChar = event.getUnicodeChar();
935 final boolean handled = super.onKeyDown(keyCode, event);
936 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
937 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
939 keyCode, event);
940 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700941 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700942 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700943 // showSearchDialog()
944 // If there are multiple keystrokes before the search dialog takes focus,
945 // onSearchRequested() will be called for every keystroke,
946 // but it is idempotent, so it's fine.
947 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
949 }
950
Joe Onorato8a9625e2010-01-28 15:55:35 -0800951 // Eat the long press event so the keyboard doesn't come up.
952 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
953 return true;
954 }
955
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 return handled;
957 }
958
Karl Rosaen138a0412009-04-23 19:00:21 -0700959 private String getTypedText() {
960 return mDefaultKeySsb.toString();
961 }
962
963 private void clearTypedText() {
964 mDefaultKeySsb.clear();
965 mDefaultKeySsb.clearSpans();
966 Selection.setSelection(mDefaultKeySsb, 0);
967 }
968
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700970 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
971 * State
972 */
973 private static State intToState(int stateOrdinal) {
974 State state = State.WORKSPACE;
975 final State[] stateValues = State.values();
976 for (int i = 0; i < stateValues.length; i++) {
977 if (stateValues[i].ordinal() == stateOrdinal) {
978 state = stateValues[i];
979 break;
980 }
981 }
982 return state;
983 }
984
985 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 * Restores the previous state, if it exists.
987 *
988 * @param savedState The previous state.
989 */
990 private void restoreState(Bundle savedState) {
991 if (savedState == null) {
992 return;
993 }
994
Michael Jurka883f55b2010-10-21 15:47:14 -0700995 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700996 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700997 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800998 }
999
Winson Chungf0c6ae02012-03-21 16:10:31 -07001000 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001002 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
1004
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001007
Winson Chung3d503fb2011-07-13 17:25:49 -07001008 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1009 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001010 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001011 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1012 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001013 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1014 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1015 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001016 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 mRestoring = true;
1018 }
1019
1020 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1021 if (renameFolder) {
1022 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001023 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 mRestoring = true;
1025 }
Winson Chunga12a2502010-12-20 14:41:35 -08001026
Winson Chung785d2eb2011-04-14 16:08:02 -07001027 // Restore the AppsCustomize tab
1028 if (mAppsCustomizeTabHost != null) {
1029 String curTab = savedState.getString("apps_customize_currentTab");
1030 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001031 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001032 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001033 mAppsCustomizeContent.loadAssociatedPages(
1034 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001035 }
1036
Winson Chung5afbf7b2011-07-25 11:53:08 -07001037 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1038 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 }
1041
1042 /**
1043 * Finds all the views we need and configure them properly.
1044 */
1045 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001046 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001047
Craig Mautner360310b2012-10-26 15:13:08 -07001048 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001049 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1050 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001051 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001052
1053 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1054 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055
Winson Chung4c98d922011-05-31 16:50:48 -07001056 // Setup the drag layer
1057 mDragLayer.setup(this, dragController);
1058
Winson Chung3d503fb2011-07-13 17:25:49 -07001059 // Setup the hotseat
1060 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1061 if (mHotseat != null) {
1062 mHotseat.setup(this);
1063 }
1064
Winson Chung4c98d922011-05-31 16:50:48 -07001065 // Setup the workspace
1066 mWorkspace.setHapticFeedbackEnabled(false);
1067 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001068 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001069 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001070
Winson Chungf0ea4d32011-06-06 14:27:16 -07001071 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001072 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001073
Winson Chungf0ea4d32011-06-06 14:27:16 -07001074 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001075 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001076 mAppsCustomizeContent = (AppsCustomizePagedView)
1077 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1078 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001079
Winson Chung3d503fb2011-07-13 17:25:49 -07001080 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001081 dragController.setDragScoller(mWorkspace);
1082 dragController.setScrollView(mDragLayer);
1083 dragController.setMoveTarget(mWorkspace);
1084 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001085 if (mSearchDropTargetBar != null) {
1086 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001087 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001088
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001089 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001090 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001091 mWeightWatcher = new WeightWatcher(this);
1092 mWeightWatcher.setAlpha(0.5f);
1093 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001094 new FrameLayout.LayoutParams(
1095 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001096 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001097 Gravity.BOTTOM)
1098 );
Adam Cohen39a06042013-07-19 14:30:12 -07001099
1100 boolean show = shouldShowWeightWatcher();
1101 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 }
1104
1105 /**
1106 * Creates a view representing a shortcut.
1107 *
1108 * @param info The data structure describing the shortcut.
1109 *
1110 * @return A View inflated from R.layout.application.
1111 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001112 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001114 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 }
1116
1117 /**
1118 * Creates a view representing a shortcut inflated from the specified resource.
1119 *
1120 * @param layoutResId The id of the XML layout used to create the shortcut.
1121 * @param parent The group the shortcut belongs to.
1122 * @param info The data structure describing the shortcut.
1123 *
1124 * @return A View inflated from layoutResId.
1125 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001126 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001127 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1128 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 return favorite;
1131 }
1132
1133 /**
1134 * Add an application shortcut to the workspace.
1135 *
1136 * @param data The intent describing the application.
1137 * @param cellInfo The position on screen where to create the shortcut.
1138 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001139 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001141 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001142
Adam Cohenfbba09b2011-07-18 21:43:05 -07001143 // First we check if we already know the exact location where we want to add this item.
1144 if (cellX >= 0 && cellY >= 0) {
1145 cellXY[0] = cellX;
1146 cellXY[1] = cellY;
1147 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001148 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149 return;
1150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001152 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001153
Romain Guy73b979d2009-06-09 12:57:21 -07001154 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001155 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001157 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001158 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001159 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001160 } else {
1161 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 }
1163 }
Romain Guycbb89e42009-06-08 15:52:54 -07001164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 /**
1166 * Add a shortcut to the workspace.
1167 *
1168 * @param data The intent describing the shortcut.
1169 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001171 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001172 int cellY) {
1173 int[] cellXY = mTmpAddItemCellCoordinates;
1174 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001175 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001176
Michael Jurkad3ef3062010-11-23 16:23:58 -08001177 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001178
Adam Cohen558baaf2011-08-15 15:22:57 -07001179 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001180 if (info == null) {
1181 return;
1182 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001183 final View view = createShortcut(info);
1184
Adam Cohenfbba09b2011-07-18 21:43:05 -07001185 // First we check if we already know the exact location where we want to add this item.
1186 if (cellX >= 0 && cellY >= 0) {
1187 cellXY[0] = cellX;
1188 cellXY[1] = cellY;
1189 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001190
1191 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001192 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001193 true, null,null)) {
1194 return;
1195 }
1196 DragObject dragObject = new DragObject();
1197 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001198 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1199 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001200 return;
1201 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001202 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001203 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001204 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001205 foundCellSpan = (result != null);
1206 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001207 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001208 }
1209
1210 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001211 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001212 return;
1213 }
1214
Adam Cohendcd297f2013-06-18 13:13:40 -07001215 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216
1217 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001218 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001219 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
1221 }
1222
Adam Cohen2f093b62012-04-30 18:59:53 -07001223 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1224 int minHeight) {
1225 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001226 // We want to account for the extra amount of padding that we are adding to the widget
1227 // to ensure that it gets the full amount of space that it has requested
1228 int requiredWidth = minWidth + padding.left + padding.right;
1229 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001230 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001231 }
1232
Adam Cohen2f093b62012-04-30 18:59:53 -07001233 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1234 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001235 }
1236
Adam Cohen2f093b62012-04-30 18:59:53 -07001237 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1238 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001239 }
1240
Adam Cohen2f093b62012-04-30 18:59:53 -07001241 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1242 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001243 }
1244
Adam Cohen2f093b62012-04-30 18:59:53 -07001245 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1246 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001247 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001248 }
1249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001251 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001253 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001254 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001256 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001257 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1258 if (appWidgetInfo == null) {
1259 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1260 }
Romain Guycbb89e42009-06-08 15:52:54 -07001261
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001262 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001263 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001264
Adam Cohen2f093b62012-04-30 18:59:53 -07001265 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1266 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001269 // We have saved the position to which the widget was dragged-- this really only matters
1270 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001271 int[] cellXY = mTmpAddItemCellCoordinates;
1272 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001273 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001274 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001275 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1276 cellXY[0] = mPendingAddInfo.cellX;
1277 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001278 spanXY[0] = mPendingAddInfo.spanX;
1279 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001280 foundCellSpan = true;
1281 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001282 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001283 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001284 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1285 spanXY[1], cellXY, finalSpan);
1286 spanXY[0] = finalSpan[0];
1287 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001288 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001289 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001290 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001291 }
1292
Michael Jurka0280c3b2010-09-17 15:00:07 -07001293 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001294 if (appWidgetId != -1) {
1295 // Deleting an app widget ID is a void call but writes to disk before returning
1296 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001297 new Thread("deleteAppWidgetId") {
1298 public void run() {
1299 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1300 }
1301 }.start();
1302 }
Winson Chung93eef082012-03-23 15:59:27 -07001303 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001304 return;
1305 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001307 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001308 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1309 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001310 launcherInfo.spanX = spanXY[0];
1311 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001312 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1313 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001316 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317
1318 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001319 if (hostView == null) {
1320 // Perform actual inflation because we're live
1321 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1322 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1323 } else {
1324 // The AppWidgetHostView has already been inflated and instantiated
1325 launcherInfo.hostView = hostView;
1326 }
Romain Guycbb89e42009-06-08 15:52:54 -07001327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001329 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001330 launcherInfo.notifyWidgetSizeChanged(this);
1331
Adam Cohendcd297f2013-06-18 13:13:40 -07001332 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001334
1335 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001337 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
Romain Guycbb89e42009-06-08 15:52:54 -07001339
Adam Cohended9f8d2010-11-03 13:25:16 -07001340 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1341 @Override
1342 public void onReceive(Context context, Intent intent) {
1343 final String action = intent.getAction();
1344 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1345 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001346 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001347 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001348
Winson Chungc100e8e2011-08-09 16:02:43 -07001349 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001350 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001351 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1352 mAppsCustomizeTabHost.reset();
1353 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001354 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001355 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1356 mUserPresent = true;
1357 updateRunning();
1358 }
1359 }
1360 };
1361
1362 @Override
1363 public void onAttachedToWindow() {
1364 super.onAttachedToWindow();
1365
1366 // Listen for broadcasts related to user-presence
1367 final IntentFilter filter = new IntentFilter();
1368 filter.addAction(Intent.ACTION_SCREEN_OFF);
1369 filter.addAction(Intent.ACTION_USER_PRESENT);
1370 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001371 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001372 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001373 mVisible = true;
1374 }
1375
1376 @Override
1377 public void onDetachedFromWindow() {
1378 super.onDetachedFromWindow();
1379 mVisible = false;
1380
Adam Cohend113e0c2010-11-11 10:48:05 -08001381 if (mAttached) {
1382 unregisterReceiver(mReceiver);
1383 mAttached = false;
1384 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001385 updateRunning();
1386 }
1387
1388 public void onWindowVisibilityChanged(int visibility) {
1389 mVisible = visibility == View.VISIBLE;
1390 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001391 // The following code used to be in onResume, but it turns out onResume is called when
1392 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1393 // is a more appropriate event to handle
1394 if (mVisible) {
1395 mAppsCustomizeTabHost.onWindowVisible();
1396 if (!mWorkspaceLoading) {
1397 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001398 // We want to let Launcher draw itself at least once before we force it to build
1399 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001400 // apps is nice and speedy.
1401 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001402 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001403 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001404 if (mStarted) return;
1405 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001406 // We delay the layer building a bit in order to give
1407 // other message processing a time to run. In particular
1408 // this avoids a delay in hiding the IME if it was
1409 // currently shown, because doing that may involve
1410 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001411 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001412 final ViewTreeObserver.OnDrawListener listener = this;
1413 mWorkspace.post(new Runnable() {
1414 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001415 if (mWorkspace != null &&
1416 mWorkspace.getViewTreeObserver() != null) {
1417 mWorkspace.getViewTreeObserver().
1418 removeOnDrawListener(listener);
1419 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001420 }
1421 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001422 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001423 }
1424 });
1425 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001426 // When Launcher comes back to foreground, a different Activity might be responsible for
1427 // the app market intent, so refresh the icon
1428 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001429 clearTypedText();
1430 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001431 }
1432
1433 private void sendAdvanceMessage(long delay) {
1434 mHandler.removeMessages(ADVANCE_MSG);
1435 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1436 mHandler.sendMessageDelayed(msg, delay);
1437 mAutoAdvanceSentTime = System.currentTimeMillis();
1438 }
1439
1440 private void updateRunning() {
1441 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1442 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1443 mAutoAdvanceRunning = autoAdvanceRunning;
1444 if (autoAdvanceRunning) {
1445 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1446 sendAdvanceMessage(delay);
1447 } else {
1448 if (!mWidgetsToAdvance.isEmpty()) {
1449 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1450 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1451 }
1452 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001453 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001454 }
1455 }
1456 }
1457
1458 private final Handler mHandler = new Handler() {
1459 @Override
1460 public void handleMessage(Message msg) {
1461 if (msg.what == ADVANCE_MSG) {
1462 int i = 0;
1463 for (View key: mWidgetsToAdvance.keySet()) {
1464 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1465 final int delay = mAdvanceStagger * i;
1466 if (v instanceof Advanceable) {
1467 postDelayed(new Runnable() {
1468 public void run() {
1469 ((Advanceable) v).advance();
1470 }
1471 }, delay);
1472 }
1473 i++;
1474 }
1475 sendAdvanceMessage(mAdvanceInterval);
1476 }
1477 }
1478 };
1479
1480 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001481 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001482 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1483 if (v instanceof Advanceable) {
1484 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001485 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001486 updateRunning();
1487 }
1488 }
1489
1490 void removeWidgetToAutoAdvance(View hostView) {
1491 if (mWidgetsToAdvance.containsKey(hostView)) {
1492 mWidgetsToAdvance.remove(hostView);
1493 updateRunning();
1494 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001495 }
1496
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001498 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001499 launcherInfo.hostView = null;
1500 }
1501
Winson Chung93eef082012-03-23 15:59:27 -07001502 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1503 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1504 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001505 }
1506
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001507 public LauncherAppWidgetHost getAppWidgetHost() {
1508 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
Romain Guycbb89e42009-06-08 15:52:54 -07001510
Winson Chunga9abd0e2010-10-27 17:18:37 -07001511 public LauncherModel getModel() {
1512 return mModel;
1513 }
1514
Bjorn Bringertc459e522013-06-07 19:36:01 +01001515 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001516 getWindow().closeAllPanels();
1517
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001518 // Whatever we were doing is hereby canceled.
1519 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001520 }
1521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 @Override
1523 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001524 long startTime = 0;
1525 if (DEBUG_RESUME_TIME) {
1526 startTime = System.currentTimeMillis();
1527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 super.onNewIntent(intent);
1529
1530 // Close the menu
1531 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001532 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001533 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001534
Jamie Genniscb222e82012-10-23 15:44:26 -07001535 final boolean alreadyOnHome =
1536 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001537 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001538
Jamie Genniscb222e82012-10-23 15:44:26 -07001539 Runnable processIntent = new Runnable() {
1540 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001541 if (mWorkspace == null) {
1542 // Can be cases where mWorkspace is null, this prevents a NPE
1543 return;
1544 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001545 Folder openFolder = mWorkspace.getOpenFolder();
1546 // In all these cases, only animate if we're already on home
1547 mWorkspace.exitWidgetResizeMode();
1548 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1549 openFolder == null) {
1550 mWorkspace.moveToDefaultScreen(true);
1551 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001552
Jamie Genniscb222e82012-10-23 15:44:26 -07001553 closeFolder();
1554 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001555
Jamie Genniscb222e82012-10-23 15:44:26 -07001556 // If we are already on home, then just animate back to the workspace,
1557 // otherwise, just wait until onResume to set the state back to Workspace
1558 if (alreadyOnHome) {
1559 showWorkspace(true);
1560 } else {
1561 mOnResumeState = State.WORKSPACE;
1562 }
1563
1564 final View v = getWindow().peekDecorView();
1565 if (v != null && v.getWindowToken() != null) {
1566 InputMethodManager imm = (InputMethodManager)getSystemService(
1567 INPUT_METHOD_SERVICE);
1568 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1569 }
1570
1571 // Reset AllApps to its initial state
1572 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1573 mAppsCustomizeTabHost.reset();
1574 }
1575 }
1576 };
1577
1578 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1579 // Delay processing of the intent to allow the status bar animation to finish
1580 // first in order to avoid janky animations.
1581 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001582 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001583 // Process the intent immediately.
1584 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001585 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
Michael Jurka447bf842013-05-15 14:52:15 +02001588 if (DEBUG_RESUME_TIME) {
1589 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
1592
1593 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001594 public void onRestoreInstanceState(Bundle state) {
1595 super.onRestoreInstanceState(state);
1596 for (int page: mSynchronouslyBoundPages) {
1597 mWorkspace.restoreInstanceStateForChild(page);
1598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
1600
1601 @Override
1602 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001603 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001604 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605
Michael Jurka883f55b2010-10-21 15:47:14 -07001606 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001607 // We close any open folder since it will not be re-opened, and we need to make sure
1608 // this state is reflected.
1609 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610
Adam Cohendcd297f2013-06-18 13:13:40 -07001611 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001612 mWaitingForResult) {
1613 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001614 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001615 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1616 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001617 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1618 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1619 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 }
1621
1622 if (mFolderInfo != null && mWaitingForResult) {
1623 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1624 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1625 }
Michael Jurka946ad472010-07-09 18:05:18 -07001626
Winson Chung785d2eb2011-04-14 16:08:02 -07001627 // Save the current AppsCustomize tab
1628 if (mAppsCustomizeTabHost != null) {
1629 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1630 if (currentTabTag != null) {
1631 outState.putString("apps_customize_currentTab", currentTabTag);
1632 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001633 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1634 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001635 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 }
1637
1638 @Override
1639 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001641
Winson Chunge7a03942011-08-05 15:05:12 -07001642 // Remove all pending runnables
1643 mHandler.removeMessages(ADVANCE_MSG);
1644 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001645 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001646
Winson Chungcd2b0142011-06-08 16:02:26 -07001647 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001648 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001649 mModel.stopLoader();
1650 app.setLauncher(null);
1651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001653 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001655 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001657 mAppWidgetHost = null;
1658
1659 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660
1661 TextKeyListener.getInstance().release();
1662
Winson Chung81b52252012-08-27 15:34:29 -07001663 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1664 // to prevent leaking Launcher activities on orientation change.
1665 if (mModel != null) {
1666 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1667 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001668
1669 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001670 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001671
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001672 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001673 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1674 mWorkspace.removeAllViews();
1675 mWorkspace = null;
1676 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001677
Michael Jurka2ecf9952012-06-18 12:52:28 -07001678 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
1680
Adam Cohena9cf38f2011-05-02 15:36:58 -07001681 public DragController getDragController() {
1682 return mDragController;
1683 }
1684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 @Override
1686 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001687 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 super.startActivityForResult(intent, requestCode);
1689 }
1690
Winson Chung70d72102011-08-12 11:24:35 -07001691 /**
1692 * Indicates that we want global search for this activity by setting the globalSearch
1693 * argument for {@link #startSearch} to true.
1694 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001696 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001698
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001699 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001700
Karl Rosaen138a0412009-04-23 19:00:21 -07001701 if (initialQuery == null) {
1702 // Use any text typed in the launcher as the initial query
1703 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 if (appSearchData == null) {
1706 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001707 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 }
Winson Chungadf0c182012-08-23 14:59:07 -07001709 Rect sourceBounds = new Rect();
1710 if (mSearchDropTargetBar != null) {
1711 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1712 }
Romain Guycbb89e42009-06-08 15:52:54 -07001713
Bjorn Bringertc459e522013-06-07 19:36:01 +01001714 startSearch(initialQuery, selectInitialQuery,
1715 appSearchData, sourceBounds);
1716 }
1717
1718 public void startSearch(String initialQuery,
1719 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001720 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001721 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001722 }
1723
1724 /**
1725 * Starts the global search activity. This code is a copied from SearchManager
1726 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001727 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001728 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001729 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001730 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1731 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1732 if (globalSearchActivity == null) {
1733 Log.w(TAG, "No global search activity found.");
1734 return;
1735 }
1736 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1737 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1738 intent.setComponent(globalSearchActivity);
1739 // Make sure that we have a Bundle to put source in
1740 if (appSearchData == null) {
1741 appSearchData = new Bundle();
1742 } else {
1743 appSearchData = new Bundle(appSearchData);
1744 }
1745 // Set source to package name of app that starts global search, if not set already.
1746 if (!appSearchData.containsKey("source")) {
1747 appSearchData.putString("source", getPackageName());
1748 }
1749 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1750 if (!TextUtils.isEmpty(initialQuery)) {
1751 intent.putExtra(SearchManager.QUERY, initialQuery);
1752 }
1753 if (selectInitialQuery) {
1754 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1755 }
1756 intent.setSourceBounds(sourceBounds);
1757 try {
1758 startActivity(intent);
1759 } catch (ActivityNotFoundException ex) {
1760 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1761 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 }
1763
Winson Chung70d72102011-08-12 11:24:35 -07001764 @Override
1765 public boolean onCreateOptionsMenu(Menu menu) {
1766 if (isWorkspaceLocked()) {
1767 return false;
1768 }
1769
1770 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001771
1772 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1773 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1774 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001775 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1776 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1777 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001778 String helpUrl = getString(R.string.help_url);
1779 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001780 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1781 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1782
Winson Chung70d72102011-08-12 11:24:35 -07001783 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1784 .setIcon(android.R.drawable.ic_menu_gallery)
1785 .setAlphabeticShortcut('W');
1786 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1787 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001788 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001789 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001790 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1791 .setIcon(android.R.drawable.ic_menu_preferences)
1792 .setIntent(settings)
1793 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001794 if (!helpUrl.isEmpty()) {
1795 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1796 .setIcon(android.R.drawable.ic_menu_help)
1797 .setIntent(help)
1798 .setAlphabeticShortcut('H');
1799 }
Winson Chung70d72102011-08-12 11:24:35 -07001800 return true;
1801 }
1802
1803 @Override
1804 public boolean onPrepareOptionsMenu(Menu menu) {
1805 super.onPrepareOptionsMenu(menu);
1806
1807 if (mAppsCustomizeTabHost.isTransitioning()) {
1808 return false;
1809 }
1810 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1811 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1812
1813 return true;
1814 }
1815
1816 @Override
1817 public boolean onOptionsItemSelected(MenuItem item) {
1818 switch (item.getItemId()) {
1819 case MENU_WALLPAPER_SETTINGS:
1820 startWallpaper();
1821 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001822 }
1823
1824 return super.onOptionsItemSelected(item);
1825 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001827 @Override
1828 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001829 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001830 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001831 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001832 }
1833
Joe Onorato9c1289c2009-08-17 11:03:03 -04001834 public boolean isWorkspaceLocked() {
1835 return mWorkspaceLoading || mWaitingForResult;
1836 }
1837
Michael Jurka0280c3b2010-09-17 15:00:07 -07001838 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001839 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001840 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001841 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1842 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001843 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001844 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001845 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001846
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001847 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1848 AppWidgetProviderInfo appWidgetInfo) {
1849 if (appWidgetInfo.configure != null) {
1850 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001851
Bjorn Bringert7984c942009-12-09 15:38:25 +00001852 // Launch over to configure widget, if needed
1853 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001854 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001855 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001856 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001858 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001859 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001860 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001861 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001862 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
1864 }
Romain Guycbb89e42009-06-08 15:52:54 -07001865
Adam Cohenfbba09b2011-07-18 21:43:05 -07001866 /**
1867 * Process a shortcut drop.
1868 *
1869 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001870 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001871 * @param cell The cell it should be added to, optional
1872 * @param position The location on the screen where it was dropped, optional
1873 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001874 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001875 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001876 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001877 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001878 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001879 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001880
1881 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001882 mPendingAddInfo.cellX = cell[0];
1883 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001884 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001885
1886 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1887 createShortcutIntent.setComponent(componentName);
1888 processShortcut(createShortcutIntent);
1889 }
1890
Adam Cohenfbba09b2011-07-18 21:43:05 -07001891 /**
1892 * Process a widget drop.
1893 *
1894 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001895 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001896 * @param cell The cell it should be added to, optional
1897 * @param position The location on the screen where it was dropped, optional
1898 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001899 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001900 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001901 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001902 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001903 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001904 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001905 mPendingAddInfo.minSpanX = info.minSpanX;
1906 mPendingAddInfo.minSpanY = info.minSpanY;
1907
Adam Cohenfbba09b2011-07-18 21:43:05 -07001908 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001909 mPendingAddInfo.cellX = cell[0];
1910 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001911 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001912 if (span != null) {
1913 mPendingAddInfo.spanX = span[0];
1914 mPendingAddInfo.spanY = span[1];
1915 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001916
Adam Cohened66b2b2012-01-23 17:28:51 -08001917 AppWidgetHostView hostView = info.boundWidget;
1918 int appWidgetId;
1919 if (hostView != null) {
1920 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001921 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001922 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001923 // In this case, we either need to start an activity to get permission to bind
1924 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001925 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001926 Bundle options = info.bindOptions;
1927
1928 boolean success = false;
1929 if (options != null) {
1930 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1931 info.componentName, options);
1932 } else {
1933 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1934 info.componentName);
1935 }
1936 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001937 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001938 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001939 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001940 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1941 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1942 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001943 // TODO: we need to make sure that this accounts for the options bundle.
1944 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001945 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1946 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001947 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001948 }
1949
Joe Onoratodeb98af2010-02-19 14:59:39 -08001950 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001951 // Handle case where user selected "Applications"
1952 String applicationName = getResources().getString(R.string.group_applications);
1953 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001954
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001955 if (applicationName != null && applicationName.equals(shortcutName)) {
1956 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1957 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001958
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001959 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1960 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001961 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001962 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001963 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001964 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
1967
Winson Chung24ab2f12010-09-16 14:10:47 -07001968 void processWallpaper(Intent intent) {
1969 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1970 }
1971
Adam Cohendcd297f2013-06-18 13:13:40 -07001972 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001973 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001974 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 folderInfo.title = getText(R.string.folder_name);
1976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001978 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001979 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001980 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981
1982 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001983 FolderIcon newFolder =
1984 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001985 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001986 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001987 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
Romain Guycbb89e42009-06-08 15:52:54 -07001989
Joe Onorato9c1289c2009-08-17 11:03:03 -04001990 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001991 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 }
1993
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001995 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001997 Intent chooser = Intent.createChooser(pickWallpaper,
1998 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001999 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
2000 // Removed in Eclair MR1
2001// WallpaperManager wm = (WallpaperManager)
2002// getSystemService(Context.WALLPAPER_SERVICE);
2003// WallpaperInfo wi = wm.getWallpaperInfo();
2004// if (wi != null && wi.getSettingsActivity() != null) {
2005// LabeledIntent li = new LabeledIntent(getPackageName(),
2006// R.string.configure_wallpaper, 0);
2007// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
2008// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
2009// }
Mike Clerona0618e42009-10-22 13:55:21 -07002010 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 }
2012
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002013 /**
2014 * Registers various content observers. The current implementation registers
2015 * only a favorites observer to keep track of the favorites applications.
2016 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002017 private void registerContentObservers() {
2018 ContentResolver resolver = getContentResolver();
2019 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2020 true, mWidgetObserver);
2021 }
2022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 @Override
2024 public boolean dispatchKeyEvent(KeyEvent event) {
2025 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2026 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002028 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002029 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002030 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002031 dumpState();
2032 return true;
2033 }
2034 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002035 }
2036 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2037 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002038 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 return true;
2040 }
2041 }
2042
2043 return super.dispatchKeyEvent(event);
2044 }
2045
Joe Onorato88ec0992009-11-19 13:16:06 -08002046 @Override
2047 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002048 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002049 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002050 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002051 Folder openFolder = mWorkspace.getOpenFolder();
2052 if (openFolder.isEditingName()) {
2053 openFolder.dismissEditingName();
2054 } else {
2055 closeFolder();
2056 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002057 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002058 mWorkspace.exitWidgetResizeMode();
2059
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002060 // Back button is a no-op here, but give at least some feedback for the button press
2061 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002062 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002063 }
2064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002066 * Re-listen when widgets are reset.
2067 */
2068 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002069 if (mAppWidgetHost != null) {
2070 mAppWidgetHost.startListening();
2071 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002072 }
2073
2074 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 * Launches the intent referred by the clicked shortcut.
2076 *
2077 * @param v The view representing the clicked shortcut.
2078 */
2079 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002080 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2081 // view has detached (it's possible for this to happen if the view is removed mid touch).
2082 if (v.getWindowToken() == null) {
2083 return;
2084 }
2085
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002086 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002087 return;
2088 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002091 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002093 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2094 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002095
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002096 // Check for special shortcuts
2097 if (intent.getComponent() != null) {
2098 final String shortcutClass = intent.getComponent().getClassName();
2099
2100 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2101 showAllApps(true);
2102 return;
2103 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2104 MemoryDumpActivity.startDump(this);
2105 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002106 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2107 toggleShowWeightWatcher();
2108 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002109 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002110 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002111
2112 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002113 int[] pos = new int[2];
2114 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002115 intent.setSourceBounds(new Rect(pos[0], pos[1],
2116 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002117
2118 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002119
Daniel Sandlerff02d492013-08-05 02:12:05 -04002120 mStats.recordLaunch(intent, shortcut);
2121
Michael Jurkaddd62e92011-02-16 17:49:14 -08002122 if (success && v instanceof BubbleTextView) {
2123 mWaitingForResume = (BubbleTextView) v;
2124 mWaitingForResume.setStayPressed(true);
2125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002127 if (v instanceof FolderIcon) {
2128 FolderIcon fi = (FolderIcon) v;
2129 handleFolderClick(fi);
2130 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002131 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002132 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002133 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002134 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002135 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 }
2138 }
2139
Michael Jurka0e260592010-06-30 17:07:39 -07002140 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002141 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002142 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002143 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2144 showWorkspace(true);
2145 }
Michael Jurka0e260592010-06-30 17:07:39 -07002146 return false;
2147 }
2148
Michael Jurkaaf442092010-06-10 17:01:57 -07002149 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002150 * Event handler for the search button
2151 *
2152 * @param v The view that was clicked.
2153 */
2154 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002155 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2156
Amith Yamasania135ba82011-08-09 17:42:01 -07002157 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002158 }
2159
2160 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002161 * Event handler for the voice button
2162 *
2163 * @param v The view that was clicked.
2164 */
2165 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002166 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002167
Bjorn Bringertc459e522013-06-07 19:36:01 +01002168 startVoice();
2169 }
2170
2171 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002172 try {
2173 final SearchManager searchManager =
2174 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2175 ComponentName activityName = searchManager.getGlobalSearchActivity();
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 if (activityName != null) {
2179 intent.setPackage(activityName.getPackageName());
2180 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002181 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002182 } catch (ActivityNotFoundException e) {
2183 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002184 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002185 startActivitySafely(null, intent, "onClickVoiceButton");
2186 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002187 }
2188
2189 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002190 * Event handler for the "grid" button that appears on the home screen, which
2191 * enters all apps mode.
2192 *
2193 * @param v The view that was clicked.
2194 */
2195 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002196 showAllApps(true);
2197 }
2198
2199 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002200 // Provide the same haptic feedback that the system offers for virtual keys.
2201 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002202 }
2203
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002204 public void onClickAppMarketButton(View v) {
2205 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002206 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002207 } else {
2208 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002209 }
2210 }
2211
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002212 void startApplicationDetailsActivity(ComponentName componentName) {
2213 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002214 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2215 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002216 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002217 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002218 }
2219
Michael Jurka1e2f4652013-07-08 18:03:46 -07002220 // returns true if the activity was started
2221 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2222 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002223 // System applications cannot be installed. For now, show a toast explaining that.
2224 // We may give them the option of disabling apps this way.
2225 int messageId = R.string.uninstall_system_app_text;
2226 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002227 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002228 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002229 String packageName = componentName.getPackageName();
2230 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002231 Intent intent = new Intent(
2232 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002233 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2234 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002235 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002236 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002237 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002238 }
2239
Michael Jurka86a720e2012-05-09 11:23:23 -07002240 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002244 // Only launch using the new animation if the shortcut has not opted out (this is a
2245 // private contract between launcher and may be ignored in the future).
2246 boolean useLaunchAnimation = (v != null) &&
2247 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2248 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002249 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2250 v.getMeasuredWidth(), v.getMeasuredHeight());
2251
2252 startActivity(intent, opts.toBundle());
2253 } else {
2254 startActivity(intent);
2255 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002256 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 } catch (SecurityException e) {
2258 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002259 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002261 "or use the exported attribute for this activity. "
2262 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002264 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002266
Michael Jurka86a720e2012-05-09 11:23:23 -07002267 boolean startActivitySafely(View v, Intent intent, Object tag) {
2268 boolean success = false;
2269 try {
2270 success = startActivity(v, intent, tag);
2271 } catch (ActivityNotFoundException e) {
2272 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2273 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2274 }
2275 return success;
2276 }
2277
Romain Guy8e633c52010-03-04 12:51:36 -08002278 void startActivityForResultSafely(Intent intent, int requestCode) {
2279 try {
2280 startActivityForResult(intent, requestCode);
2281 } catch (ActivityNotFoundException e) {
2282 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2283 } catch (SecurityException e) {
2284 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2285 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2286 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2287 "or use the exported attribute for this activity.", e);
2288 }
2289 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290
Adam Cohena9cf38f2011-05-02 15:36:58 -07002291 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002292 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002293 Folder openFolder = mWorkspace.getFolderForTag(info);
2294
2295 // If the folder info reports that the associated folder is open, then verify that
2296 // it is actually opened. There have been a few instances where this gets out of sync.
2297 if (info.opened && openFolder == null) {
2298 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002300 info.opened = false;
2301 }
2302
Adam Cohenfb91f302012-06-11 15:45:18 -07002303 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 // Close any open folder
2305 closeFolder();
2306 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002307 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 } else {
2309 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002310 int folderScreen;
2311 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002312 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 // .. and close it
2314 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002315 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 // Close any folder open on the current screen
2317 closeFolder();
2318 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002319 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 }
2321 }
2322 }
2323 }
2324
Adam Cohen268c4752012-06-06 17:47:33 -07002325 /**
2326 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2327 * in the DragLayer in the exact absolute location of the original FolderIcon.
2328 */
2329 private void copyFolderIconToImage(FolderIcon fi) {
2330 final int width = fi.getMeasuredWidth();
2331 final int height = fi.getMeasuredHeight();
2332
2333 // Lazy load ImageView, Bitmap and Canvas
2334 if (mFolderIconImageView == null) {
2335 mFolderIconImageView = new ImageView(this);
2336 }
2337 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2338 mFolderIconBitmap.getHeight() != height) {
2339 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2340 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2341 }
2342
2343 DragLayer.LayoutParams lp;
2344 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2345 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2346 } else {
2347 lp = new DragLayer.LayoutParams(width, height);
2348 }
2349
Adam Cohen307fe232012-08-16 17:55:58 -07002350 // The layout from which the folder is being opened may be scaled, adjust the starting
2351 // view size by this scale factor.
2352 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002353 lp.customPosition = true;
2354 lp.x = mRectForFolderAnimation.left;
2355 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002356 lp.width = (int) (scale * width);
2357 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002358
2359 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2360 fi.draw(mFolderIconCanvas);
2361 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002362 if (fi.getFolder() != null) {
2363 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2364 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002365 }
Adam Cohen268c4752012-06-06 17:47:33 -07002366 // Just in case this image view is still in the drag layer from a previous animation,
2367 // we remove it and re-add it.
2368 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2369 mDragLayer.removeView(mFolderIconImageView);
2370 }
2371 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002372 if (fi.getFolder() != null) {
2373 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002374 }
Adam Cohen268c4752012-06-06 17:47:33 -07002375 }
2376
Adam Cohen2801caf2011-05-13 20:57:39 -07002377 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002378 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002379 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2380 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2381 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2382
Adam Cohenc51934b2011-07-26 21:07:43 -07002383 FolderInfo info = (FolderInfo) fi.getTag();
2384 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2385 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002386 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2387 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002388 }
2389
Adam Cohen268c4752012-06-06 17:47:33 -07002390 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2391 copyFolderIconToImage(fi);
2392 fi.setVisibility(View.INVISIBLE);
2393
Michael Jurka2ecf9952012-06-18 12:52:28 -07002394 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002395 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002396 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2397 oa.start();
2398 }
2399
Adam Cohen268c4752012-06-06 17:47:33 -07002400 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002401 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002402 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2403 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2404 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2405
Adam Cohen268c4752012-06-06 17:47:33 -07002406 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002407
Adam Cohen268c4752012-06-06 17:47:33 -07002408 // We remove and re-draw the FolderIcon in-case it has changed
2409 mDragLayer.removeView(mFolderIconImageView);
2410 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002411 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002412 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002413 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002414 oa.addListener(new AnimatorListenerAdapter() {
2415 @Override
2416 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002417 if (cl != null) {
2418 cl.clearFolderLeaveBehind();
2419 // Remove the ImageView copy of the FolderIcon and make the original visible.
2420 mDragLayer.removeView(mFolderIconImageView);
2421 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002422 }
2423 }
2424 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002425 oa.start();
2426 }
2427
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002429 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 * is animated relative to the specified View. If the View is null, no animation
2431 * is played.
2432 *
2433 * @param folderInfo The FolderInfo describing the folder to open.
2434 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002435 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002436 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002437 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438
Adam Cohena9cf38f2011-05-02 15:36:58 -07002439 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440
Adam Cohen4554ee12011-08-03 16:13:21 -07002441 // Just verify that the folder hasn't already been added to the DragLayer.
2442 // There was a one-off crash where the folder had a parent already.
2443 if (folder.getParent() == null) {
2444 mDragLayer.addView(folder);
2445 mDragController.addDropTarget((DropTarget) folder);
2446 } else {
2447 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2448 folder.getParent() + ").");
2449 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002450 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002451 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002452
2453 // Notify the accessibility manager that this folder "window" has appeared and occluded
2454 // the workspace items
2455 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2456 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002457 }
2458
2459 public void closeFolder() {
2460 Folder folder = mWorkspace.getOpenFolder();
2461 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002462 if (folder.isEditingName()) {
2463 folder.dismissEditingName();
2464 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002465 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002466
2467 // Dismiss the folder cling
2468 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002469 }
2470 }
2471
2472 void closeFolder(Folder folder) {
2473 folder.getInfo().opened = false;
2474
2475 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2476 if (parent != null) {
2477 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2478 shrinkAndFadeInFolderIcon(fi);
2479 }
2480 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002481
2482 // Notify the accessibility manager that this folder "window" has disappeard and no
2483 // longer occludeds the workspace items
2484 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 }
2486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002488 if (!isDraggingEnabled()) return false;
2489 if (isWorkspaceLocked()) return false;
2490 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491
2492 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002493 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 }
2495
Michael Jurka0280c3b2010-09-17 15:00:07 -07002496 resetAddInfo();
2497 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002499 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 return true;
2501 }
2502
Winson Chung3d503fb2011-07-13 17:25:49 -07002503 // The hotseat touch handling does not go through Workspace, and we always allow long press
2504 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002505 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002506 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2507 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002508 if (itemUnderLongClick == null) {
2509 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002510 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2511 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002512 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002513 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2514 mWorkspace.startReordering();
2515 } else {
2516 startWallpaper();
2517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002519 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002521 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 }
2523 }
2524 }
2525 return true;
2526 }
2527
Winson Chung3d503fb2011-07-13 17:25:49 -07002528 boolean isHotseatLayout(View layout) {
2529 return mHotseat != null && layout != null &&
2530 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2531 }
2532 Hotseat getHotseat() {
2533 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002534 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002535 SearchDropTargetBar getSearchBar() {
2536 return mSearchDropTargetBar;
2537 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002538
Winson Chung3d503fb2011-07-13 17:25:49 -07002539 /**
2540 * Returns the CellLayout of the specified container at the specified screen.
2541 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002542 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002543 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2544 if (mHotseat != null) {
2545 return mHotseat.getLayout();
2546 } else {
2547 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002548 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002549 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002550 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002551 }
2552 }
2553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 Workspace getWorkspace() {
2555 return mWorkspace;
2556 }
2557
Daniel Sandler843e8602010-06-07 14:59:01 -04002558 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002559 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002560 }
2561
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002562 /**
2563 * Helper method for the cameraZoomIn/cameraZoomOut animations
2564 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002565 * @param scaleFactor The scale factor used for the zoom
2566 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002567 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002568 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002569 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002570 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002571
Winson Chung18f41f82012-05-09 13:28:10 -07002572 void disableWallpaperIfInAllApps() {
2573 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002574 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002575 if (mAppsCustomizeTabHost != null &&
2576 !mAppsCustomizeTabHost.isTransitioning()) {
2577 updateWallpaperVisibility(false);
2578 }
2579 }
2580 }
2581
Craig Mautner360310b2012-10-26 15:13:08 -07002582 private void setWorkspaceBackground(boolean workspace) {
2583 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002584 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002585 }
2586
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002587 void updateWallpaperVisibility(boolean visible) {
2588 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2589 int curflags = getWindow().getAttributes().flags
2590 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2591 if (wpflags != curflags) {
2592 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2593 }
Craig Mautner360310b2012-10-26 15:13:08 -07002594 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002595 }
2596
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002597 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2598 if (v instanceof LauncherTransitionable) {
2599 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2600 }
2601 }
2602
Michael Jurkabed61d22012-02-14 22:51:29 -08002603 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2604 if (v instanceof LauncherTransitionable) {
2605 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2606 }
Winson Chung70442722012-02-10 15:43:22 -08002607
2608 // Update the workspace transition step as well
2609 dispatchOnLauncherTransitionStep(v, 0f);
2610 }
2611
2612 private void dispatchOnLauncherTransitionStep(View v, float t) {
2613 if (v instanceof LauncherTransitionable) {
2614 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2615 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002616 }
2617
2618 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2619 if (v instanceof LauncherTransitionable) {
2620 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2621 }
Winson Chung70442722012-02-10 15:43:22 -08002622
2623 // Update the workspace transition step as well
2624 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002625 }
2626
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002627 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002628 * Things to test when changing the following seven functions.
2629 * - Home from workspace
2630 * - from center screen
2631 * - from other screens
2632 * - Home from all apps
2633 * - from center screen
2634 * - from other screens
2635 * - Back from all apps
2636 * - from center screen
2637 * - from other screens
2638 * - Launch app from workspace and quit
2639 * - with back
2640 * - with home
2641 * - Launch app from all apps and quit
2642 * - with back
2643 * - with home
2644 * - Go to a screen that's not the default, then all
2645 * apps, and launch and app, and go back
2646 * - with back
2647 * -with home
2648 * - On workspace, long press power and go back
2649 * - with back
2650 * - with home
2651 * - On all apps, long press power and go back
2652 * - with back
2653 * - with home
2654 * - On workspace, power off
2655 * - On all apps, power off
2656 * - Launch an app and turn off the screen while in that app
2657 * - Go back with home key
2658 * - Go back with back key TODO: make this not go to workspace
2659 * - From all apps
2660 * - From workspace
2661 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2662 * - From all apps
2663 * - From the center workspace
2664 * - From another workspace
2665 */
2666
2667 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002668 * Zoom the camera out from the workspace to reveal 'toView'.
2669 * Assumes that the view to show is anchored at either the very top or very bottom
2670 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002671 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002672 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002673 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002674 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002675 mStateAnimation.cancel();
2676 mStateAnimation = null;
2677 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002678 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002679
Winson Chungf0ea4d32011-06-06 14:27:16 -07002680 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2681 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2682 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002683 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002684 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002685 final int startDelay =
2686 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002687
Michael Jurkab3e22d92011-10-31 15:58:33 -07002688 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002689
Michael Jurkad74c9842011-07-10 12:44:21 -07002690 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002691 Animator workspaceAnim =
2692 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002693
2694 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002695 toView.setScaleX(scale);
2696 toView.setScaleY(scale);
2697 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2698 scaleAnim.
2699 scaleX(1f).scaleY(1f).
2700 setDuration(duration).
2701 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002702
Winson Chungf0ea4d32011-06-06 14:27:16 -07002703 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002704 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002705 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002706 .ofFloat(toView, "alpha", 0f, 1f)
2707 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002708 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002709 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2710 @Override
2711 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002712 if (animation == null) {
2713 throw new RuntimeException("animation is null");
2714 }
Winson Chung70442722012-02-10 15:43:22 -08002715 float t = (Float) animation.getAnimatedValue();
2716 dispatchOnLauncherTransitionStep(fromView, t);
2717 dispatchOnLauncherTransitionStep(toView, t);
2718 }
2719 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002720
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002721 // toView should appear right at the end of the workspace shrink
2722 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002723 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002724 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002725 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002726
2727 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002728 boolean animationCancelled = false;
2729
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002730 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002731 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002732 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002733 // Prepare the position
2734 toView.setTranslationX(0.0f);
2735 toView.setTranslationY(0.0f);
2736 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002737 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002738 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002739 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002740 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002741 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2742 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002743
Daniel Sandlere4f98912013-06-25 15:13:26 -04002744 if (mWorkspace != null
2745 && !springLoaded
2746 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002747 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002748 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002749 if (!animationCancelled) {
2750 updateWallpaperVisibility(false);
2751 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002752
2753 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002754 if (mSearchDropTargetBar != null) {
2755 mSearchDropTargetBar.hideSearchBar(false);
2756 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002757 }
2758
Adam Cohencff6af82011-09-13 14:51:53 -07002759 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002760 public void onAnimationCancel(Animator animation) {
2761 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002762 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002763 });
2764
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002765 if (workspaceAnim != null) {
2766 mStateAnimation.play(workspaceAnim);
2767 }
Michael Jurka1899a362011-11-03 13:50:45 -07002768
2769 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002770
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002771 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2772 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002773
2774 // If any of the objects being animated haven't been measured/laid out
2775 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002776 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002777 (mWorkspace.getMeasuredWidth() == 0) ||
2778 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002779 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002780 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002781
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002782 final AnimatorSet stateAnimation = mStateAnimation;
2783 final Runnable startAnimRunnable = new Runnable() {
2784 public void run() {
2785 // Check that mStateAnimation hasn't changed while
2786 // we waited for a layout/draw pass
2787 if (mStateAnimation != stateAnimation)
2788 return;
2789 setPivotsForZoom(toView, scale);
2790 dispatchOnLauncherTransitionStart(fromView, animated, false);
2791 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002792 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002793 }
2794 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002795 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002796 final ViewTreeObserver observer = toView.getViewTreeObserver();
2797 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2798 public void onGlobalLayout() {
2799 startAnimRunnable.run();
2800 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2801 }
2802 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002803 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002804 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002805 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002806 } else {
2807 toView.setTranslationX(0.0f);
2808 toView.setTranslationY(0.0f);
2809 toView.setScaleX(1.0f);
2810 toView.setScaleY(1.0f);
2811 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002812 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002813
Daniel Sandlere4f98912013-06-25 15:13:26 -04002814 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002815 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002816
2817 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002818 if (mSearchDropTargetBar != null) {
2819 mSearchDropTargetBar.hideSearchBar(false);
2820 }
Michael Jurkaabded662011-03-04 12:06:57 -08002821 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002822 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002823 dispatchOnLauncherTransitionStart(fromView, animated, false);
2824 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002825 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002826 dispatchOnLauncherTransitionStart(toView, animated, false);
2827 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002828 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002830 }
2831
2832 /**
2833 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002834 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002835 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002836 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002837 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2838 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002839
Michael Jurkab3e22d92011-10-31 15:58:33 -07002840 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002841 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002842 mStateAnimation.cancel();
2843 mStateAnimation = null;
2844 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002845 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002846
Winson Chungf0ea4d32011-06-06 14:27:16 -07002847 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002848 final int fadeOutDuration =
2849 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002850 final float scaleFactor = (float)
2851 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2852 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002853 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002854 Animator workspaceAnim = null;
2855
2856 if (toState == State.WORKSPACE) {
2857 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2858 workspaceAnim = mWorkspace.getChangeStateAnimation(
2859 Workspace.State.NORMAL, animated, stagger);
2860 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2861 workspaceAnim = mWorkspace.getChangeStateAnimation(
2862 Workspace.State.SPRING_LOADED, animated);
2863 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002864
Michael Jurkab3e22d92011-10-31 15:58:33 -07002865 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002866 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002867 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002868 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002869 final LauncherViewPropertyAnimator scaleAnim =
2870 new LauncherViewPropertyAnimator(fromView);
2871 scaleAnim.
2872 scaleX(scaleFactor).scaleY(scaleFactor).
2873 setDuration(duration).
2874 setInterpolator(new Workspace.ZoomInInterpolator());
2875
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002876 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002877 .ofFloat(fromView, "alpha", 1f, 0f)
2878 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002879 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002880 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2881 @Override
2882 public void onAnimationUpdate(ValueAnimator animation) {
2883 float t = 1f - (Float) animation.getAnimatedValue();
2884 dispatchOnLauncherTransitionStep(fromView, t);
2885 dispatchOnLauncherTransitionStep(toView, t);
2886 }
2887 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002888
Michael Jurka2ecf9952012-06-18 12:52:28 -07002889 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002890
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002891 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2892 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002893 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002894
2895 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002896 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002897 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002898 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002899 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002900 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2901 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002902 if (onCompleteRunnable != null) {
2903 onCompleteRunnable.run();
2904 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002905 mAppsCustomizeContent.updateCurrentPageScroll();
2906 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002907 }
2908 });
2909
Winson Chungf0ea4d32011-06-06 14:27:16 -07002910 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002911 if (workspaceAnim != null) {
2912 mStateAnimation.play(workspaceAnim);
2913 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002914 dispatchOnLauncherTransitionStart(fromView, animated, true);
2915 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002916 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002917 } else {
2918 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002919 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002920 dispatchOnLauncherTransitionStart(fromView, animated, true);
2921 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002922 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002923 dispatchOnLauncherTransitionStart(toView, animated, true);
2924 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002925 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002926 }
2927
Michael Jurkae326f182011-11-21 14:05:46 -08002928 @Override
2929 public void onTrimMemory(int level) {
2930 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002931 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002932 mAppsCustomizeTabHost.onTrimMemory();
2933 }
2934 }
2935
Winson Chung18f41f82012-05-09 13:28:10 -07002936 @Override
2937 public void onWindowFocusChanged(boolean hasFocus) {
2938 if (!hasFocus) {
2939 // When another window occludes launcher (like the notification shade, or recents),
2940 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2941 updateWallpaperVisibility(true);
2942 } else {
2943 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002944 mWorkspace.postDelayed(new Runnable() {
2945 @Override
2946 public void run() {
2947 disableWallpaperIfInAllApps();
2948 }
2949 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002950 }
2951 }
2952
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002953 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002954 showWorkspace(animated, null);
2955 }
2956
2957 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002958 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002959 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002960 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002961 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002962
Winson Chungc7d2b602012-05-16 17:02:20 -07002963 // Show the search bar (only animate if we were showing the drop target bar in spring
2964 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002965 if (mSearchDropTargetBar != null) {
2966 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2967 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002968
Michael Jurkab737ee62011-11-15 15:57:22 -08002969 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002970 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002971
2972 // Set focus to the AppsCustomize button
2973 if (mAllAppsButton != null) {
2974 mAllAppsButton.requestFocus();
2975 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002976 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002977
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002978 // Change the state *after* we've called all the transition code
2979 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002980
Winson Chung5fb63472011-02-02 17:03:37 -08002981 // Resume the auto-advance of widgets
2982 mUserPresent = true;
2983 updateRunning();
2984
alanv1d4fde62012-10-17 13:15:47 -07002985 // Send an accessibility event to announce the context change
2986 getWindow().getDecorView()
2987 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002988
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002989 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002990 }
2991
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002992 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002993 }
2994
Michael Jurkab3e22d92011-10-31 15:58:33 -07002995 void showAllApps(boolean animated) {
2996 if (mState != State.WORKSPACE) return;
2997
2998 showAppsCustomizeHelper(animated, false);
2999 mAppsCustomizeTabHost.requestFocus();
3000
Michael Jurkab3e22d92011-10-31 15:58:33 -07003001 // Change the state *after* we've called all the transition code
3002 mState = State.APPS_CUSTOMIZE;
3003
3004 // Pause the auto-advance of widgets until we are out of AllApps
3005 mUserPresent = false;
3006 updateRunning();
3007 closeFolder();
3008
3009 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003010 getWindow().getDecorView()
3011 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003012 }
3013
Adam Cohen7777d962011-08-18 18:58:38 -07003014 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003015 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003016 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08003017 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07003018 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003019 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003020 }
Adam Cohen7777d962011-08-18 18:58:38 -07003021
Adam Cohened66b2b2012-01-23 17:28:51 -08003022 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3023 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003024 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3025
Winson Chunge7a03942011-08-05 15:05:12 -07003026 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003027 @Override
3028 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003029 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003030 // Before we show workspace, hide all apps again because
3031 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3032 // clean up our state transition functions
3033 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003034 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003035 } else {
3036 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003037 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003038 }
3039 }, (extendedDelay ?
3040 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3041 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3042 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003043
Michael Jurkad3ef3062010-11-23 16:23:58 -08003044 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003045 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003046 final boolean animated = true;
3047 final boolean springLoaded = true;
3048 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003049 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003050 }
3051 // Otherwise, we are not in spring loaded mode, so don't do anything.
3052 }
3053
Michael Jurkab737ee62011-11-15 15:57:22 -08003054 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003055 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003056 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003057 }
3058 }
3059
3060 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003061 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003062 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003063 if (mDividerAnimator != null) {
3064 mDividerAnimator.cancel();
3065 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003066 mDividerAnimator = null;
3067 }
3068 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003069 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003070 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003071 int duration = 0;
3072 if (mSearchDropTargetBar != null) {
3073 duration = mSearchDropTargetBar.getTransitionInDuration();
3074 }
3075 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003076 mDividerAnimator.start();
3077 }
3078 }
3079 }
3080
Michael Jurkab3e22d92011-10-31 15:58:33 -07003081 void lockAllApps() {
3082 // TODO
3083 }
3084
3085 void unlockAllApps() {
3086 // TODO
3087 }
3088
Winson Chungf0ea4d32011-06-06 14:27:16 -07003089 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003090 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003091 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003092 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003093 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003094 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003095 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003096 int duration = 0;
3097 if (mSearchDropTargetBar != null) {
3098 duration = mSearchDropTargetBar.getTransitionInDuration();
3099 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003100 mHotseat.animate().alpha(1f).setDuration(duration);
3101 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003102 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003103 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003104 }
3105 }
3106 }
3107
3108 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003109 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003110 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003111 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003112 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003113 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003114 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003115 int duration = 0;
3116 if (mSearchDropTargetBar != null) {
3117 duration = mSearchDropTargetBar.getTransitionOutDuration();
3118 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003119 mHotseat.animate().alpha(0f).setDuration(duration);
3120 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003121 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003122 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003123 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003124 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003125 }
3126
Patrick Dubroy5f445422011-02-18 14:35:21 -08003127 /**
3128 * Add an item from all apps or customize onto the given workspace screen.
3129 * If layout is null, add to the current screen.
3130 */
3131 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003132 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003133 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003134 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003135 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003136
Winson Chungdff8ebb2011-09-08 17:25:31 -07003137 /** Maps the current orientation to an index for referencing orientation correct global icons */
3138 private int getCurrentOrientationIndexForGlobalIcons() {
3139 // default - 0, landscape - 1
3140 switch (getResources().getConfiguration().orientation) {
3141 case Configuration.ORIENTATION_LANDSCAPE:
3142 return 1;
3143 default:
3144 return 0;
3145 }
3146 }
3147
Michael Jurkaae65ee32012-04-30 11:45:54 -07003148 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003149 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003150 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003151 // Look for the toolbar icon specified in the activity meta-data
3152 Bundle metaData = packageManager.getActivityInfo(
3153 activityName, PackageManager.GET_META_DATA).metaData;
3154 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003155 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003156 if (iconResId != 0) {
3157 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003158 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003159 }
3160 }
3161 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003162 // This can happen if the activity defines an invalid drawable
3163 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3164 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003165 } catch (Resources.NotFoundException nfe) {
3166 // This can happen if the activity defines an invalid drawable
3167 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3168 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003169 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003170 return null;
3171 }
3172
3173 // if successful in getting icon, return it; otherwise, set button to use default drawable
3174 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003175 int buttonId, ComponentName activityName, int fallbackDrawableId,
3176 String toolbarResourceName) {
3177 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003178 Resources r = getResources();
3179 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3180 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003181
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003182 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003183 // If we were unable to find the icon via the meta-data, use a generic one
3184 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003185 toolbarIcon = r.getDrawable(fallbackDrawableId);
3186 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003187 if (button != null) {
3188 button.setCompoundDrawables(toolbarIcon, null, null, null);
3189 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003190 return null;
3191 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003192 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003193 if (button != null) {
3194 button.setCompoundDrawables(toolbarIcon, null, null, null);
3195 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003196 return toolbarIcon.getConstantState();
3197 }
3198 }
3199
3200 // if successful in getting icon, return it; otherwise, set button to use default drawable
3201 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003202 int buttonId, ComponentName activityName, int fallbackDrawableId,
3203 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003204 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003205 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003206
Michael Jurka19e0fc52011-07-22 18:00:21 -07003207 if (button != null) {
3208 // If we were unable to find the icon via the meta-data, use a
3209 // generic one
3210 if (toolbarIcon == null) {
3211 button.setImageResource(fallbackDrawableId);
3212 } else {
3213 button.setImageDrawable(toolbarIcon);
3214 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003215 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003216
3217 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3218
Michael Jurka0423dcf2010-10-05 14:56:18 -07003219 }
3220
Winson Chung1b884092012-06-08 17:13:02 -07003221 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003222 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003223 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003224 }
3225
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003226 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003227 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003228 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003229 }
3230
Winson Chungbb185bd2011-11-21 12:31:42 -08003231 private void invalidatePressedFocusedStates(View container, View button) {
3232 if (container instanceof HolographicLinearLayout) {
3233 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3234 layout.invalidatePressedFocusedStates();
3235 } else if (button instanceof HolographicImageView) {
3236 HolographicImageView view = (HolographicImageView) button;
3237 view.invalidatePressedFocusedStates();
3238 }
3239 }
3240
Winson Chungc51db6a2011-10-05 11:44:49 -07003241 private boolean updateGlobalSearchIcon() {
3242 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003243 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003244 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003245 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003246
Winson Chung1cad91e2011-05-25 17:41:01 -07003247 final SearchManager searchManager =
3248 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3249 ComponentName activityName = searchManager.getGlobalSearchActivity();
3250 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003251 int coi = getCurrentOrientationIndexForGlobalIcons();
3252 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003253 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3254 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3255 if (sGlobalSearchIcon[coi] == null) {
3256 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3257 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3258 TOOLBAR_ICON_METADATA_NAME);
3259 }
3260
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3262 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003263 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003264 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003265 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003266 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003267 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3268 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3269 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003270 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003271 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003272 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003273 }
3274 }
3275
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003276 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003277 final View searchButtonContainer = findViewById(R.id.search_button_container);
3278 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003279 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003280 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003281 }
3282
Winson Chungc51db6a2011-10-05 11:44:49 -07003283 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003284 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003285 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003286
Winson Chungc51db6a2011-10-05 11:44:49 -07003287 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003288 final SearchManager searchManager =
3289 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3290 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3291
3292 ComponentName activityName = null;
3293 if (globalSearchActivity != null) {
3294 // Check if the global search activity handles voice search
3295 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3296 intent.setPackage(globalSearchActivity.getPackageName());
3297 activityName = intent.resolveActivity(getPackageManager());
3298 }
3299
3300 if (activityName == null) {
3301 // Fallback: check if an activity other than the global search activity
3302 // resolves this
3303 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3304 activityName = intent.resolveActivity(getPackageManager());
3305 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003306 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003307 int coi = getCurrentOrientationIndexForGlobalIcons();
3308 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003309 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3310 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3311 if (sVoiceSearchIcon[coi] == null) {
3312 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3313 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3314 TOOLBAR_ICON_METADATA_NAME);
3315 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003316 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003317 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003318 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003319 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003320 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003321 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003322 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003323 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003324 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003325 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003326 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003327 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003328
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003329 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003330 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3331 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003332 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003333 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003334 }
3335
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003336 public void setVoiceButtonProxyVisible(boolean visible) {
3337 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3338 if (voiceButtonProxy != null) {
3339 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3340 }
3341 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003342 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003343 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003344 */
3345 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003346 final View marketButton = findViewById(R.id.market_button);
3347 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3348 // Find the app market activity by resolving an intent.
3349 // (If multiple app markets are installed, it will return the ResolverActivity.)
3350 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003351 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003352 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003353 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003354 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003355 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3356 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003357 marketButton.setVisibility(View.VISIBLE);
3358 } else {
3359 // We should hide and disable the view so that we don't try and restore the visibility
3360 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3361 marketButton.setVisibility(View.GONE);
3362 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003363 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003364 }
3365
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003366 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003367 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3368 Resources r = getResources();
3369 Drawable marketIconDrawable = d.newDrawable(r);
3370 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3371 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3372 marketIconDrawable.setBounds(0, 0, w, h);
3373
3374 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003375 }
3376
Michael Jurkad7c28052012-04-27 15:43:36 -07003377 @Override
3378 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003379 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003380 final List<CharSequence> text = event.getText();
3381 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003382 // Populate event with a fake title based on the current state.
3383 if (mState == State.APPS_CUSTOMIZE) {
3384 text.add(getString(R.string.all_apps_button_label));
3385 } else {
3386 text.add(getString(R.string.all_apps_home_button_label));
3387 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003388 return result;
3389 }
3390
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003391 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003392 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003393 */
3394 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3395 @Override
3396 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003397 closeSystemDialogs();
3398 }
3399 }
3400
3401 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003402 * Receives notifications whenever the appwidgets are reset.
3403 */
3404 private class AppWidgetResetObserver extends ContentObserver {
3405 public AppWidgetResetObserver() {
3406 super(new Handler());
3407 }
3408
3409 @Override
3410 public void onChange(boolean selfChange) {
3411 onAppWidgetReset();
3412 }
3413 }
3414
3415 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003416 * If the activity is currently paused, signal that we need to run the passed Runnable
3417 * in onResume.
3418 *
3419 * This needs to be called from incoming places where resources might have been loaded
3420 * while we are paused. That is becaues the Configuration might be wrong
3421 * when we're not running, and if it comes back to what it was when we
3422 * were paused, we are not restarted.
3423 *
3424 * Implementation of the method from LauncherModel.Callbacks.
3425 *
3426 * @return true if we are currently paused. The caller might be able to
3427 * skip some work in that case since we will come back again.
3428 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003429 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003430 if (mPaused) {
3431 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003432 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003433 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003434 }
3435 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003436 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003437 return true;
3438 } else {
3439 return false;
3440 }
3441 }
3442
Michael Jurkac402cd92013-05-20 15:49:32 +02003443 private boolean waitUntilResume(Runnable run) {
3444 return waitUntilResume(run, false);
3445 }
3446
Michael Jurka1e2f4652013-07-08 18:03:46 -07003447 public void addOnResumeCallback(Runnable run) {
3448 mBindOnResumeCallbacks.add(run);
3449 }
3450
3451 public void removeOnResumeCallback(Runnable run) {
3452 mBindOnResumeCallbacks.remove(run);
3453 }
3454
Michael Jurka7607c2f2013-04-03 14:33:19 -07003455 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003456 * If the activity is currently paused, signal that we need to re-run the loader
3457 * in onResume.
3458 *
3459 * This needs to be called from incoming places where resources might have been loaded
3460 * while we are paused. That is becaues the Configuration might be wrong
3461 * when we're not running, and if it comes back to what it was when we
3462 * were paused, we are not restarted.
3463 *
3464 * Implementation of the method from LauncherModel.Callbacks.
3465 *
3466 * @return true if we are currently paused. The caller might be able to
3467 * skip some work in that case since we will come back again.
3468 */
3469 public boolean setLoadOnResume() {
3470 if (mPaused) {
3471 Log.i(TAG, "setLoadOnResume");
3472 mOnResumeNeedsLoad = true;
3473 return true;
3474 } else {
3475 return false;
3476 }
3477 }
3478
3479 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003481 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003482 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003483 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003484 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003485 } else {
3486 return SCREEN_COUNT / 2;
3487 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003489
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 /**
3491 * Refreshes the shortcuts shown on the workspace.
3492 *
3493 * Implementation of the method from LauncherModel.Callbacks.
3494 */
3495 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003496 // If we're starting binding all over again, clear any bind calls we'd postponed in
3497 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3498 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003499 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003500
Michael Jurka7607c2f2013-04-03 14:33:19 -07003501 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003502 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003503 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003504 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003505 int count = workspace.getChildCount();
3506 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003507 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003508 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3509 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003511 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003512 if (mHotseat != null) {
3513 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003514 }
3515 }
3516
Adam Cohendcd297f2013-06-18 13:13:40 -07003517 @Override
3518 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003519 bindAddScreens(orderedScreenIds);
3520 mWorkspace.addExtraEmptyScreen();
3521 }
3522
3523 @Override
3524 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003525 int count = orderedScreenIds.size();
3526 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003527 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003528 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003529 }
3530
Adam Cohen39a06042013-07-19 14:30:12 -07003531 private boolean shouldShowWeightWatcher() {
3532 String spKey = LauncherAppState.getSharedPreferencesKey();
3533 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3534 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3535
3536 return show;
3537 }
3538
3539 private void toggleShowWeightWatcher() {
3540 String spKey = LauncherAppState.getSharedPreferencesKey();
3541 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3542 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3543
3544 show = !show;
3545
3546 SharedPreferences.Editor editor = sp.edit();
3547 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3548 editor.commit();
3549
3550 if (mWeightWatcher != null) {
3551 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3552 }
3553 }
3554
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003555 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 * Bind the items start-end from the list.
3557 *
3558 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003559 */
Winson Chung64359a52013-07-08 17:17:08 -07003560 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3561 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003562 if (waitUntilResume(new Runnable() {
3563 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003564 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003565 }
3566 })) {
3567 return;
3568 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003569
Winson Chungf0c6ae02012-03-21 16:10:31 -07003570 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3571 Set<String> newApps = new HashSet<String>();
3572 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3573
Winson Chung64359a52013-07-08 17:17:08 -07003574 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3575 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003576 Workspace workspace = mWorkspace;
3577 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003578 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003579
3580 // Short circuit if we are loading dock items for a configuration which has no dock
3581 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3582 mHotseat == null) {
3583 continue;
3584 }
3585
Joe Onorato9c1289c2009-08-17 11:03:03 -04003586 switch (item.itemType) {
3587 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3588 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003589 ShortcutInfo info = (ShortcutInfo) item;
3590 String uri = info.intent.toUri(0).toString();
3591 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003592
Winson Chung64359a52013-07-08 17:17:08 -07003593 /*
3594 * TODO: FIX collision case
3595 */
Winson Chungce376632013-07-11 16:12:41 -07003596 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3597 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3598 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3599 throw new RuntimeException("OCCUPIED");
3600 }
Winson Chung64359a52013-07-08 17:17:08 -07003601 }
3602
Adam Cohendcd297f2013-06-18 13:13:40 -07003603 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3604 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003605 boolean animateIconUp = false;
3606 synchronized (newApps) {
3607 if (newApps.contains(uri)) {
3608 animateIconUp = newApps.remove(uri);
3609 }
3610 }
Winson Chung64359a52013-07-08 17:17:08 -07003611 if (forceAnimateIcons) {
3612 // Animate all the applications up now
3613 shortcut.setAlpha(0f);
3614 shortcut.setScaleX(0f);
3615 shortcut.setScaleY(0f);
3616 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3617 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003618 // Prepare the view to be animated up
3619 shortcut.setAlpha(0f);
3620 shortcut.setScaleX(0f);
3621 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003622 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003623 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3624 mNewShortcutAnimateViews.add(shortcut);
3625 }
3626 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003627 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003628 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003629 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003630 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003631 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003632 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3633 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003634 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003635 default:
3636 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003638 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003639
Winson Chung64359a52013-07-08 17:17:08 -07003640 if (forceAnimateIcons) {
3641 // We post the animation slightly delayed to prevent slowdowns when we are loading
3642 // right after we return to launcher.
3643 mWorkspace.postDelayed(new Runnable() {
3644 public void run() {
3645 anim.playTogether(bounceAnims);
3646 anim.start();
3647 }
3648 }, NEW_APPS_ANIMATION_DELAY);
3649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003651 }
3652
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 /**
3654 * Implementation of the method from LauncherModel.Callbacks.
3655 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003656 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3657 if (waitUntilResume(new Runnable() {
3658 public void run() {
3659 bindFolders(folders);
3660 }
3661 })) {
3662 return;
3663 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003664 sFolders.clear();
3665 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003667
3668 /**
3669 * Add the views for a widget to the workspace.
3670 *
3671 * Implementation of the method from LauncherModel.Callbacks.
3672 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003673 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3674 if (waitUntilResume(new Runnable() {
3675 public void run() {
3676 bindAppWidget(item);
3677 }
3678 })) {
3679 return;
3680 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003681
Daniel Sandler843e8602010-06-07 14:59:01 -04003682 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3683 if (DEBUG_WIDGETS) {
3684 Log.d(TAG, "bindAppWidget: " + item);
3685 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 final Workspace workspace = mWorkspace;
3687
3688 final int appWidgetId = item.appWidgetId;
3689 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003690 if (DEBUG_WIDGETS) {
3691 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3692 }
3693
Joe Onorato9c1289c2009-08-17 11:03:03 -04003694 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3695
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003697 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698
Adam Cohendcd297f2013-06-18 13:13:40 -07003699 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003701 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3702
Joe Onorato9c1289c2009-08-17 11:03:03 -04003703 workspace.requestLayout();
3704
Daniel Sandler843e8602010-06-07 14:59:01 -04003705 if (DEBUG_WIDGETS) {
3706 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3707 + (SystemClock.uptimeMillis()-start) + "ms");
3708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003709 }
3710
Adam Cohen1462de32012-07-24 22:34:36 -07003711 public void onPageBoundSynchronously(int page) {
3712 mSynchronouslyBoundPages.add(page);
3713 }
3714
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 /**
3716 * Callback saying that there aren't any more items to bind.
3717 *
3718 * Implementation of the method from LauncherModel.Callbacks.
3719 */
Adam Cohene25af792013-06-06 23:08:25 -07003720 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003721 if (waitUntilResume(new Runnable() {
3722 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003723 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003724 }
3725 })) {
3726 return;
3727 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 if (mSavedState != null) {
3729 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003730 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 mSavedState = null;
3733 }
3734
Winson Chung98ca0f72013-07-29 12:58:51 -07003735 // Create the custom content page here before onLayout to prevent flashing
3736 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3737 mWorkspace.createCustomContentPage();
3738 }
3739
Adam Cohen1462de32012-07-24 22:34:36 -07003740 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003742 // If we received the result of any pending adds while the loader was running (e.g. the
3743 // widget configuration forced an orientation change), process them now.
3744 for (int i = 0; i < sPendingAddList.size(); i++) {
3745 completeAdd(sPendingAddList.get(i));
3746 }
3747 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003748
Winson Chungc51db6a2011-10-05 11:44:49 -07003749 // Update the market app icon as necessary (the other icons will be managed in response to
3750 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003751 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003752
Winson Chungf0c6ae02012-03-21 16:10:31 -07003753 // Animate up any icons as necessary
3754 if (mVisible || mWorkspaceLoading) {
3755 Runnable newAppsRunnable = new Runnable() {
3756 @Override
3757 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003758 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003759 }
3760 };
Winson Chunga2413752012-04-03 14:22:34 -07003761
Adam Cohendcd297f2013-06-18 13:13:40 -07003762 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3763 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003764 if (canRunNewAppsAnimation()) {
3765 // If the user has not interacted recently, then either snap to the new page to show
3766 // the new-apps animation or just run them if they are to appear on the current page
3767 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003768 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003769 } else {
3770 runNewAppsAnimation(false);
3771 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003772 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003773 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003774 // are on another page (or just normally if they are added to the current page)
3775 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003776 }
3777 }
3778
3779 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003780 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003781 mWorkspace.stripDuplicateApps();
3782 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003783 }
Adam Cohen99894d92013-06-14 11:22:59 -07003784
Adam Cohen66a01fd2013-06-11 12:48:00 -07003785 mWorkspace.post(new Runnable() {
3786 @Override
3787 public void run() {
3788 onFinishBindingItems();
3789 }
3790 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003791 }
3792
Winson Chunga2413752012-04-03 14:22:34 -07003793 private boolean canRunNewAppsAnimation() {
3794 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3795 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3796 }
3797
Winson Chungc9168342013-06-26 14:54:55 -07003798 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3799 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3800 PropertyValuesHolder.ofFloat("alpha", 1f),
3801 PropertyValuesHolder.ofFloat("scaleX", 1f),
3802 PropertyValuesHolder.ofFloat("scaleY", 1f));
3803 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3804 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3805 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3806 return bounceAnim;
3807 }
3808
Winson Chungf0c6ae02012-03-21 16:10:31 -07003809 /**
3810 * Runs a new animation that scales up icons that were added while Launcher was in the
3811 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003812 *
3813 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003814 */
Winson Chunga2413752012-04-03 14:22:34 -07003815 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003816 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003817 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003818
3819 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003820 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3821 @Override
3822 public int compare(View a, View b) {
3823 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3824 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3825 int cellCountX = LauncherModel.getCellCountX();
3826 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3827 }
3828 });
Winson Chunga2413752012-04-03 14:22:34 -07003829
3830 // Animate each of the views in place (or show them immediately if requested)
3831 if (immediate) {
3832 for (View v : mNewShortcutAnimateViews) {
3833 v.setAlpha(1f);
3834 v.setScaleX(1f);
3835 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003836 }
Winson Chunga2413752012-04-03 14:22:34 -07003837 } else {
3838 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3839 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003840 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003841 }
3842 anim.playTogether(bounceAnims);
3843 anim.addListener(new AnimatorListenerAdapter() {
3844 @Override
3845 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003846 if (mWorkspace != null) {
3847 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3848 }
Winson Chunga2413752012-04-03 14:22:34 -07003849 }
3850 });
3851 anim.start();
3852 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003853
3854 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003855 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003856 mNewShortcutAnimateViews.clear();
3857 new Thread("clearNewAppsThread") {
3858 public void run() {
3859 mSharedPrefs.edit()
3860 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3861 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3862 .commit();
3863 }
3864 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003865 }
3866
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003867 @Override
3868 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003869 boolean searchVisible = updateGlobalSearchIcon();
3870 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003871 if (mSearchDropTargetBar != null) {
3872 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3873 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003874 }
3875
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003876 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003877 * Add the icons for all apps.
3878 *
3879 * Implementation of the method from LauncherModel.Callbacks.
3880 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003881 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003882 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3883 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003884 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003885 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003886 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 }
3888
3889 /**
3890 * A package was updated.
3891 *
3892 * Implementation of the method from LauncherModel.Callbacks.
3893 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003894 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3895 if (waitUntilResume(new Runnable() {
3896 public void run() {
3897 bindAppsUpdated(apps);
3898 }
3899 })) {
3900 return;
3901 }
3902
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003903 if (mWorkspace != null) {
3904 mWorkspace.updateShortcuts(apps);
3905 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003906 }
3907
3908 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003909 * A package was uninstalled. We take both the super set of packageNames
3910 * in addition to specific applications to remove, the reason being that
3911 * this can be called when a package is updated as well. In that scenario,
3912 * we only remove specific components from the workspace, where as
3913 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003914 *
3915 * Implementation of the method from LauncherModel.Callbacks.
3916 */
Winson Chung83892cc2013-05-01 16:53:33 -07003917 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3918 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003919 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003920 if (waitUntilResume(new Runnable() {
3921 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003922 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003923 }
3924 })) {
3925 return;
3926 }
3927
Winson Chung64359a52013-07-08 17:17:08 -07003928 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003929 mWorkspace.removeItemsByPackageName(packageNames);
3930 } else {
3931 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003932 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003933
Winson Chunga1820962011-10-03 16:31:06 -07003934 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003935 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003936 }
Joe Onoratobe386092009-11-17 17:32:16 -08003937
3938 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003939 * A number of packages were updated.
3940 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003941 private ArrayList<Object> mWidgetsAndShortcuts;
3942 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003943 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003944 bindPackagesUpdated(mWidgetsAndShortcuts);
3945 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003946 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003947 };
3948
3949 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3950 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3951 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003952 return;
3953 }
3954
Winson Chung64359a52013-07-08 17:17:08 -07003955 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003956 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003957 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003958 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003959 }
3960
Winson Chung400438b2011-01-16 17:53:48 -08003961 private int mapConfigurationOriActivityInfoOri(int configOri) {
3962 final Display d = getWindowManager().getDefaultDisplay();
3963 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3964 switch (d.getRotation()) {
3965 case Surface.ROTATION_0:
3966 case Surface.ROTATION_180:
3967 // We are currently in the same basic orientation as the natural orientation
3968 naturalOri = configOri;
3969 break;
3970 case Surface.ROTATION_90:
3971 case Surface.ROTATION_270:
3972 // We are currently in the other basic orientation to the natural orientation
3973 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3974 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3975 break;
3976 }
3977
3978 int[] oriMap = {
3979 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3980 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3981 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3982 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3983 };
3984 // Since the map starts at portrait, we need to offset if this device's natural orientation
3985 // is landscape.
3986 int indexOffset = 0;
3987 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3988 indexOffset = 1;
3989 }
3990 return oriMap[(d.getRotation() + indexOffset) % 4];
3991 }
Adam Cohen446e9402011-09-15 18:21:21 -07003992
Winson Chung4b919f82012-05-01 10:44:08 -07003993 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003994 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003995 getResources().getBoolean(R.bool.allow_rotation);
3996 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003997 }
Winson Chung4b919f82012-05-01 10:44:08 -07003998 public void lockScreenOrientation() {
3999 if (isRotationEnabled()) {
4000 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4001 .getConfiguration().orientation));
4002 }
4003 }
4004 public void unlockScreenOrientation(boolean immediate) {
4005 if (isRotationEnabled()) {
4006 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004007 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004008 } else {
4009 mHandler.postDelayed(new Runnable() {
4010 public void run() {
4011 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4012 }
4013 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004014 }
Winson Chung4b919f82012-05-01 10:44:08 -07004015 }
Winson Chung400438b2011-01-16 17:53:48 -08004016 }
4017
Winson Chung82f55532011-08-09 14:14:23 -07004018 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004019 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004020 if (DISABLE_CLINGS) {
4021 return false;
4022 }
4023
Brett Chabot2a9e2282011-08-23 15:03:13 -07004024 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004025 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004026
Michael Jurkae233a8b2013-03-19 13:49:20 +01004027 // Restricted secondary users (child mode) will potentially have very few apps
4028 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004029// boolean supportsLimitedUsers =
4030// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4031// Account[] accounts = AccountManager.get(this).getAccounts();
4032// if (supportsLimitedUsers && accounts.length == 0) {
4033// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4034// Bundle restrictions = um.getUserRestrictions();
4035// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4036// return false;
4037// }
4038// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004039 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004040 }
Michael Jurka22143132012-10-04 17:42:38 +02004041
Winson Chung7d7541e2011-09-16 20:14:36 -07004042 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004043 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004044 if (cling != null) {
4045 cling.init(this, positionData);
4046 cling.setVisibility(View.VISIBLE);
4047 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4048 if (animate) {
4049 cling.buildLayer();
4050 cling.setAlpha(0f);
4051 cling.animate()
4052 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004053 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004054 .setDuration(SHOW_CLING_DURATION)
4055 .setStartDelay(delay)
4056 .start();
4057 } else {
4058 cling.setAlpha(1f);
4059 }
Michael Jurka22143132012-10-04 17:42:38 +02004060 cling.setFocusableInTouchMode(true);
4061 cling.post(new Runnable() {
4062 public void run() {
4063 cling.setFocusable(true);
4064 cling.requestFocus();
4065 }
4066 });
4067 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4068 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004069 }
4070 return cling;
4071 }
Michael Jurka22143132012-10-04 17:42:38 +02004072
Winson Chung7d7541e2011-09-16 20:14:36 -07004073 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004074 // To catch cases where siblings of top-level views are made invisible, just check whether
4075 // the cling is directly set to GONE before dismissing it.
4076 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004077 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004078 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004079 anim.addListener(new AnimatorListenerAdapter() {
4080 public void onAnimationEnd(Animator animation) {
4081 cling.setVisibility(View.GONE);
4082 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004083 // We should update the shared preferences on a background thread
4084 new Thread("dismissClingThread") {
4085 public void run() {
4086 SharedPreferences.Editor editor = mSharedPrefs.edit();
4087 editor.putBoolean(flag, true);
4088 editor.commit();
4089 }
4090 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004091 };
4092 });
4093 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004094 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004095 }
4096 }
Michael Jurka22143132012-10-04 17:42:38 +02004097
Winson Chung9d9d74f2011-09-19 11:49:12 -07004098 private void removeCling(int id) {
4099 final View cling = findViewById(id);
4100 if (cling != null) {
4101 final ViewGroup parent = (ViewGroup) cling.getParent();
4102 parent.post(new Runnable() {
4103 @Override
4104 public void run() {
4105 parent.removeView(cling);
4106 }
4107 });
Michael Jurka22143132012-10-04 17:42:38 +02004108 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004109 }
4110 }
Michael Jurka974c3862012-05-22 22:00:31 -07004111
4112 private boolean skipCustomClingIfNoAccounts() {
4113 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4114 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4115 if (customCling) {
4116 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004117 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004118 Account[] accounts = am.getAccountsByType("com.google");
4119 return accounts.length == 0;
4120 }
4121 return false;
4122 }
4123
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004125 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004126 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004127 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4128 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004129 // If we're not using the default workspace layout, replace workspace cling
4130 // with a custom workspace cling (usually specified in an overlay)
4131 // For now, only do this on tablets
4132 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004133 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004134 // Use a custom cling
4135 View cling = findViewById(R.id.workspace_cling);
4136 ViewGroup clingParent = (ViewGroup) cling.getParent();
4137 int clingIndex = clingParent.indexOfChild(cling);
4138 clingParent.removeViewAt(clingIndex);
4139 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4140 clingParent.addView(customCling, clingIndex);
4141 customCling.setId(R.id.workspace_cling);
4142 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004143 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004144 } else {
4145 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004146 }
4147 }
4148 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004149 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004150 if (isClingsEnabled() &&
4151 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004152 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004153 } else {
4154 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004155 }
4156 }
4157 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004158 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004159 if (isClingsEnabled() &&
4160 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4161 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004162 } else {
4163 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004164 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004165 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004166 }
4167 public boolean isFolderClingVisible() {
4168 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004169 if (cling != null) {
4170 return cling.getVisibility() == View.VISIBLE;
4171 }
4172 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004173 }
Winson Chung82f55532011-08-09 14:14:23 -07004174 public void dismissWorkspaceCling(View v) {
4175 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004176 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004177 }
4178 public void dismissAllAppsCling(View v) {
4179 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004180 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4181 }
4182 public void dismissFolderCling(View v) {
4183 Cling cling = (Cling) findViewById(R.id.folder_cling);
4184 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004185 }
4186
Winson Chung80baf5a2010-08-09 16:03:15 -07004187 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004188 * Prints out out state for debugging.
4189 */
4190 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004191 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004192 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004193 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4194 Log.d(TAG, "mRestoring=" + mRestoring);
4195 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4196 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004197 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004198 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004199
Winson Chung785d2eb2011-04-14 16:08:02 -07004200 if (mAppsCustomizeContent != null) {
4201 mAppsCustomizeContent.dumpState();
4202 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004203 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004204 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004205
4206 @Override
4207 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4208 super.dump(prefix, fd, writer, args);
4209 writer.println(" ");
4210 writer.println("Debug logs: ");
4211 for (int i = 0; i < sDumpLogs.size(); i++) {
4212 writer.println(" " + sDumpLogs.get(i));
4213 }
4214 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004215
4216 public static void dumpDebugLogsToConsole() {
4217 Log.d(TAG, "");
4218 Log.d(TAG, "*********************");
4219 Log.d(TAG, "Launcher debug logs: ");
4220 for (int i = 0; i < sDumpLogs.size(); i++) {
4221 Log.d(TAG, " " + sDumpLogs.get(i));
4222 }
4223 Log.d(TAG, "*********************");
4224 Log.d(TAG, "");
4225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004226}
Michael Jurka2763be32011-02-24 11:19:57 -08004227
Michael Jurkaabded662011-03-04 12:06:57 -08004228interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004229 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004230 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004231 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004232 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004233 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004234}