blob: 65845b41e1285b2aa37f18d827216283068ceccf [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Adam Cohen96d30a12013-07-16 18:13:21 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
79import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070089import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Daniel Sandler325dc232013-06-05 22:57:57 -040098import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100118public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700120 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Daniel Sandlerf061f822013-06-27 13:59:36 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700126 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
Winson Chung70d72102011-08-12 11:24:35 -0700129 private static final int MENU_GROUP_WALLPAPER = 1;
130 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
131 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700132 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
133 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chung2672ff92012-05-04 16:22:30 -0700161 // The Intent extra that defines whether to ignore the launch animation
162 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400163 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
166 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700167 // Type: int
168 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
171 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: boolean
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
179 // Type: long
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
185 // Type: parcelable
186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100188 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700189 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700191 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100192 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700193
Adam Cohen39a06042013-07-19 14:30:12 -0700194 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
195
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700196 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700197 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700198 private State mState = State.WORKSPACE;
199 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800200 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700203 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
204 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700205 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700206 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800209 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
212 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700213 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700214
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800215 private final BroadcastReceiver mCloseSystemDialogsReceiver
216 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800217 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private LayoutInflater mInflater;
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800222 private View mQsbDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700223 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private DragLayer mDragLayer;
225 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700226 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700228 private AppWidgetManager mAppWidgetManager;
229 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Michael Jurkac9d95c52011-08-29 14:03:34 -0700231 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700232 private AppWidgetProviderInfo mPendingAddWidgetInfo;
233
Michael Jurka0280c3b2010-09-17 15:00:07 -0700234 private int[] mTmpAddItemCellCoordinates = new int[2];
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private FolderInfo mFolderInfo;
237
Winson Chung3d503fb2011-07-13 17:25:49 -0700238 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800239 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700240
Winson Chungc51db6a2011-10-05 11:44:49 -0700241 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700242 private AppsCustomizeTabHost mAppsCustomizeTabHost;
243 private AppsCustomizePagedView mAppsCustomizeContent;
244 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
248 // scroll issues (because the workspace may not have been measured yet) and extra work.
249 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
250 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
252 private SpannableStringBuilder mDefaultKeySsb = null;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private boolean mWorkspaceLoading = true;
255
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800256 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private boolean mRestoring;
258 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700259 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurka1e2f4652013-07-08 18:03:46 -0700261 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
263
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // Keep track of whether the user has left launcher
265 private static boolean sPausedFromUserAction = false;
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedInstanceState;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800271 private boolean mUserPresent = true;
272 private boolean mVisible = false;
273 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700274 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700276 private static LocaleConfiguration sLocaleConfiguration = null;
277
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700278 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700279
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700280 private Intent mAppMarketIntent = null;
281
Adam Cohended9f8d2010-11-03 13:25:16 -0700282 // Related to the auto-advancing of widgets
283 private final int ADVANCE_MSG = 1;
284 private final int mAdvanceInterval = 20000;
285 private final int mAdvanceStagger = 250;
286 private long mAutoAdvanceSentTime;
287 private long mAutoAdvanceTimeLeft = -1;
288 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
289 new HashMap<View, AppWidgetProviderInfo>();
290
Winson Chung400438b2011-01-16 17:53:48 -0800291 // Determines how long to wait after a rotation before restoring the screen orientation to
292 // match the sensor state.
293 private final int mRestoreScreenOrientationDelay = 500;
294
Michael Jurka4ef207b2010-11-29 17:05:45 -0800295 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700296 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
297 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
298 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800299
Craig Mautner360310b2012-10-26 15:13:08 -0700300 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700301
Adam Cohen1462de32012-07-24 22:34:36 -0700302 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
303
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700304 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
305
Winson Chung46353de2012-02-16 14:05:10 -0800306 // We only want to get the SharedPreferences once since it does an FS stat each time we get
307 // it from the context.
308 private SharedPreferences mSharedPrefs;
309
Adam Cohene25af792013-06-06 23:08:25 -0700310 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
311
Winson Chungf0c6ae02012-03-21 16:10:31 -0700312 // Holds the page that we need to animate to, and the icon views that we need to animate up
313 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700314 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700315 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700316 private ImageView mFolderIconImageView;
317 private Bitmap mFolderIconBitmap;
318 private Canvas mFolderIconCanvas;
319 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700320
Michael Jurkaddd62e92011-02-16 17:49:14 -0800321 private BubbleTextView mWaitingForResume;
322
Michael Jurka22143132012-10-04 17:42:38 +0200323 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
324 = new HideFromAccessibilityHelper();
325
Michael Jurkac1f5d262011-09-30 19:32:27 -0700326 private Runnable mBuildLayersRunnable = new Runnable() {
327 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700328 if (mWorkspace != null) {
329 mWorkspace.buildPageHardwareLayers();
330 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700331 }
332 };
333
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800334 private static ArrayList<PendingAddArguments> sPendingAddList
335 = new ArrayList<PendingAddArguments>();
336
Michael Jurka0a457bf2012-11-19 14:05:05 -0800337 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
338
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800339 private static class PendingAddArguments {
340 int requestCode;
341 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700342 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700343 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800344 int cellX;
345 int cellY;
346 }
347
Michael Jurka0a457bf2012-11-19 14:05:05 -0800348 private static boolean isPropertyEnabled(String propertyName) {
349 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700350 }
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 @Override
353 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700354 if (DEBUG_STRICT_MODE) {
355 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
356 .detectDiskReads()
357 .detectDiskWrites()
358 .detectNetwork() // or .detectAll() for all detectable problems
359 .penaltyLog()
360 .build());
361 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
362 .detectLeakedSqlLiteObjects()
363 .detectLeakedClosableObjects()
364 .penaltyLog()
365 .penaltyDeath()
366 .build());
367 }
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400370
371 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
372 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400373 LauncherAppState app = LauncherAppState.getInstance();
374 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700375 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800376 mModel = app.setLauncher(this);
377 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400378 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700380
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
383 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700384
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700385 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
386 // this also ensures that any synchronous binding below doesn't re-trigger another
387 // LauncherModel load.
388 mPaused = false;
389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200391 android.os.Debug.startMethodTracing(
392 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
394
395 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 setContentView(R.layout.launcher);
397 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700398 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800400 registerContentObservers();
401
Joe Onorato7c312c12009-08-13 21:36:53 -0700402 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 mSavedState = savedInstanceState;
405 restoreState(mSavedState);
406
Winson Chunga12a2502010-12-20 14:41:35 -0800407 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700408 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200409 mAppsCustomizeContent.onPackagesUpdated(
410 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700411 }
Winson Chunga12a2502010-12-20 14:41:35 -0800412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 if (PROFILE_STARTUP) {
414 android.os.Debug.stopMethodTracing();
415 }
416
417 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700418 if (sPausedFromUserAction) {
419 // If the user leaves launcher, then we should just load items asynchronously when
420 // they return.
421 mModel.startLoader(true, -1);
422 } else {
423 // We only load the page synchronously if the user rotates (or triggers a
424 // configuration change) while launcher is in the foreground
425 mModel.startLoader(true, mWorkspace.getCurrentPage());
426 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 }
428
429 // For handling default keys
430 mDefaultKeySsb = new SpannableStringBuilder();
431 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800432
433 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
434 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800435
Adam Cohenf9426d52012-06-04 17:26:21 -0700436 updateGlobalIcons();
437
438 // On large interfaces, we want the screen to auto-rotate based on the current orientation
439 unlockScreenOrientation(true);
440 }
441
Winson Chung4a2afa32012-07-19 14:53:05 -0700442 protected void onUserLeaveHint() {
443 super.onUserLeaveHint();
444 sPausedFromUserAction = true;
445 }
446
Adam Cohenf9426d52012-06-04 17:26:21 -0700447 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700448 boolean searchVisible = false;
449 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800450 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700451 int coi = getCurrentOrientationIndexForGlobalIcons();
452 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
453 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700454 updateAppMarketIcon();
455 searchVisible = updateGlobalSearchIcon();
456 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700457 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700458 if (sGlobalSearchIcon[coi] != null) {
459 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700460 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700461 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700462 if (sVoiceSearchIcon[coi] != null) {
463 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700464 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700465 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700466 if (sAppMarketIcon[coi] != null) {
467 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800468 }
Winson Chungadf0c182012-08-23 14:59:07 -0700469 if (mSearchDropTargetBar != null) {
470 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
Romain Guycbb89e42009-06-08 15:52:54 -0700473
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700475 if (sLocaleConfiguration == null) {
476 new AsyncTask<Void, Void, LocaleConfiguration>() {
477 @Override
478 protected LocaleConfiguration doInBackground(Void... unused) {
479 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
480 readConfiguration(Launcher.this, localeConfiguration);
481 return localeConfiguration;
482 }
483
484 @Override
485 protected void onPostExecute(LocaleConfiguration result) {
486 sLocaleConfiguration = result;
487 checkForLocaleChange(); // recursive, but now with a locale configuration
488 }
489 }.execute();
490 return;
491 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700492
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 final Configuration configuration = getResources().getConfiguration();
494
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 final String locale = configuration.locale.toString();
497
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700498 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 final int mcc = configuration.mcc;
500
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700501 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 final int mnc = configuration.mnc;
503
Romain Guy84f296c2009-11-04 15:00:44 -0800504 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505
Romain Guy84f296c2009-11-04 15:00:44 -0800506 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700507 sLocaleConfiguration.locale = locale;
508 sLocaleConfiguration.mcc = mcc;
509 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700510
Joe Onorato0589f0f2010-02-08 13:44:00 -0800511 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700512
513 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
514 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700515 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700516 public void run() {
517 writeConfiguration(Launcher.this, localeConfiguration);
518 }
519 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700520 }
521 }
522
523 private static class LocaleConfiguration {
524 public String locale;
525 public int mcc = -1;
526 public int mnc = -1;
527 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700528
Romain Guy98d01652009-06-30 16:21:04 -0700529 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
530 DataInputStream in = null;
531 try {
532 in = new DataInputStream(context.openFileInput(PREFERENCES));
533 configuration.locale = in.readUTF();
534 configuration.mcc = in.readInt();
535 configuration.mnc = in.readInt();
536 } catch (FileNotFoundException e) {
537 // Ignore
538 } catch (IOException e) {
539 // Ignore
540 } finally {
541 if (in != null) {
542 try {
543 in.close();
544 } catch (IOException e) {
545 // Ignore
546 }
547 }
548 }
549 }
550
551 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
552 DataOutputStream out = null;
553 try {
554 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
555 out.writeUTF(configuration.locale);
556 out.writeInt(configuration.mcc);
557 out.writeInt(configuration.mnc);
558 out.flush();
559 } catch (FileNotFoundException e) {
560 // Ignore
561 } catch (IOException e) {
562 //noinspection ResultOfMethodCallIgnored
563 context.getFileStreamPath(PREFERENCES).delete();
564 } finally {
565 if (out != null) {
566 try {
567 out.close();
568 } catch (IOException e) {
569 // Ignore
570 }
571 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 }
573 }
574
Bjorn Bringertc459e522013-06-07 19:36:01 +0100575 public LayoutInflater getInflater() {
576 return mInflater;
577 }
578
Adam Cohen716b51e2011-06-30 12:09:54 -0700579 public DragLayer getDragLayer() {
580 return mDragLayer;
581 }
582
Winson Chung36a62fe2012-05-06 18:04:42 -0700583 boolean isDraggingEnabled() {
584 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
585 // that is subsequently removed from the workspace in startBinding().
586 return !mModel.isLoadingWorkspace();
587 }
588
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 static int getScreen() {
590 synchronized (sLock) {
591 return sScreen;
592 }
593 }
594
595 static void setScreen(int screen) {
596 synchronized (sLock) {
597 sScreen = screen;
598 }
599 }
600
Winson Chung557d6ed2011-07-08 15:34:52 -0700601 /**
602 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
603 * a configuration step, this allows the proper animations to run after other transitions.
604 */
605 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700606 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800607 switch (args.requestCode) {
608 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700609 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700610 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800611 break;
612 case REQUEST_PICK_SHORTCUT:
613 processShortcut(args.intent);
614 break;
615 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700616 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700617 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700618 result = true;
619 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800620 case REQUEST_CREATE_APPWIDGET:
621 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700622 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700623 result = true;
624 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800625 case REQUEST_PICK_WALLPAPER:
626 // We just wanted the activity result here so we can clear mWaitingForResult
627 break;
628 }
Michael Jurka27614d22012-04-02 06:40:22 -0700629 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
630 // if you turned the screen off and then back while in All Apps, Launcher would not
631 // return to the workspace. Clearing mAddInfo.container here fixes this issue
632 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700633 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800634 }
635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700637 protected void onActivityResult(
638 final int requestCode, final int resultCode, final Intent data) {
639 if (requestCode == REQUEST_BIND_APPWIDGET) {
640 int appWidgetId = data != null ?
641 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
642 if (resultCode == RESULT_CANCELED) {
643 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
644 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700645 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700646 }
647 return;
648 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700649 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
651 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700652 mWaitingForResult = false;
653
Adam Cohened66b2b2012-01-23 17:28:51 -0800654 // We have special handling for widgets
655 if (isWidgetDrop) {
656 int appWidgetId = data != null ?
657 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700658 if (appWidgetId < 0) {
659 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
660 "widget configuration activity.");
661 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
662 } else {
663 completeTwoStageWidgetDrop(resultCode, appWidgetId);
664 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800665 return;
666 }
667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 // The pattern used here is that a user PICKs a specific application,
669 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
672 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700673 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800674 final PendingAddArguments args = new PendingAddArguments();
675 args.requestCode = requestCode;
676 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700677 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700678 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700679 args.cellX = mPendingAddInfo.cellX;
680 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 if (isWorkspaceLocked()) {
682 sPendingAddList.add(args);
683 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700684 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700688 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800689 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
690 null);
691 }
692
693 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700694 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700695 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800696 Runnable onCompleteRunnable = null;
697 int animationType = 0;
698
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700699 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800700 if (resultCode == RESULT_OK) {
701 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
702 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700703 mPendingAddWidgetInfo);
704 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800705 onCompleteRunnable = new Runnable() {
706 @Override
707 public void run() {
708 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800710 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
711 null);
712 }
713 };
714 } else if (resultCode == RESULT_CANCELED) {
715 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
716 onCompleteRunnable = new Runnable() {
717 @Override
718 public void run() {
719 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
720 null);
721 }
722 };
723 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700724 if (mDragLayer.getAnimatedView() != null) {
725 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
726 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
727 animationType, boundWidget, true);
728 } else {
729 // The animated view may be null in the case of a rotation during widget configuration
730 onCompleteRunnable.run();
731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
733
734 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700735 protected void onStop() {
736 super.onStop();
737 FirstFrameAnimatorHelper.setIsVisible(false);
738 }
739
740 @Override
741 protected void onStart() {
742 super.onStart();
743 FirstFrameAnimatorHelper.setIsVisible(true);
744 }
745
746 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200748 long startTime = 0;
749 if (DEBUG_RESUME_TIME) {
750 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400751 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200752 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700754
Winson Chung4a2afa32012-07-19 14:53:05 -0700755 // Restore the previous launcher state
756 if (mOnResumeState == State.WORKSPACE) {
757 showWorkspace(false);
758 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
759 showAllApps(false);
760 }
761 mOnResumeState = State.NONE;
762
Craig Mautner360310b2012-10-26 15:13:08 -0700763 // Background was set to gradient in onPause(), restore to black if in all apps.
764 setWorkspaceBackground(mState == State.WORKSPACE);
765
Winson Chungde0fb8f2012-05-08 14:37:08 -0700766 // Process any items that were added while Launcher was away
767 InstallShortcutReceiver.flushInstallQueue(this);
768
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800769 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700770 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700771 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700773 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400774 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700775 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700777 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200778 // We might have postponed some bind calls until onResume (see waitUntilResume) --
779 // execute them here
780 long startTimeCallbacks = 0;
781 if (DEBUG_RESUME_TIME) {
782 startTimeCallbacks = System.currentTimeMillis();
783 }
784
785 if (mAppsCustomizeContent != null) {
786 mAppsCustomizeContent.setBulkBind(true);
787 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700788 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
789 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200790 }
791 if (mAppsCustomizeContent != null) {
792 mAppsCustomizeContent.setBulkBind(false);
793 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700794 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200795 if (DEBUG_RESUME_TIME) {
796 Log.d(TAG, "Time spent processing callbacks in onResume: " +
797 (System.currentTimeMillis() - startTimeCallbacks));
798 }
Michael Jurka447bf842013-05-15 14:52:15 +0200799 }
Winson Chunge4e50662012-01-23 14:45:13 -0800800
801 // Reset the pressed state of icons that were locked in the press state while activities
802 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800803 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800804 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800805 mWaitingForResume.setStayPressed(false);
806 }
Winson Chunge4e50662012-01-23 14:45:13 -0800807 if (mAppsCustomizeContent != null) {
808 // Resets the previous all apps icon press state
809 mAppsCustomizeContent.resetDrawableState();
810 }
Adam Cohen06dff352012-06-01 17:17:08 -0700811 // It is possible that widgets can receive updates while launcher is not in the foreground.
812 // Consequently, the widgets will be inflated in the orientation of the foreground activity
813 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
814 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700815 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700816
817 // Again, as with the above scenario, it's possible that one or more of the global icons
818 // were updated in the wrong orientation.
819 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200820 if (DEBUG_RESUME_TIME) {
821 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
822 }
Adam Cohen06dff352012-06-01 17:17:08 -0700823 }
824
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700826 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700827 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
828 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
829 // when Launcher resumes and we are still in AllApps.
830 updateWallpaperVisibility(true);
831
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700832 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700833 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800834 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700835 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700836 }
Romain Guycbb89e42009-06-08 15:52:54 -0700837
Adam Cohen66a01fd2013-06-11 12:48:00 -0700838 protected void onFinishBindingItems() {
839 }
840
841 // Add a fullscreen unpadded view to the workspace to the left all other screens.
842 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700843 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700844 }
845
Adam Cohenedb40762013-07-18 16:45:45 -0700846 // The custom content needs to offset its content to account for the QSB
847 public int getTopOffsetForCustomContent() {
848 return mWorkspace.getPaddingTop();
849 }
850
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700851 @Override
852 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853 // Flag the loader to stop early before switching
854 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700855 if (mAppsCustomizeContent != null) {
856 mAppsCustomizeContent.surrender();
857 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800858 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700859 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700860
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800861 // We can't hide the IME if it was forced open. So don't bother
862 /*
863 @Override
864 public void onWindowFocusChanged(boolean hasFocus) {
865 super.onWindowFocusChanged(hasFocus);
866
867 if (hasFocus) {
868 final InputMethodManager inputManager = (InputMethodManager)
869 getSystemService(Context.INPUT_METHOD_SERVICE);
870 WindowManager.LayoutParams lp = getWindow().getAttributes();
871 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
872 android.os.Handler()) {
873 protected void onReceiveResult(int resultCode, Bundle resultData) {
874 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
875 }
876 });
877 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
878 }
879 }
880 */
881
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 private boolean acceptFilter() {
883 final InputMethodManager inputManager = (InputMethodManager)
884 getSystemService(Context.INPUT_METHOD_SERVICE);
885 return !inputManager.isFullscreenMode();
886 }
887
888 @Override
889 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700890 final int uniChar = event.getUnicodeChar();
891 final boolean handled = super.onKeyDown(keyCode, event);
892 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
893 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
895 keyCode, event);
896 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700897 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700898 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700899 // showSearchDialog()
900 // If there are multiple keystrokes before the search dialog takes focus,
901 // onSearchRequested() will be called for every keystroke,
902 // but it is idempotent, so it's fine.
903 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905 }
906
Joe Onorato8a9625e2010-01-28 15:55:35 -0800907 // Eat the long press event so the keyboard doesn't come up.
908 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
909 return true;
910 }
911
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 return handled;
913 }
914
Karl Rosaen138a0412009-04-23 19:00:21 -0700915 private String getTypedText() {
916 return mDefaultKeySsb.toString();
917 }
918
919 private void clearTypedText() {
920 mDefaultKeySsb.clear();
921 mDefaultKeySsb.clearSpans();
922 Selection.setSelection(mDefaultKeySsb, 0);
923 }
924
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700926 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
927 * State
928 */
929 private static State intToState(int stateOrdinal) {
930 State state = State.WORKSPACE;
931 final State[] stateValues = State.values();
932 for (int i = 0; i < stateValues.length; i++) {
933 if (stateValues[i].ordinal() == stateOrdinal) {
934 state = stateValues[i];
935 break;
936 }
937 }
938 return state;
939 }
940
941 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 * Restores the previous state, if it exists.
943 *
944 * @param savedState The previous state.
945 */
946 private void restoreState(Bundle savedState) {
947 if (savedState == null) {
948 return;
949 }
950
Michael Jurka883f55b2010-10-21 15:47:14 -0700951 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700952 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700953 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800954 }
955
Winson Chungf0c6ae02012-03-21 16:10:31 -0700956 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700958 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
960
Winson Chung3d503fb2011-07-13 17:25:49 -0700961 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700962 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700963
Winson Chung3d503fb2011-07-13 17:25:49 -0700964 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
965 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700966 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700967 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
968 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700969 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
970 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
971 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700972 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 mRestoring = true;
974 }
975
976 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
977 if (renameFolder) {
978 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700979 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 mRestoring = true;
981 }
Winson Chunga12a2502010-12-20 14:41:35 -0800982
Winson Chung785d2eb2011-04-14 16:08:02 -0700983 // Restore the AppsCustomize tab
984 if (mAppsCustomizeTabHost != null) {
985 String curTab = savedState.getString("apps_customize_currentTab");
986 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700987 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700988 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700989 mAppsCustomizeContent.loadAssociatedPages(
990 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700991 }
992
Winson Chung5afbf7b2011-07-25 11:53:08 -0700993 int currentIndex = savedState.getInt("apps_customize_currentIndex");
994 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997
998 /**
999 * Finds all the views we need and configure them properly.
1000 */
1001 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001002 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001003
Craig Mautner360310b2012-10-26 15:13:08 -07001004 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001005 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1006 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001007 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001008
1009 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1010 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011
Winson Chung4c98d922011-05-31 16:50:48 -07001012 // Setup the drag layer
1013 mDragLayer.setup(this, dragController);
1014
Winson Chung3d503fb2011-07-13 17:25:49 -07001015 // Setup the hotseat
1016 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1017 if (mHotseat != null) {
1018 mHotseat.setup(this);
1019 }
1020
Winson Chung4c98d922011-05-31 16:50:48 -07001021 // Setup the workspace
1022 mWorkspace.setHapticFeedbackEnabled(false);
1023 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001024 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001025 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001026
Winson Chungf0ea4d32011-06-06 14:27:16 -07001027 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001028 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001029
Winson Chungf0ea4d32011-06-06 14:27:16 -07001030 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001031 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001032 mAppsCustomizeContent = (AppsCustomizePagedView)
1033 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1034 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001035
Winson Chung3d503fb2011-07-13 17:25:49 -07001036 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001037 dragController.setDragScoller(mWorkspace);
1038 dragController.setScrollView(mDragLayer);
1039 dragController.setMoveTarget(mWorkspace);
1040 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001041 if (mSearchDropTargetBar != null) {
1042 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001043 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001044
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001045 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001046 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001047 mWeightWatcher = new WeightWatcher(this);
1048 mWeightWatcher.setAlpha(0.5f);
1049 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001050 new FrameLayout.LayoutParams(
1051 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001052 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001053 Gravity.BOTTOM)
1054 );
Adam Cohen39a06042013-07-19 14:30:12 -07001055
1056 boolean show = shouldShowWeightWatcher();
1057 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
1060
1061 /**
1062 * Creates a view representing a shortcut.
1063 *
1064 * @param info The data structure describing the shortcut.
1065 *
1066 * @return A View inflated from R.layout.application.
1067 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001068 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001070 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 }
1072
1073 /**
1074 * Creates a view representing a shortcut inflated from the specified resource.
1075 *
1076 * @param layoutResId The id of the XML layout used to create the shortcut.
1077 * @param parent The group the shortcut belongs to.
1078 * @param info The data structure describing the shortcut.
1079 *
1080 * @return A View inflated from layoutResId.
1081 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001082 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001083 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1084 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 return favorite;
1087 }
1088
1089 /**
1090 * Add an application shortcut to the workspace.
1091 *
1092 * @param data The intent describing the application.
1093 * @param cellInfo The position on screen where to create the shortcut.
1094 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001095 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001096 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001097 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098
Adam Cohenfbba09b2011-07-18 21:43:05 -07001099 // First we check if we already know the exact location where we want to add this item.
1100 if (cellX >= 0 && cellY >= 0) {
1101 cellXY[0] = cellX;
1102 cellXY[1] = cellY;
1103 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001104 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001105 return;
1106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001108 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001109
Romain Guy73b979d2009-06-09 12:57:21 -07001110 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001111 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001113 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001114 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001115 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001116 } else {
1117 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
1119 }
Romain Guycbb89e42009-06-08 15:52:54 -07001120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 /**
1122 * Add a shortcut to the workspace.
1123 *
1124 * @param data The intent describing the shortcut.
1125 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001127 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001128 int cellY) {
1129 int[] cellXY = mTmpAddItemCellCoordinates;
1130 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001131 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001132
Michael Jurkad3ef3062010-11-23 16:23:58 -08001133 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001134
Adam Cohen558baaf2011-08-15 15:22:57 -07001135 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001136 if (info == null) {
1137 return;
1138 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001139 final View view = createShortcut(info);
1140
Adam Cohenfbba09b2011-07-18 21:43:05 -07001141 // First we check if we already know the exact location where we want to add this item.
1142 if (cellX >= 0 && cellY >= 0) {
1143 cellXY[0] = cellX;
1144 cellXY[1] = cellY;
1145 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001146
1147 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001148 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001149 true, null,null)) {
1150 return;
1151 }
1152 DragObject dragObject = new DragObject();
1153 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001154 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1155 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001156 return;
1157 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001158 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001159 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001160 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001161 foundCellSpan = (result != null);
1162 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001163 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001164 }
1165
1166 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001167 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001168 return;
1169 }
1170
Adam Cohendcd297f2013-06-18 13:13:40 -07001171 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172
1173 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001174 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001175 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177 }
1178
Adam Cohen2f093b62012-04-30 18:59:53 -07001179 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1180 int minHeight) {
1181 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001182 // We want to account for the extra amount of padding that we are adding to the widget
1183 // to ensure that it gets the full amount of space that it has requested
1184 int requiredWidth = minWidth + padding.left + padding.right;
1185 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001186 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001187 }
1188
Adam Cohen2f093b62012-04-30 18:59:53 -07001189 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1190 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001191 }
1192
Adam Cohen2f093b62012-04-30 18:59:53 -07001193 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1194 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001195 }
1196
Adam Cohen2f093b62012-04-30 18:59:53 -07001197 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1198 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001199 }
1200
Adam Cohen2f093b62012-04-30 18:59:53 -07001201 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1202 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001203 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001204 }
1205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001207 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001209 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001210 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001212 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001213 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1214 if (appWidgetInfo == null) {
1215 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1216 }
Romain Guycbb89e42009-06-08 15:52:54 -07001217
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001218 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001219 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001220
Adam Cohen2f093b62012-04-30 18:59:53 -07001221 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1222 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001225 // We have saved the position to which the widget was dragged-- this really only matters
1226 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001227 int[] cellXY = mTmpAddItemCellCoordinates;
1228 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001229 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001230 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001231 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1232 cellXY[0] = mPendingAddInfo.cellX;
1233 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001234 spanXY[0] = mPendingAddInfo.spanX;
1235 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001236 foundCellSpan = true;
1237 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001239 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001240 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1241 spanXY[1], cellXY, finalSpan);
1242 spanXY[0] = finalSpan[0];
1243 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001244 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001245 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001246 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001247 }
1248
Michael Jurka0280c3b2010-09-17 15:00:07 -07001249 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001250 if (appWidgetId != -1) {
1251 // Deleting an app widget ID is a void call but writes to disk before returning
1252 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001253 new Thread("deleteAppWidgetId") {
1254 public void run() {
1255 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1256 }
1257 }.start();
1258 }
Winson Chung93eef082012-03-23 15:59:27 -07001259 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001260 return;
1261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001263 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001264 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1265 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001266 launcherInfo.spanX = spanXY[0];
1267 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001268 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1269 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001272 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273
1274 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001275 if (hostView == null) {
1276 // Perform actual inflation because we're live
1277 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1278 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1279 } else {
1280 // The AppWidgetHostView has already been inflated and instantiated
1281 launcherInfo.hostView = hostView;
1282 }
Romain Guycbb89e42009-06-08 15:52:54 -07001283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001285 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001286 launcherInfo.notifyWidgetSizeChanged(this);
1287
Adam Cohendcd297f2013-06-18 13:13:40 -07001288 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001289 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001290
1291 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001293 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 }
Romain Guycbb89e42009-06-08 15:52:54 -07001295
Adam Cohended9f8d2010-11-03 13:25:16 -07001296 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1297 @Override
1298 public void onReceive(Context context, Intent intent) {
1299 final String action = intent.getAction();
1300 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1301 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001302 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001303 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001304
Winson Chungc100e8e2011-08-09 16:02:43 -07001305 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001306 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001307 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1308 mAppsCustomizeTabHost.reset();
1309 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001310 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001311 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1312 mUserPresent = true;
1313 updateRunning();
1314 }
1315 }
1316 };
1317
1318 @Override
1319 public void onAttachedToWindow() {
1320 super.onAttachedToWindow();
1321
1322 // Listen for broadcasts related to user-presence
1323 final IntentFilter filter = new IntentFilter();
1324 filter.addAction(Intent.ACTION_SCREEN_OFF);
1325 filter.addAction(Intent.ACTION_USER_PRESENT);
1326 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001327 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001328 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001329 mVisible = true;
1330 }
1331
1332 @Override
1333 public void onDetachedFromWindow() {
1334 super.onDetachedFromWindow();
1335 mVisible = false;
1336
Adam Cohend113e0c2010-11-11 10:48:05 -08001337 if (mAttached) {
1338 unregisterReceiver(mReceiver);
1339 mAttached = false;
1340 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001341 updateRunning();
1342 }
1343
1344 public void onWindowVisibilityChanged(int visibility) {
1345 mVisible = visibility == View.VISIBLE;
1346 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001347 // The following code used to be in onResume, but it turns out onResume is called when
1348 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1349 // is a more appropriate event to handle
1350 if (mVisible) {
1351 mAppsCustomizeTabHost.onWindowVisible();
1352 if (!mWorkspaceLoading) {
1353 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001354 // We want to let Launcher draw itself at least once before we force it to build
1355 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001356 // apps is nice and speedy.
1357 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001358 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001359 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001360 if (mStarted) return;
1361 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001362 // We delay the layer building a bit in order to give
1363 // other message processing a time to run. In particular
1364 // this avoids a delay in hiding the IME if it was
1365 // currently shown, because doing that may involve
1366 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001367 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001368 final ViewTreeObserver.OnDrawListener listener = this;
1369 mWorkspace.post(new Runnable() {
1370 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001371 if (mWorkspace != null &&
1372 mWorkspace.getViewTreeObserver() != null) {
1373 mWorkspace.getViewTreeObserver().
1374 removeOnDrawListener(listener);
1375 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001376 }
1377 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001378 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001379 }
1380 });
1381 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001382 // When Launcher comes back to foreground, a different Activity might be responsible for
1383 // the app market intent, so refresh the icon
1384 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001385 clearTypedText();
1386 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001387 }
1388
1389 private void sendAdvanceMessage(long delay) {
1390 mHandler.removeMessages(ADVANCE_MSG);
1391 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1392 mHandler.sendMessageDelayed(msg, delay);
1393 mAutoAdvanceSentTime = System.currentTimeMillis();
1394 }
1395
1396 private void updateRunning() {
1397 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1398 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1399 mAutoAdvanceRunning = autoAdvanceRunning;
1400 if (autoAdvanceRunning) {
1401 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1402 sendAdvanceMessage(delay);
1403 } else {
1404 if (!mWidgetsToAdvance.isEmpty()) {
1405 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1406 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1407 }
1408 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001409 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001410 }
1411 }
1412 }
1413
1414 private final Handler mHandler = new Handler() {
1415 @Override
1416 public void handleMessage(Message msg) {
1417 if (msg.what == ADVANCE_MSG) {
1418 int i = 0;
1419 for (View key: mWidgetsToAdvance.keySet()) {
1420 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1421 final int delay = mAdvanceStagger * i;
1422 if (v instanceof Advanceable) {
1423 postDelayed(new Runnable() {
1424 public void run() {
1425 ((Advanceable) v).advance();
1426 }
1427 }, delay);
1428 }
1429 i++;
1430 }
1431 sendAdvanceMessage(mAdvanceInterval);
1432 }
1433 }
1434 };
1435
1436 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001437 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001438 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1439 if (v instanceof Advanceable) {
1440 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001441 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001442 updateRunning();
1443 }
1444 }
1445
1446 void removeWidgetToAutoAdvance(View hostView) {
1447 if (mWidgetsToAdvance.containsKey(hostView)) {
1448 mWidgetsToAdvance.remove(hostView);
1449 updateRunning();
1450 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001451 }
1452
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001454 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001455 launcherInfo.hostView = null;
1456 }
1457
Winson Chung93eef082012-03-23 15:59:27 -07001458 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1459 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1460 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001461 }
1462
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001463 public LauncherAppWidgetHost getAppWidgetHost() {
1464 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
Romain Guycbb89e42009-06-08 15:52:54 -07001466
Winson Chunga9abd0e2010-10-27 17:18:37 -07001467 public LauncherModel getModel() {
1468 return mModel;
1469 }
1470
Bjorn Bringertc459e522013-06-07 19:36:01 +01001471 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001472 getWindow().closeAllPanels();
1473
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001474 // Whatever we were doing is hereby canceled.
1475 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001476 }
1477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 @Override
1479 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001480 long startTime = 0;
1481 if (DEBUG_RESUME_TIME) {
1482 startTime = System.currentTimeMillis();
1483 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 super.onNewIntent(intent);
1485
1486 // Close the menu
1487 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001488 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001489 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001490
Jamie Genniscb222e82012-10-23 15:44:26 -07001491 final boolean alreadyOnHome =
1492 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001493 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001494
Jamie Genniscb222e82012-10-23 15:44:26 -07001495 Runnable processIntent = new Runnable() {
1496 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001497 if (mWorkspace == null) {
1498 // Can be cases where mWorkspace is null, this prevents a NPE
1499 return;
1500 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001501 Folder openFolder = mWorkspace.getOpenFolder();
1502 // In all these cases, only animate if we're already on home
1503 mWorkspace.exitWidgetResizeMode();
1504 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1505 openFolder == null) {
1506 mWorkspace.moveToDefaultScreen(true);
1507 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001508
Jamie Genniscb222e82012-10-23 15:44:26 -07001509 closeFolder();
1510 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001511
Jamie Genniscb222e82012-10-23 15:44:26 -07001512 // If we are already on home, then just animate back to the workspace,
1513 // otherwise, just wait until onResume to set the state back to Workspace
1514 if (alreadyOnHome) {
1515 showWorkspace(true);
1516 } else {
1517 mOnResumeState = State.WORKSPACE;
1518 }
1519
1520 final View v = getWindow().peekDecorView();
1521 if (v != null && v.getWindowToken() != null) {
1522 InputMethodManager imm = (InputMethodManager)getSystemService(
1523 INPUT_METHOD_SERVICE);
1524 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1525 }
1526
1527 // Reset AllApps to its initial state
1528 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1529 mAppsCustomizeTabHost.reset();
1530 }
1531 }
1532 };
1533
1534 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1535 // Delay processing of the intent to allow the status bar animation to finish
1536 // first in order to avoid janky animations.
1537 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001538 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001539 // Process the intent immediately.
1540 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001541 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
Michael Jurka447bf842013-05-15 14:52:15 +02001544 if (DEBUG_RESUME_TIME) {
1545 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
1548
1549 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001550 public void onRestoreInstanceState(Bundle state) {
1551 super.onRestoreInstanceState(state);
1552 for (int page: mSynchronouslyBoundPages) {
1553 mWorkspace.restoreInstanceStateForChild(page);
1554 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556
1557 @Override
1558 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001559 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001560 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561
Michael Jurka883f55b2010-10-21 15:47:14 -07001562 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001563 // We close any open folder since it will not be re-opened, and we need to make sure
1564 // this state is reflected.
1565 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566
Adam Cohendcd297f2013-06-18 13:13:40 -07001567 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001568 mWaitingForResult) {
1569 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001570 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001571 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1572 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001573 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1574 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1575 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 }
1577
1578 if (mFolderInfo != null && mWaitingForResult) {
1579 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1580 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1581 }
Michael Jurka946ad472010-07-09 18:05:18 -07001582
Winson Chung785d2eb2011-04-14 16:08:02 -07001583 // Save the current AppsCustomize tab
1584 if (mAppsCustomizeTabHost != null) {
1585 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1586 if (currentTabTag != null) {
1587 outState.putString("apps_customize_currentTab", currentTabTag);
1588 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001589 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1590 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001591 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
1593
1594 @Override
1595 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001597
Winson Chunge7a03942011-08-05 15:05:12 -07001598 // Remove all pending runnables
1599 mHandler.removeMessages(ADVANCE_MSG);
1600 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001601 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001602
Winson Chungcd2b0142011-06-08 16:02:26 -07001603 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001604 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001605 mModel.stopLoader();
1606 app.setLauncher(null);
1607
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001609 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001611 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001613 mAppWidgetHost = null;
1614
1615 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616
1617 TextKeyListener.getInstance().release();
1618
Winson Chung81b52252012-08-27 15:34:29 -07001619 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1620 // to prevent leaking Launcher activities on orientation change.
1621 if (mModel != null) {
1622 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1623 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001624
1625 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001626 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001627
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001628 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001629 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1630 mWorkspace.removeAllViews();
1631 mWorkspace = null;
1632 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001633
Michael Jurka2ecf9952012-06-18 12:52:28 -07001634 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
1636
Adam Cohena9cf38f2011-05-02 15:36:58 -07001637 public DragController getDragController() {
1638 return mDragController;
1639 }
1640
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 @Override
1642 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001643 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 super.startActivityForResult(intent, requestCode);
1645 }
1646
Winson Chung70d72102011-08-12 11:24:35 -07001647 /**
1648 * Indicates that we want global search for this activity by setting the globalSearch
1649 * argument for {@link #startSearch} to true.
1650 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001652 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001654
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001655 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001656
Karl Rosaen138a0412009-04-23 19:00:21 -07001657 if (initialQuery == null) {
1658 // Use any text typed in the launcher as the initial query
1659 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 if (appSearchData == null) {
1662 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001663 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
Winson Chungadf0c182012-08-23 14:59:07 -07001665 Rect sourceBounds = new Rect();
1666 if (mSearchDropTargetBar != null) {
1667 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1668 }
Romain Guycbb89e42009-06-08 15:52:54 -07001669
Bjorn Bringertc459e522013-06-07 19:36:01 +01001670 startSearch(initialQuery, selectInitialQuery,
1671 appSearchData, sourceBounds);
1672 }
1673
1674 public void startSearch(String initialQuery,
1675 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001676 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001677 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001678 }
1679
1680 /**
1681 * Starts the global search activity. This code is a copied from SearchManager
1682 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001683 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001684 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001685 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001686 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1687 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1688 if (globalSearchActivity == null) {
1689 Log.w(TAG, "No global search activity found.");
1690 return;
1691 }
1692 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1693 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1694 intent.setComponent(globalSearchActivity);
1695 // Make sure that we have a Bundle to put source in
1696 if (appSearchData == null) {
1697 appSearchData = new Bundle();
1698 } else {
1699 appSearchData = new Bundle(appSearchData);
1700 }
1701 // Set source to package name of app that starts global search, if not set already.
1702 if (!appSearchData.containsKey("source")) {
1703 appSearchData.putString("source", getPackageName());
1704 }
1705 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1706 if (!TextUtils.isEmpty(initialQuery)) {
1707 intent.putExtra(SearchManager.QUERY, initialQuery);
1708 }
1709 if (selectInitialQuery) {
1710 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1711 }
1712 intent.setSourceBounds(sourceBounds);
1713 try {
1714 startActivity(intent);
1715 } catch (ActivityNotFoundException ex) {
1716 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 }
1719
Winson Chung70d72102011-08-12 11:24:35 -07001720 @Override
1721 public boolean onCreateOptionsMenu(Menu menu) {
1722 if (isWorkspaceLocked()) {
1723 return false;
1724 }
1725
1726 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001727
1728 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1729 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1730 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001731 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1732 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1733 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001734 String helpUrl = getString(R.string.help_url);
1735 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001736 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1737 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1738
Winson Chung70d72102011-08-12 11:24:35 -07001739 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1740 .setIcon(android.R.drawable.ic_menu_gallery)
1741 .setAlphabeticShortcut('W');
1742 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1743 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001744 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001745 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001746 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1747 .setIcon(android.R.drawable.ic_menu_preferences)
1748 .setIntent(settings)
1749 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001750 if (!helpUrl.isEmpty()) {
1751 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1752 .setIcon(android.R.drawable.ic_menu_help)
1753 .setIntent(help)
1754 .setAlphabeticShortcut('H');
1755 }
Winson Chung70d72102011-08-12 11:24:35 -07001756 return true;
1757 }
1758
1759 @Override
1760 public boolean onPrepareOptionsMenu(Menu menu) {
1761 super.onPrepareOptionsMenu(menu);
1762
1763 if (mAppsCustomizeTabHost.isTransitioning()) {
1764 return false;
1765 }
1766 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1767 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1768
1769 return true;
1770 }
1771
1772 @Override
1773 public boolean onOptionsItemSelected(MenuItem item) {
1774 switch (item.getItemId()) {
1775 case MENU_WALLPAPER_SETTINGS:
1776 startWallpaper();
1777 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001778 }
1779
1780 return super.onOptionsItemSelected(item);
1781 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001783 @Override
1784 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001785 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001786 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001787 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001788 }
1789
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790 public boolean isWorkspaceLocked() {
1791 return mWorkspaceLoading || mWaitingForResult;
1792 }
1793
Michael Jurka0280c3b2010-09-17 15:00:07 -07001794 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001795 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001796 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001797 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1798 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001799 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001800 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001801 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001802
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001803 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1804 AppWidgetProviderInfo appWidgetInfo) {
1805 if (appWidgetInfo.configure != null) {
1806 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001807
Bjorn Bringert7984c942009-12-09 15:38:25 +00001808 // Launch over to configure widget, if needed
1809 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001810 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001811 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001812 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001814 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001815 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001816 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001817 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001818 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
1820 }
Romain Guycbb89e42009-06-08 15:52:54 -07001821
Adam Cohenfbba09b2011-07-18 21:43:05 -07001822 /**
1823 * Process a shortcut drop.
1824 *
1825 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001826 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001827 * @param cell The cell it should be added to, optional
1828 * @param position The location on the screen where it was dropped, optional
1829 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001830 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001831 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001832 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001833 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001834 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001835 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001836
1837 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001838 mPendingAddInfo.cellX = cell[0];
1839 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001840 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001841
1842 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1843 createShortcutIntent.setComponent(componentName);
1844 processShortcut(createShortcutIntent);
1845 }
1846
Adam Cohenfbba09b2011-07-18 21:43:05 -07001847 /**
1848 * Process a widget drop.
1849 *
1850 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001851 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001852 * @param cell The cell it should be added to, optional
1853 * @param position The location on the screen where it was dropped, optional
1854 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001855 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001856 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001857 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001858 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001859 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001860 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001861 mPendingAddInfo.minSpanX = info.minSpanX;
1862 mPendingAddInfo.minSpanY = info.minSpanY;
1863
Adam Cohenfbba09b2011-07-18 21:43:05 -07001864 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001865 mPendingAddInfo.cellX = cell[0];
1866 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001867 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001868 if (span != null) {
1869 mPendingAddInfo.spanX = span[0];
1870 mPendingAddInfo.spanY = span[1];
1871 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001872
Adam Cohened66b2b2012-01-23 17:28:51 -08001873 AppWidgetHostView hostView = info.boundWidget;
1874 int appWidgetId;
1875 if (hostView != null) {
1876 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001877 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001878 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001879 // In this case, we either need to start an activity to get permission to bind
1880 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001881 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001882 Bundle options = info.bindOptions;
1883
1884 boolean success = false;
1885 if (options != null) {
1886 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1887 info.componentName, options);
1888 } else {
1889 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1890 info.componentName);
1891 }
1892 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001893 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001894 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001895 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001896 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1897 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1898 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001899 // TODO: we need to make sure that this accounts for the options bundle.
1900 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001901 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1902 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001903 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001904 }
1905
Joe Onoratodeb98af2010-02-19 14:59:39 -08001906 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001907 // Handle case where user selected "Applications"
1908 String applicationName = getResources().getString(R.string.group_applications);
1909 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001910
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001911 if (applicationName != null && applicationName.equals(shortcutName)) {
1912 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1913 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001914
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001915 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1916 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001917 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001918 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001919 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001920 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 }
1923
Winson Chung24ab2f12010-09-16 14:10:47 -07001924 void processWallpaper(Intent intent) {
1925 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1926 }
1927
Adam Cohendcd297f2013-06-18 13:13:40 -07001928 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001929 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001930 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 folderInfo.title = getText(R.string.folder_name);
1932
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001934 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001935 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001936 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937
1938 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001939 FolderIcon newFolder =
1940 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001941 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001942 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001943 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
Romain Guycbb89e42009-06-08 15:52:54 -07001945
Joe Onorato9c1289c2009-08-17 11:03:03 -04001946 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001947 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001948 }
1949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001951 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001953 Intent chooser = Intent.createChooser(pickWallpaper,
1954 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001955 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1956 // Removed in Eclair MR1
1957// WallpaperManager wm = (WallpaperManager)
1958// getSystemService(Context.WALLPAPER_SERVICE);
1959// WallpaperInfo wi = wm.getWallpaperInfo();
1960// if (wi != null && wi.getSettingsActivity() != null) {
1961// LabeledIntent li = new LabeledIntent(getPackageName(),
1962// R.string.configure_wallpaper, 0);
1963// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1964// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1965// }
Mike Clerona0618e42009-10-22 13:55:21 -07001966 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
1968
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001969 /**
1970 * Registers various content observers. The current implementation registers
1971 * only a favorites observer to keep track of the favorites applications.
1972 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001973 private void registerContentObservers() {
1974 ContentResolver resolver = getContentResolver();
1975 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1976 true, mWidgetObserver);
1977 }
1978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 @Override
1980 public boolean dispatchKeyEvent(KeyEvent event) {
1981 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1982 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001984 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001985 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001986 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001987 dumpState();
1988 return true;
1989 }
1990 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001991 }
1992 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1993 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001994 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 return true;
1996 }
1997 }
1998
1999 return super.dispatchKeyEvent(event);
2000 }
2001
Joe Onorato88ec0992009-11-19 13:16:06 -08002002 @Override
2003 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002004 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002005 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002006 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002007 Folder openFolder = mWorkspace.getOpenFolder();
2008 if (openFolder.isEditingName()) {
2009 openFolder.dismissEditingName();
2010 } else {
2011 closeFolder();
2012 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002013 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002014 mWorkspace.exitWidgetResizeMode();
2015
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002016 // Back button is a no-op here, but give at least some feedback for the button press
2017 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002018 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002019 }
2020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002022 * Re-listen when widgets are reset.
2023 */
2024 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002025 if (mAppWidgetHost != null) {
2026 mAppWidgetHost.startListening();
2027 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002028 }
2029
2030 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 * Launches the intent referred by the clicked shortcut.
2032 *
2033 * @param v The view representing the clicked shortcut.
2034 */
2035 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002036 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2037 // view has detached (it's possible for this to happen if the view is removed mid touch).
2038 if (v.getWindowToken() == null) {
2039 return;
2040 }
2041
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002042 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002043 return;
2044 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002047 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002049 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002050
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002051 // Check for special shortcuts
2052 if (intent.getComponent() != null) {
2053 final String shortcutClass = intent.getComponent().getClassName();
2054
2055 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2056 showAllApps(true);
2057 return;
2058 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2059 MemoryDumpActivity.startDump(this);
2060 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002061 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2062 toggleShowWeightWatcher();
2063 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002064 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002065 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002066
2067 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002068 int[] pos = new int[2];
2069 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002070 intent.setSourceBounds(new Rect(pos[0], pos[1],
2071 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002072
2073 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002074
2075 if (success && v instanceof BubbleTextView) {
2076 mWaitingForResume = (BubbleTextView) v;
2077 mWaitingForResume.setStayPressed(true);
2078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002080 if (v instanceof FolderIcon) {
2081 FolderIcon fi = (FolderIcon) v;
2082 handleFolderClick(fi);
2083 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002084 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002085 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002086 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002087 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002088 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 }
2091 }
2092
Michael Jurka0e260592010-06-30 17:07:39 -07002093 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002094 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002095 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002096 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2097 showWorkspace(true);
2098 }
Michael Jurka0e260592010-06-30 17:07:39 -07002099 return false;
2100 }
2101
Michael Jurkaaf442092010-06-10 17:01:57 -07002102 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002103 * Event handler for the search button
2104 *
2105 * @param v The view that was clicked.
2106 */
2107 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002108 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2109
Amith Yamasania135ba82011-08-09 17:42:01 -07002110 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002111 }
2112
2113 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002114 * Event handler for the voice button
2115 *
2116 * @param v The view that was clicked.
2117 */
2118 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002119 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002120
Bjorn Bringertc459e522013-06-07 19:36:01 +01002121 startVoice();
2122 }
2123
2124 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002125 try {
2126 final SearchManager searchManager =
2127 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2128 ComponentName activityName = searchManager.getGlobalSearchActivity();
2129 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002130 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002131 if (activityName != null) {
2132 intent.setPackage(activityName.getPackageName());
2133 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002134 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002135 } catch (ActivityNotFoundException e) {
2136 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002137 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002138 startActivitySafely(null, intent, "onClickVoiceButton");
2139 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002140 }
2141
2142 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002143 * Event handler for the "grid" button that appears on the home screen, which
2144 * enters all apps mode.
2145 *
2146 * @param v The view that was clicked.
2147 */
2148 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002149 showAllApps(true);
2150 }
2151
2152 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002153 // Provide the same haptic feedback that the system offers for virtual keys.
2154 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002155 }
2156
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002157 public void onClickAppMarketButton(View v) {
2158 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002159 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002160 } else {
2161 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002162 }
2163 }
2164
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002165 void startApplicationDetailsActivity(ComponentName componentName) {
2166 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002167 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2168 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002169 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002170 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002171 }
2172
Michael Jurka1e2f4652013-07-08 18:03:46 -07002173 // returns true if the activity was started
2174 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2175 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002176 // System applications cannot be installed. For now, show a toast explaining that.
2177 // We may give them the option of disabling apps this way.
2178 int messageId = R.string.uninstall_system_app_text;
2179 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002180 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002181 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002182 String packageName = componentName.getPackageName();
2183 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002184 Intent intent = new Intent(
2185 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002186 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2187 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002188 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002189 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002190 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002191 }
2192
Michael Jurka86a720e2012-05-09 11:23:23 -07002193 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002197 // Only launch using the new animation if the shortcut has not opted out (this is a
2198 // private contract between launcher and may be ignored in the future).
2199 boolean useLaunchAnimation = (v != null) &&
2200 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2201 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002202 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2203 v.getMeasuredWidth(), v.getMeasuredHeight());
2204
2205 startActivity(intent, opts.toBundle());
2206 } else {
2207 startActivity(intent);
2208 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002209 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 } catch (SecurityException e) {
2211 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002212 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002214 "or use the exported attribute for this activity. "
2215 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002217 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002219
Michael Jurka86a720e2012-05-09 11:23:23 -07002220 boolean startActivitySafely(View v, Intent intent, Object tag) {
2221 boolean success = false;
2222 try {
2223 success = startActivity(v, intent, tag);
2224 } catch (ActivityNotFoundException e) {
2225 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2226 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2227 }
2228 return success;
2229 }
2230
Romain Guy8e633c52010-03-04 12:51:36 -08002231 void startActivityForResultSafely(Intent intent, int requestCode) {
2232 try {
2233 startActivityForResult(intent, requestCode);
2234 } catch (ActivityNotFoundException e) {
2235 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2236 } catch (SecurityException e) {
2237 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2238 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2239 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2240 "or use the exported attribute for this activity.", e);
2241 }
2242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243
Adam Cohena9cf38f2011-05-02 15:36:58 -07002244 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002245 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002246 Folder openFolder = mWorkspace.getFolderForTag(info);
2247
2248 // If the folder info reports that the associated folder is open, then verify that
2249 // it is actually opened. There have been a few instances where this gets out of sync.
2250 if (info.opened && openFolder == null) {
2251 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002253 info.opened = false;
2254 }
2255
Adam Cohenfb91f302012-06-11 15:45:18 -07002256 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 // Close any open folder
2258 closeFolder();
2259 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002260 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 } else {
2262 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 int folderScreen;
2264 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002265 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 // .. and close it
2267 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002268 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 // Close any folder open on the current screen
2270 closeFolder();
2271 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002272 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 }
2274 }
2275 }
2276 }
2277
Adam Cohen268c4752012-06-06 17:47:33 -07002278 /**
2279 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2280 * in the DragLayer in the exact absolute location of the original FolderIcon.
2281 */
2282 private void copyFolderIconToImage(FolderIcon fi) {
2283 final int width = fi.getMeasuredWidth();
2284 final int height = fi.getMeasuredHeight();
2285
2286 // Lazy load ImageView, Bitmap and Canvas
2287 if (mFolderIconImageView == null) {
2288 mFolderIconImageView = new ImageView(this);
2289 }
2290 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2291 mFolderIconBitmap.getHeight() != height) {
2292 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2293 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2294 }
2295
2296 DragLayer.LayoutParams lp;
2297 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2298 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2299 } else {
2300 lp = new DragLayer.LayoutParams(width, height);
2301 }
2302
Adam Cohen307fe232012-08-16 17:55:58 -07002303 // The layout from which the folder is being opened may be scaled, adjust the starting
2304 // view size by this scale factor.
2305 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002306 lp.customPosition = true;
2307 lp.x = mRectForFolderAnimation.left;
2308 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002309 lp.width = (int) (scale * width);
2310 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002311
2312 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2313 fi.draw(mFolderIconCanvas);
2314 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002315 if (fi.getFolder() != null) {
2316 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2317 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002318 }
Adam Cohen268c4752012-06-06 17:47:33 -07002319 // Just in case this image view is still in the drag layer from a previous animation,
2320 // we remove it and re-add it.
2321 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2322 mDragLayer.removeView(mFolderIconImageView);
2323 }
2324 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002325 if (fi.getFolder() != null) {
2326 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002327 }
Adam Cohen268c4752012-06-06 17:47:33 -07002328 }
2329
Adam Cohen2801caf2011-05-13 20:57:39 -07002330 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002331 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002332 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2333 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2334 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2335
Adam Cohenc51934b2011-07-26 21:07:43 -07002336 FolderInfo info = (FolderInfo) fi.getTag();
2337 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2338 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002339 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2340 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002341 }
2342
Adam Cohen268c4752012-06-06 17:47:33 -07002343 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2344 copyFolderIconToImage(fi);
2345 fi.setVisibility(View.INVISIBLE);
2346
Michael Jurka2ecf9952012-06-18 12:52:28 -07002347 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002348 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002349 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2350 oa.start();
2351 }
2352
Adam Cohen268c4752012-06-06 17:47:33 -07002353 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002354 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002355 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2356 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2357 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2358
Adam Cohen268c4752012-06-06 17:47:33 -07002359 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002360
Adam Cohen268c4752012-06-06 17:47:33 -07002361 // We remove and re-draw the FolderIcon in-case it has changed
2362 mDragLayer.removeView(mFolderIconImageView);
2363 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002364 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002365 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002366 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002367 oa.addListener(new AnimatorListenerAdapter() {
2368 @Override
2369 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002370 if (cl != null) {
2371 cl.clearFolderLeaveBehind();
2372 // Remove the ImageView copy of the FolderIcon and make the original visible.
2373 mDragLayer.removeView(mFolderIconImageView);
2374 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002375 }
2376 }
2377 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002378 oa.start();
2379 }
2380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002382 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 * is animated relative to the specified View. If the View is null, no animation
2384 * is played.
2385 *
2386 * @param folderInfo The FolderInfo describing the folder to open.
2387 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002388 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002389 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002390 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391
Adam Cohena9cf38f2011-05-02 15:36:58 -07002392 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393
Adam Cohen4554ee12011-08-03 16:13:21 -07002394 // Just verify that the folder hasn't already been added to the DragLayer.
2395 // There was a one-off crash where the folder had a parent already.
2396 if (folder.getParent() == null) {
2397 mDragLayer.addView(folder);
2398 mDragController.addDropTarget((DropTarget) folder);
2399 } else {
2400 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2401 folder.getParent() + ").");
2402 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002403 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002404 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002405
2406 // Notify the accessibility manager that this folder "window" has appeared and occluded
2407 // the workspace items
2408 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2409 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002410 }
2411
2412 public void closeFolder() {
2413 Folder folder = mWorkspace.getOpenFolder();
2414 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002415 if (folder.isEditingName()) {
2416 folder.dismissEditingName();
2417 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002418 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002419
2420 // Dismiss the folder cling
2421 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002422 }
2423 }
2424
2425 void closeFolder(Folder folder) {
2426 folder.getInfo().opened = false;
2427
2428 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2429 if (parent != null) {
2430 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2431 shrinkAndFadeInFolderIcon(fi);
2432 }
2433 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002434
2435 // Notify the accessibility manager that this folder "window" has disappeard and no
2436 // longer occludeds the workspace items
2437 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 }
2439
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002441 if (!isDraggingEnabled()) return false;
2442 if (isWorkspaceLocked()) return false;
2443 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444
2445 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002446 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 }
2448
Michael Jurka0280c3b2010-09-17 15:00:07 -07002449 resetAddInfo();
2450 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002452 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 return true;
2454 }
2455
Winson Chung3d503fb2011-07-13 17:25:49 -07002456 // The hotseat touch handling does not go through Workspace, and we always allow long press
2457 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002458 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002459 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2460 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002461 if (itemUnderLongClick == null) {
2462 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002463 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2464 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002465 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002466 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2467 mWorkspace.startReordering();
2468 } else {
2469 startWallpaper();
2470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002472 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002474 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 }
2476 }
2477 }
2478 return true;
2479 }
2480
Winson Chung3d503fb2011-07-13 17:25:49 -07002481 boolean isHotseatLayout(View layout) {
2482 return mHotseat != null && layout != null &&
2483 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2484 }
2485 Hotseat getHotseat() {
2486 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002487 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002488 SearchDropTargetBar getSearchBar() {
2489 return mSearchDropTargetBar;
2490 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002491
Winson Chung3d503fb2011-07-13 17:25:49 -07002492 /**
2493 * Returns the CellLayout of the specified container at the specified screen.
2494 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002495 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002496 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2497 if (mHotseat != null) {
2498 return mHotseat.getLayout();
2499 } else {
2500 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002501 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002502 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002503 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002504 }
2505 }
2506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 Workspace getWorkspace() {
2508 return mWorkspace;
2509 }
2510
Daniel Sandler843e8602010-06-07 14:59:01 -04002511 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002512 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002513 }
2514
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002515 /**
2516 * Helper method for the cameraZoomIn/cameraZoomOut animations
2517 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002518 * @param scaleFactor The scale factor used for the zoom
2519 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002520 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002521 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002522 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002523 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002524
Winson Chung18f41f82012-05-09 13:28:10 -07002525 void disableWallpaperIfInAllApps() {
2526 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002527 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002528 if (mAppsCustomizeTabHost != null &&
2529 !mAppsCustomizeTabHost.isTransitioning()) {
2530 updateWallpaperVisibility(false);
2531 }
2532 }
2533 }
2534
Craig Mautner360310b2012-10-26 15:13:08 -07002535 private void setWorkspaceBackground(boolean workspace) {
2536 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002537 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002538 }
2539
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002540 void updateWallpaperVisibility(boolean visible) {
2541 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2542 int curflags = getWindow().getAttributes().flags
2543 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2544 if (wpflags != curflags) {
2545 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2546 }
Craig Mautner360310b2012-10-26 15:13:08 -07002547 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002548 }
2549
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002550 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2551 if (v instanceof LauncherTransitionable) {
2552 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2553 }
2554 }
2555
Michael Jurkabed61d22012-02-14 22:51:29 -08002556 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2557 if (v instanceof LauncherTransitionable) {
2558 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2559 }
Winson Chung70442722012-02-10 15:43:22 -08002560
2561 // Update the workspace transition step as well
2562 dispatchOnLauncherTransitionStep(v, 0f);
2563 }
2564
2565 private void dispatchOnLauncherTransitionStep(View v, float t) {
2566 if (v instanceof LauncherTransitionable) {
2567 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2568 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002569 }
2570
2571 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2572 if (v instanceof LauncherTransitionable) {
2573 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2574 }
Winson Chung70442722012-02-10 15:43:22 -08002575
2576 // Update the workspace transition step as well
2577 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002578 }
2579
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002580 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002581 * Things to test when changing the following seven functions.
2582 * - Home from workspace
2583 * - from center screen
2584 * - from other screens
2585 * - Home from all apps
2586 * - from center screen
2587 * - from other screens
2588 * - Back from all apps
2589 * - from center screen
2590 * - from other screens
2591 * - Launch app from workspace and quit
2592 * - with back
2593 * - with home
2594 * - Launch app from all apps and quit
2595 * - with back
2596 * - with home
2597 * - Go to a screen that's not the default, then all
2598 * apps, and launch and app, and go back
2599 * - with back
2600 * -with home
2601 * - On workspace, long press power and go back
2602 * - with back
2603 * - with home
2604 * - On all apps, long press power and go back
2605 * - with back
2606 * - with home
2607 * - On workspace, power off
2608 * - On all apps, power off
2609 * - Launch an app and turn off the screen while in that app
2610 * - Go back with home key
2611 * - Go back with back key TODO: make this not go to workspace
2612 * - From all apps
2613 * - From workspace
2614 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2615 * - From all apps
2616 * - From the center workspace
2617 * - From another workspace
2618 */
2619
2620 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002621 * Zoom the camera out from the workspace to reveal 'toView'.
2622 * Assumes that the view to show is anchored at either the very top or very bottom
2623 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002624 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002625 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002626 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002627 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002628 mStateAnimation.cancel();
2629 mStateAnimation = null;
2630 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002631 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002632
Winson Chungf0ea4d32011-06-06 14:27:16 -07002633 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2634 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2635 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002636 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002637 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002638 final int startDelay =
2639 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002640
Michael Jurkab3e22d92011-10-31 15:58:33 -07002641 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002642
Michael Jurkad74c9842011-07-10 12:44:21 -07002643 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002644 Animator workspaceAnim =
2645 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002646
2647 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002648 toView.setScaleX(scale);
2649 toView.setScaleY(scale);
2650 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2651 scaleAnim.
2652 scaleX(1f).scaleY(1f).
2653 setDuration(duration).
2654 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002655
Winson Chungf0ea4d32011-06-06 14:27:16 -07002656 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002657 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002658 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002659 .ofFloat(toView, "alpha", 0f, 1f)
2660 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002661 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002662 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2663 @Override
2664 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002665 if (animation == null) {
2666 throw new RuntimeException("animation is null");
2667 }
Winson Chung70442722012-02-10 15:43:22 -08002668 float t = (Float) animation.getAnimatedValue();
2669 dispatchOnLauncherTransitionStep(fromView, t);
2670 dispatchOnLauncherTransitionStep(toView, t);
2671 }
2672 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002673
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002674 // toView should appear right at the end of the workspace shrink
2675 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002676 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002677 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002678 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002679
2680 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002681 boolean animationCancelled = false;
2682
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002683 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002684 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002685 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002686 // Prepare the position
2687 toView.setTranslationX(0.0f);
2688 toView.setTranslationY(0.0f);
2689 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002690 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002691 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002692 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002693 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002694 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2695 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002696
Daniel Sandlere4f98912013-06-25 15:13:26 -04002697 if (mWorkspace != null
2698 && !springLoaded
2699 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002700 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002701 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002702 if (!animationCancelled) {
2703 updateWallpaperVisibility(false);
2704 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002705
2706 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002707 if (mSearchDropTargetBar != null) {
2708 mSearchDropTargetBar.hideSearchBar(false);
2709 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002710 }
2711
Adam Cohencff6af82011-09-13 14:51:53 -07002712 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002713 public void onAnimationCancel(Animator animation) {
2714 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002715 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002716 });
2717
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002718 if (workspaceAnim != null) {
2719 mStateAnimation.play(workspaceAnim);
2720 }
Michael Jurka1899a362011-11-03 13:50:45 -07002721
2722 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002723
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002724 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2725 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002726
2727 // If any of the objects being animated haven't been measured/laid out
2728 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002729 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002730 (mWorkspace.getMeasuredWidth() == 0) ||
2731 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002732 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002733 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002734
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002735 final AnimatorSet stateAnimation = mStateAnimation;
2736 final Runnable startAnimRunnable = new Runnable() {
2737 public void run() {
2738 // Check that mStateAnimation hasn't changed while
2739 // we waited for a layout/draw pass
2740 if (mStateAnimation != stateAnimation)
2741 return;
2742 setPivotsForZoom(toView, scale);
2743 dispatchOnLauncherTransitionStart(fromView, animated, false);
2744 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002745 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002746 }
2747 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002748 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002749 final ViewTreeObserver observer = toView.getViewTreeObserver();
2750 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2751 public void onGlobalLayout() {
2752 startAnimRunnable.run();
2753 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2754 }
2755 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002756 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002757 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002758 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002759 } else {
2760 toView.setTranslationX(0.0f);
2761 toView.setTranslationY(0.0f);
2762 toView.setScaleX(1.0f);
2763 toView.setScaleY(1.0f);
2764 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002765 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002766
Daniel Sandlere4f98912013-06-25 15:13:26 -04002767 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002768 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002769
2770 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002771 if (mSearchDropTargetBar != null) {
2772 mSearchDropTargetBar.hideSearchBar(false);
2773 }
Michael Jurkaabded662011-03-04 12:06:57 -08002774 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002775 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002776 dispatchOnLauncherTransitionStart(fromView, animated, false);
2777 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002778 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002779 dispatchOnLauncherTransitionStart(toView, animated, false);
2780 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002781 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002782 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002783 }
2784
2785 /**
2786 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002787 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002788 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002789 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002790 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2791 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002792
Michael Jurkab3e22d92011-10-31 15:58:33 -07002793 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002794 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002795 mStateAnimation.cancel();
2796 mStateAnimation = null;
2797 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002798 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002799
Winson Chungf0ea4d32011-06-06 14:27:16 -07002800 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002801 final int fadeOutDuration =
2802 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002803 final float scaleFactor = (float)
2804 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2805 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002806 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002807 Animator workspaceAnim = null;
2808
2809 if (toState == State.WORKSPACE) {
2810 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2811 workspaceAnim = mWorkspace.getChangeStateAnimation(
2812 Workspace.State.NORMAL, animated, stagger);
2813 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2814 workspaceAnim = mWorkspace.getChangeStateAnimation(
2815 Workspace.State.SPRING_LOADED, animated);
2816 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002817
Michael Jurkab3e22d92011-10-31 15:58:33 -07002818 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002819 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002820 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002821 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002822 final LauncherViewPropertyAnimator scaleAnim =
2823 new LauncherViewPropertyAnimator(fromView);
2824 scaleAnim.
2825 scaleX(scaleFactor).scaleY(scaleFactor).
2826 setDuration(duration).
2827 setInterpolator(new Workspace.ZoomInInterpolator());
2828
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002829 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002830 .ofFloat(fromView, "alpha", 1f, 0f)
2831 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002832 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002833 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2834 @Override
2835 public void onAnimationUpdate(ValueAnimator animation) {
2836 float t = 1f - (Float) animation.getAnimatedValue();
2837 dispatchOnLauncherTransitionStep(fromView, t);
2838 dispatchOnLauncherTransitionStep(toView, t);
2839 }
2840 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002841
Michael Jurka2ecf9952012-06-18 12:52:28 -07002842 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002843
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002844 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2845 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002846 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002847
2848 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002849 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002850 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002851 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002852 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002853 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2854 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002855 if (onCompleteRunnable != null) {
2856 onCompleteRunnable.run();
2857 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002858 mAppsCustomizeContent.updateCurrentPageScroll();
2859 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002860 }
2861 });
2862
Winson Chungf0ea4d32011-06-06 14:27:16 -07002863 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002864 if (workspaceAnim != null) {
2865 mStateAnimation.play(workspaceAnim);
2866 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002867 dispatchOnLauncherTransitionStart(fromView, animated, true);
2868 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002869 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002870 } else {
2871 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002872 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002873 dispatchOnLauncherTransitionStart(fromView, animated, true);
2874 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002875 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002876 dispatchOnLauncherTransitionStart(toView, animated, true);
2877 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002878 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002879 }
2880
Michael Jurkae326f182011-11-21 14:05:46 -08002881 @Override
2882 public void onTrimMemory(int level) {
2883 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002884 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002885 mAppsCustomizeTabHost.onTrimMemory();
2886 }
2887 }
2888
Winson Chung18f41f82012-05-09 13:28:10 -07002889 @Override
2890 public void onWindowFocusChanged(boolean hasFocus) {
2891 if (!hasFocus) {
2892 // When another window occludes launcher (like the notification shade, or recents),
2893 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2894 updateWallpaperVisibility(true);
2895 } else {
2896 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002897 mWorkspace.postDelayed(new Runnable() {
2898 @Override
2899 public void run() {
2900 disableWallpaperIfInAllApps();
2901 }
2902 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002903 }
2904 }
2905
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002906 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002907 showWorkspace(animated, null);
2908 }
2909
2910 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002911 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002912 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002913 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002914 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002915
Winson Chungc7d2b602012-05-16 17:02:20 -07002916 // Show the search bar (only animate if we were showing the drop target bar in spring
2917 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002918 if (mSearchDropTargetBar != null) {
2919 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2920 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002921
Michael Jurkab737ee62011-11-15 15:57:22 -08002922 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002923 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002924
2925 // Set focus to the AppsCustomize button
2926 if (mAllAppsButton != null) {
2927 mAllAppsButton.requestFocus();
2928 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002929 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002930
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002931 // Change the state *after* we've called all the transition code
2932 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002933
Winson Chung5fb63472011-02-02 17:03:37 -08002934 // Resume the auto-advance of widgets
2935 mUserPresent = true;
2936 updateRunning();
2937
alanv1d4fde62012-10-17 13:15:47 -07002938 // Send an accessibility event to announce the context change
2939 getWindow().getDecorView()
2940 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002941
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002942 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002943 }
2944
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002945 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002946 }
2947
Michael Jurkab3e22d92011-10-31 15:58:33 -07002948 void showAllApps(boolean animated) {
2949 if (mState != State.WORKSPACE) return;
2950
2951 showAppsCustomizeHelper(animated, false);
2952 mAppsCustomizeTabHost.requestFocus();
2953
Michael Jurkab3e22d92011-10-31 15:58:33 -07002954 // Change the state *after* we've called all the transition code
2955 mState = State.APPS_CUSTOMIZE;
2956
2957 // Pause the auto-advance of widgets until we are out of AllApps
2958 mUserPresent = false;
2959 updateRunning();
2960 closeFolder();
2961
2962 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002963 getWindow().getDecorView()
2964 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002965 }
2966
Adam Cohen7777d962011-08-18 18:58:38 -07002967 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002968 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002969 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002970 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002971 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002972 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002973 }
Adam Cohen7777d962011-08-18 18:58:38 -07002974
Adam Cohened66b2b2012-01-23 17:28:51 -08002975 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2976 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002977 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2978
Winson Chunge7a03942011-08-05 15:05:12 -07002979 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002980 @Override
2981 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002982 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002983 // Before we show workspace, hide all apps again because
2984 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2985 // clean up our state transition functions
2986 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002987 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002988 } else {
2989 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002990 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002991 }
2992 }, (extendedDelay ?
2993 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2994 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2995 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002996
Michael Jurkad3ef3062010-11-23 16:23:58 -08002997 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002998 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002999 final boolean animated = true;
3000 final boolean springLoaded = true;
3001 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003002 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003003 }
3004 // Otherwise, we are not in spring loaded mode, so don't do anything.
3005 }
3006
Michael Jurkab737ee62011-11-15 15:57:22 -08003007 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003008 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003009 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003010 }
3011 }
3012
3013 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003014 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003015 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003016 if (mDividerAnimator != null) {
3017 mDividerAnimator.cancel();
3018 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003019 mDividerAnimator = null;
3020 }
3021 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003022 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003023 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003024 int duration = 0;
3025 if (mSearchDropTargetBar != null) {
3026 duration = mSearchDropTargetBar.getTransitionInDuration();
3027 }
3028 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003029 mDividerAnimator.start();
3030 }
3031 }
3032 }
3033
Michael Jurkab3e22d92011-10-31 15:58:33 -07003034 void lockAllApps() {
3035 // TODO
3036 }
3037
3038 void unlockAllApps() {
3039 // TODO
3040 }
3041
Winson Chungf0ea4d32011-06-06 14:27:16 -07003042 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003043 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003044 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003045 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003046 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003047 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003048 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003049 int duration = 0;
3050 if (mSearchDropTargetBar != null) {
3051 duration = mSearchDropTargetBar.getTransitionInDuration();
3052 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003053 mHotseat.animate().alpha(1f).setDuration(duration);
3054 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003055 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003056 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003057 }
3058 }
3059 }
3060
3061 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003062 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003063 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003064 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003065 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003066 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003067 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003068 int duration = 0;
3069 if (mSearchDropTargetBar != null) {
3070 duration = mSearchDropTargetBar.getTransitionOutDuration();
3071 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003072 mHotseat.animate().alpha(0f).setDuration(duration);
3073 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003074 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003075 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003076 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003077 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003078 }
3079
Patrick Dubroy5f445422011-02-18 14:35:21 -08003080 /**
3081 * Add an item from all apps or customize onto the given workspace screen.
3082 * If layout is null, add to the current screen.
3083 */
3084 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003085 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003086 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003087 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003088 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003089
Winson Chungdff8ebb2011-09-08 17:25:31 -07003090 /** Maps the current orientation to an index for referencing orientation correct global icons */
3091 private int getCurrentOrientationIndexForGlobalIcons() {
3092 // default - 0, landscape - 1
3093 switch (getResources().getConfiguration().orientation) {
3094 case Configuration.ORIENTATION_LANDSCAPE:
3095 return 1;
3096 default:
3097 return 0;
3098 }
3099 }
3100
Michael Jurkaae65ee32012-04-30 11:45:54 -07003101 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003102 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003103 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003104 // Look for the toolbar icon specified in the activity meta-data
3105 Bundle metaData = packageManager.getActivityInfo(
3106 activityName, PackageManager.GET_META_DATA).metaData;
3107 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003108 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003109 if (iconResId != 0) {
3110 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003111 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003112 }
3113 }
3114 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003115 // This can happen if the activity defines an invalid drawable
3116 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3117 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003118 } catch (Resources.NotFoundException nfe) {
3119 // This can happen if the activity defines an invalid drawable
3120 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3121 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003122 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003123 return null;
3124 }
3125
3126 // if successful in getting icon, return it; otherwise, set button to use default drawable
3127 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003128 int buttonId, ComponentName activityName, int fallbackDrawableId,
3129 String toolbarResourceName) {
3130 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003131 Resources r = getResources();
3132 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3133 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003134
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003135 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003136 // If we were unable to find the icon via the meta-data, use a generic one
3137 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003138 toolbarIcon = r.getDrawable(fallbackDrawableId);
3139 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003140 if (button != null) {
3141 button.setCompoundDrawables(toolbarIcon, null, null, null);
3142 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003143 return null;
3144 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003145 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003146 if (button != null) {
3147 button.setCompoundDrawables(toolbarIcon, null, null, null);
3148 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003149 return toolbarIcon.getConstantState();
3150 }
3151 }
3152
3153 // if successful in getting icon, return it; otherwise, set button to use default drawable
3154 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003155 int buttonId, ComponentName activityName, int fallbackDrawableId,
3156 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003157 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003158 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003159
Michael Jurka19e0fc52011-07-22 18:00:21 -07003160 if (button != null) {
3161 // If we were unable to find the icon via the meta-data, use a
3162 // generic one
3163 if (toolbarIcon == null) {
3164 button.setImageResource(fallbackDrawableId);
3165 } else {
3166 button.setImageDrawable(toolbarIcon);
3167 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003168 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003169
3170 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3171
Michael Jurka0423dcf2010-10-05 14:56:18 -07003172 }
3173
Winson Chung1b884092012-06-08 17:13:02 -07003174 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003175 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003176 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003177 }
3178
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003179 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003180 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003181 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003182 }
3183
Winson Chungbb185bd2011-11-21 12:31:42 -08003184 private void invalidatePressedFocusedStates(View container, View button) {
3185 if (container instanceof HolographicLinearLayout) {
3186 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3187 layout.invalidatePressedFocusedStates();
3188 } else if (button instanceof HolographicImageView) {
3189 HolographicImageView view = (HolographicImageView) button;
3190 view.invalidatePressedFocusedStates();
3191 }
3192 }
3193
Winson Chungc51db6a2011-10-05 11:44:49 -07003194 private boolean updateGlobalSearchIcon() {
3195 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003196 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003197 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003198 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003199
Winson Chung1cad91e2011-05-25 17:41:01 -07003200 final SearchManager searchManager =
3201 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3202 ComponentName activityName = searchManager.getGlobalSearchActivity();
3203 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003204 int coi = getCurrentOrientationIndexForGlobalIcons();
3205 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003206 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3207 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3208 if (sGlobalSearchIcon[coi] == null) {
3209 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3210 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3211 TOOLBAR_ICON_METADATA_NAME);
3212 }
3213
Winson Chungc51db6a2011-10-05 11:44:49 -07003214 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3215 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003216 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003217 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003218 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003219 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003220 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3221 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3222 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003223 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003224 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003225 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003226 }
3227 }
3228
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003229 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003230 final View searchButtonContainer = findViewById(R.id.search_button_container);
3231 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003232 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003233 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003234 }
3235
Winson Chungc51db6a2011-10-05 11:44:49 -07003236 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003237 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003238 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003239
Winson Chungc51db6a2011-10-05 11:44:49 -07003240 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003241 final SearchManager searchManager =
3242 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3243 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3244
3245 ComponentName activityName = null;
3246 if (globalSearchActivity != null) {
3247 // Check if the global search activity handles voice search
3248 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3249 intent.setPackage(globalSearchActivity.getPackageName());
3250 activityName = intent.resolveActivity(getPackageManager());
3251 }
3252
3253 if (activityName == null) {
3254 // Fallback: check if an activity other than the global search activity
3255 // resolves this
3256 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3257 activityName = intent.resolveActivity(getPackageManager());
3258 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003259 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003260 int coi = getCurrentOrientationIndexForGlobalIcons();
3261 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003262 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3263 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3264 if (sVoiceSearchIcon[coi] == null) {
3265 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3266 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3267 TOOLBAR_ICON_METADATA_NAME);
3268 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003269 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003270 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003271 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003272 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003273 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003274 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003275 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003276 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003277 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003278 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003279 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003280 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003281
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003282 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003283 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3284 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003285 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003286 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003287 }
3288
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003289 public void setVoiceButtonProxyVisible(boolean visible) {
3290 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3291 if (voiceButtonProxy != null) {
3292 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3293 }
3294 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003295 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003296 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003297 */
3298 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003299 final View marketButton = findViewById(R.id.market_button);
3300 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3301 // Find the app market activity by resolving an intent.
3302 // (If multiple app markets are installed, it will return the ResolverActivity.)
3303 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003304 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003305 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003306 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003307 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003308 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3309 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003310 marketButton.setVisibility(View.VISIBLE);
3311 } else {
3312 // We should hide and disable the view so that we don't try and restore the visibility
3313 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3314 marketButton.setVisibility(View.GONE);
3315 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003316 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003317 }
3318
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003319 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003320 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3321 Resources r = getResources();
3322 Drawable marketIconDrawable = d.newDrawable(r);
3323 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3324 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3325 marketIconDrawable.setBounds(0, 0, w, h);
3326
3327 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003328 }
3329
Michael Jurkad7c28052012-04-27 15:43:36 -07003330 @Override
3331 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003332 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003333 final List<CharSequence> text = event.getText();
3334 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003335 // Populate event with a fake title based on the current state.
3336 if (mState == State.APPS_CUSTOMIZE) {
3337 text.add(getString(R.string.all_apps_button_label));
3338 } else {
3339 text.add(getString(R.string.all_apps_home_button_label));
3340 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003341 return result;
3342 }
3343
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003344 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003345 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003346 */
3347 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3348 @Override
3349 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003350 closeSystemDialogs();
3351 }
3352 }
3353
3354 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003355 * Receives notifications whenever the appwidgets are reset.
3356 */
3357 private class AppWidgetResetObserver extends ContentObserver {
3358 public AppWidgetResetObserver() {
3359 super(new Handler());
3360 }
3361
3362 @Override
3363 public void onChange(boolean selfChange) {
3364 onAppWidgetReset();
3365 }
3366 }
3367
3368 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003369 * If the activity is currently paused, signal that we need to run the passed Runnable
3370 * in onResume.
3371 *
3372 * This needs to be called from incoming places where resources might have been loaded
3373 * while we are paused. That is becaues the Configuration might be wrong
3374 * when we're not running, and if it comes back to what it was when we
3375 * were paused, we are not restarted.
3376 *
3377 * Implementation of the method from LauncherModel.Callbacks.
3378 *
3379 * @return true if we are currently paused. The caller might be able to
3380 * skip some work in that case since we will come back again.
3381 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003382 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003383 if (mPaused) {
3384 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003385 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003386 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003387 }
3388 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003389 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003390 return true;
3391 } else {
3392 return false;
3393 }
3394 }
3395
Michael Jurkac402cd92013-05-20 15:49:32 +02003396 private boolean waitUntilResume(Runnable run) {
3397 return waitUntilResume(run, false);
3398 }
3399
Michael Jurka1e2f4652013-07-08 18:03:46 -07003400 public void addOnResumeCallback(Runnable run) {
3401 mBindOnResumeCallbacks.add(run);
3402 }
3403
3404 public void removeOnResumeCallback(Runnable run) {
3405 mBindOnResumeCallbacks.remove(run);
3406 }
3407
Michael Jurka7607c2f2013-04-03 14:33:19 -07003408 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003409 * If the activity is currently paused, signal that we need to re-run the loader
3410 * in onResume.
3411 *
3412 * This needs to be called from incoming places where resources might have been loaded
3413 * while we are paused. That is becaues the Configuration might be wrong
3414 * when we're not running, and if it comes back to what it was when we
3415 * were paused, we are not restarted.
3416 *
3417 * Implementation of the method from LauncherModel.Callbacks.
3418 *
3419 * @return true if we are currently paused. The caller might be able to
3420 * skip some work in that case since we will come back again.
3421 */
3422 public boolean setLoadOnResume() {
3423 if (mPaused) {
3424 Log.i(TAG, "setLoadOnResume");
3425 mOnResumeNeedsLoad = true;
3426 return true;
3427 } else {
3428 return false;
3429 }
3430 }
3431
3432 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003434 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003436 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003437 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003438 } else {
3439 return SCREEN_COUNT / 2;
3440 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003441 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003442
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 /**
3444 * Refreshes the shortcuts shown on the workspace.
3445 *
3446 * Implementation of the method from LauncherModel.Callbacks.
3447 */
3448 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003449 // If we're starting binding all over again, clear any bind calls we'd postponed in
3450 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3451 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003452 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003453
Michael Jurka7607c2f2013-04-03 14:33:19 -07003454 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003455 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003456 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003457 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 int count = workspace.getChildCount();
3459 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003460 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003461 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3462 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003463 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003464 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003465 if (mHotseat != null) {
3466 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003467 }
3468 }
3469
Adam Cohendcd297f2013-06-18 13:13:40 -07003470 @Override
3471 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003472 bindAddScreens(orderedScreenIds);
3473 mWorkspace.addExtraEmptyScreen();
3474 }
3475
3476 @Override
3477 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003478 int count = orderedScreenIds.size();
3479 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003480 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003481 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003482 }
3483
Adam Cohen39a06042013-07-19 14:30:12 -07003484 private boolean shouldShowWeightWatcher() {
3485 String spKey = LauncherAppState.getSharedPreferencesKey();
3486 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3487 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3488
3489 return show;
3490 }
3491
3492 private void toggleShowWeightWatcher() {
3493 String spKey = LauncherAppState.getSharedPreferencesKey();
3494 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3495 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3496
3497 show = !show;
3498
3499 SharedPreferences.Editor editor = sp.edit();
3500 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3501 editor.commit();
3502
3503 if (mWeightWatcher != null) {
3504 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3505 }
3506 }
3507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003508 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003509 * Bind the items start-end from the list.
3510 *
3511 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003512 */
Winson Chung64359a52013-07-08 17:17:08 -07003513 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3514 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003515 if (waitUntilResume(new Runnable() {
3516 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003517 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003518 }
3519 })) {
3520 return;
3521 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003522
Winson Chungf0c6ae02012-03-21 16:10:31 -07003523 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3524 Set<String> newApps = new HashSet<String>();
3525 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3526
Winson Chung64359a52013-07-08 17:17:08 -07003527 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3528 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003529 Workspace workspace = mWorkspace;
3530 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003531 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003532
3533 // Short circuit if we are loading dock items for a configuration which has no dock
3534 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3535 mHotseat == null) {
3536 continue;
3537 }
3538
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 switch (item.itemType) {
3540 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3541 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003542 ShortcutInfo info = (ShortcutInfo) item;
3543 String uri = info.intent.toUri(0).toString();
3544 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003545
Winson Chung64359a52013-07-08 17:17:08 -07003546 /*
3547 * TODO: FIX collision case
3548 */
Winson Chungce376632013-07-11 16:12:41 -07003549 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3550 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3551 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3552 throw new RuntimeException("OCCUPIED");
3553 }
Winson Chung64359a52013-07-08 17:17:08 -07003554 }
3555
Adam Cohendcd297f2013-06-18 13:13:40 -07003556 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3557 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003558 boolean animateIconUp = false;
3559 synchronized (newApps) {
3560 if (newApps.contains(uri)) {
3561 animateIconUp = newApps.remove(uri);
3562 }
3563 }
Winson Chung64359a52013-07-08 17:17:08 -07003564 if (forceAnimateIcons) {
3565 // Animate all the applications up now
3566 shortcut.setAlpha(0f);
3567 shortcut.setScaleX(0f);
3568 shortcut.setScaleY(0f);
3569 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3570 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003571 // Prepare the view to be animated up
3572 shortcut.setAlpha(0f);
3573 shortcut.setScaleX(0f);
3574 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003575 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003576 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3577 mNewShortcutAnimateViews.add(shortcut);
3578 }
3579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003581 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003582 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003583 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003584 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003585 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3586 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003587 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003588 default:
3589 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003591 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003592
Winson Chung64359a52013-07-08 17:17:08 -07003593 if (forceAnimateIcons) {
3594 // We post the animation slightly delayed to prevent slowdowns when we are loading
3595 // right after we return to launcher.
3596 mWorkspace.postDelayed(new Runnable() {
3597 public void run() {
3598 anim.playTogether(bounceAnims);
3599 anim.start();
3600 }
3601 }, NEW_APPS_ANIMATION_DELAY);
3602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003604 }
3605
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 /**
3607 * Implementation of the method from LauncherModel.Callbacks.
3608 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003609 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3610 if (waitUntilResume(new Runnable() {
3611 public void run() {
3612 bindFolders(folders);
3613 }
3614 })) {
3615 return;
3616 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003617 sFolders.clear();
3618 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003619 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620
3621 /**
3622 * Add the views for a widget to the workspace.
3623 *
3624 * Implementation of the method from LauncherModel.Callbacks.
3625 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003626 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3627 if (waitUntilResume(new Runnable() {
3628 public void run() {
3629 bindAppWidget(item);
3630 }
3631 })) {
3632 return;
3633 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003634
Daniel Sandler843e8602010-06-07 14:59:01 -04003635 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3636 if (DEBUG_WIDGETS) {
3637 Log.d(TAG, "bindAppWidget: " + item);
3638 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639 final Workspace workspace = mWorkspace;
3640
3641 final int appWidgetId = item.appWidgetId;
3642 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003643 if (DEBUG_WIDGETS) {
3644 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3645 }
3646
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3648
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003650 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651
Adam Cohendcd297f2013-06-18 13:13:40 -07003652 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003654 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3655
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 workspace.requestLayout();
3657
Daniel Sandler843e8602010-06-07 14:59:01 -04003658 if (DEBUG_WIDGETS) {
3659 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3660 + (SystemClock.uptimeMillis()-start) + "ms");
3661 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003662 }
3663
Adam Cohen1462de32012-07-24 22:34:36 -07003664 public void onPageBoundSynchronously(int page) {
3665 mSynchronouslyBoundPages.add(page);
3666 }
3667
Joe Onorato9c1289c2009-08-17 11:03:03 -04003668 /**
3669 * Callback saying that there aren't any more items to bind.
3670 *
3671 * Implementation of the method from LauncherModel.Callbacks.
3672 */
Adam Cohene25af792013-06-06 23:08:25 -07003673 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003674 if (waitUntilResume(new Runnable() {
3675 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003676 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003677 }
3678 })) {
3679 return;
3680 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003681 if (mSavedState != null) {
3682 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003683 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003684 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003685 mSavedState = null;
3686 }
3687
Adam Cohen1462de32012-07-24 22:34:36 -07003688 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003690 // If we received the result of any pending adds while the loader was running (e.g. the
3691 // widget configuration forced an orientation change), process them now.
3692 for (int i = 0; i < sPendingAddList.size(); i++) {
3693 completeAdd(sPendingAddList.get(i));
3694 }
3695 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696
Winson Chungc51db6a2011-10-05 11:44:49 -07003697 // Update the market app icon as necessary (the other icons will be managed in response to
3698 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003699 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003700
Winson Chungf0c6ae02012-03-21 16:10:31 -07003701 // Animate up any icons as necessary
3702 if (mVisible || mWorkspaceLoading) {
3703 Runnable newAppsRunnable = new Runnable() {
3704 @Override
3705 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003706 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003707 }
3708 };
Winson Chunga2413752012-04-03 14:22:34 -07003709
Adam Cohendcd297f2013-06-18 13:13:40 -07003710 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3711 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003712 if (canRunNewAppsAnimation()) {
3713 // If the user has not interacted recently, then either snap to the new page to show
3714 // the new-apps animation or just run them if they are to appear on the current page
3715 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003716 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003717 } else {
3718 runNewAppsAnimation(false);
3719 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003720 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003721 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003722 // are on another page (or just normally if they are added to the current page)
3723 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003724 }
3725 }
3726
3727 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003728 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003729 mWorkspace.stripDuplicateApps();
3730 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003731 }
Adam Cohen99894d92013-06-14 11:22:59 -07003732
Adam Cohen66a01fd2013-06-11 12:48:00 -07003733 mWorkspace.post(new Runnable() {
3734 @Override
3735 public void run() {
3736 onFinishBindingItems();
3737 }
3738 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003739 }
3740
Winson Chunga2413752012-04-03 14:22:34 -07003741 private boolean canRunNewAppsAnimation() {
3742 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3743 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3744 }
3745
Winson Chungc9168342013-06-26 14:54:55 -07003746 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3747 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3748 PropertyValuesHolder.ofFloat("alpha", 1f),
3749 PropertyValuesHolder.ofFloat("scaleX", 1f),
3750 PropertyValuesHolder.ofFloat("scaleY", 1f));
3751 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3752 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3753 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3754 return bounceAnim;
3755 }
3756
Winson Chungf0c6ae02012-03-21 16:10:31 -07003757 /**
3758 * Runs a new animation that scales up icons that were added while Launcher was in the
3759 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003760 *
3761 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003762 */
Winson Chunga2413752012-04-03 14:22:34 -07003763 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003764 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003765 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003766
3767 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003768 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3769 @Override
3770 public int compare(View a, View b) {
3771 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3772 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3773 int cellCountX = LauncherModel.getCellCountX();
3774 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3775 }
3776 });
Winson Chunga2413752012-04-03 14:22:34 -07003777
3778 // Animate each of the views in place (or show them immediately if requested)
3779 if (immediate) {
3780 for (View v : mNewShortcutAnimateViews) {
3781 v.setAlpha(1f);
3782 v.setScaleX(1f);
3783 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003784 }
Winson Chunga2413752012-04-03 14:22:34 -07003785 } else {
3786 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3787 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003788 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003789 }
3790 anim.playTogether(bounceAnims);
3791 anim.addListener(new AnimatorListenerAdapter() {
3792 @Override
3793 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003794 if (mWorkspace != null) {
3795 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3796 }
Winson Chunga2413752012-04-03 14:22:34 -07003797 }
3798 });
3799 anim.start();
3800 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003801
3802 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003803 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003804 mNewShortcutAnimateViews.clear();
3805 new Thread("clearNewAppsThread") {
3806 public void run() {
3807 mSharedPrefs.edit()
3808 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3809 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3810 .commit();
3811 }
3812 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003813 }
3814
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003815 @Override
3816 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003817 boolean searchVisible = updateGlobalSearchIcon();
3818 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003819 if (mSearchDropTargetBar != null) {
3820 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3821 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003822 }
3823
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003824 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003825 * Add the icons for all apps.
3826 *
3827 * Implementation of the method from LauncherModel.Callbacks.
3828 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003829 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003830 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3831 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003832 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003833 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003834 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 }
3836
3837 /**
3838 * A package was updated.
3839 *
3840 * Implementation of the method from LauncherModel.Callbacks.
3841 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003842 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3843 if (waitUntilResume(new Runnable() {
3844 public void run() {
3845 bindAppsUpdated(apps);
3846 }
3847 })) {
3848 return;
3849 }
3850
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003851 if (mWorkspace != null) {
3852 mWorkspace.updateShortcuts(apps);
3853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003854 }
3855
3856 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003857 * A package was uninstalled. We take both the super set of packageNames
3858 * in addition to specific applications to remove, the reason being that
3859 * this can be called when a package is updated as well. In that scenario,
3860 * we only remove specific components from the workspace, where as
3861 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003862 *
3863 * Implementation of the method from LauncherModel.Callbacks.
3864 */
Winson Chung83892cc2013-05-01 16:53:33 -07003865 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3866 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003867 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003868 if (waitUntilResume(new Runnable() {
3869 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003870 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003871 }
3872 })) {
3873 return;
3874 }
3875
Winson Chung64359a52013-07-08 17:17:08 -07003876 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003877 mWorkspace.removeItemsByPackageName(packageNames);
3878 } else {
3879 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003880 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003881
Winson Chunga1820962011-10-03 16:31:06 -07003882 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003883 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 }
Joe Onoratobe386092009-11-17 17:32:16 -08003885
3886 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003887 * A number of packages were updated.
3888 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003889 private ArrayList<Object> mWidgetsAndShortcuts;
3890 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003891 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003892 bindPackagesUpdated(mWidgetsAndShortcuts);
3893 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003894 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003895 };
3896
3897 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3898 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3899 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003900 return;
3901 }
3902
Winson Chung64359a52013-07-08 17:17:08 -07003903 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003904 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003905 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003906 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003907 }
3908
Winson Chung400438b2011-01-16 17:53:48 -08003909 private int mapConfigurationOriActivityInfoOri(int configOri) {
3910 final Display d = getWindowManager().getDefaultDisplay();
3911 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3912 switch (d.getRotation()) {
3913 case Surface.ROTATION_0:
3914 case Surface.ROTATION_180:
3915 // We are currently in the same basic orientation as the natural orientation
3916 naturalOri = configOri;
3917 break;
3918 case Surface.ROTATION_90:
3919 case Surface.ROTATION_270:
3920 // We are currently in the other basic orientation to the natural orientation
3921 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3922 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3923 break;
3924 }
3925
3926 int[] oriMap = {
3927 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3928 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3929 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3930 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3931 };
3932 // Since the map starts at portrait, we need to offset if this device's natural orientation
3933 // is landscape.
3934 int indexOffset = 0;
3935 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3936 indexOffset = 1;
3937 }
3938 return oriMap[(d.getRotation() + indexOffset) % 4];
3939 }
Adam Cohen446e9402011-09-15 18:21:21 -07003940
Winson Chung4b919f82012-05-01 10:44:08 -07003941 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003942 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003943 getResources().getBoolean(R.bool.allow_rotation);
3944 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003945 }
Winson Chung4b919f82012-05-01 10:44:08 -07003946 public void lockScreenOrientation() {
3947 if (isRotationEnabled()) {
3948 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3949 .getConfiguration().orientation));
3950 }
3951 }
3952 public void unlockScreenOrientation(boolean immediate) {
3953 if (isRotationEnabled()) {
3954 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003955 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003956 } else {
3957 mHandler.postDelayed(new Runnable() {
3958 public void run() {
3959 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3960 }
3961 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003962 }
Winson Chung4b919f82012-05-01 10:44:08 -07003963 }
Winson Chung400438b2011-01-16 17:53:48 -08003964 }
3965
Winson Chung82f55532011-08-09 14:14:23 -07003966 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003967 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003968 if (DISABLE_CLINGS) {
3969 return false;
3970 }
3971
Brett Chabot2a9e2282011-08-23 15:03:13 -07003972 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003973 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003974
Michael Jurkae233a8b2013-03-19 13:49:20 +01003975 // Restricted secondary users (child mode) will potentially have very few apps
3976 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003977// boolean supportsLimitedUsers =
3978// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3979// Account[] accounts = AccountManager.get(this).getAccounts();
3980// if (supportsLimitedUsers && accounts.length == 0) {
3981// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3982// Bundle restrictions = um.getUserRestrictions();
3983// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3984// return false;
3985// }
3986// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003987 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003988 }
Michael Jurka22143132012-10-04 17:42:38 +02003989
Winson Chung7d7541e2011-09-16 20:14:36 -07003990 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003991 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003992 if (cling != null) {
3993 cling.init(this, positionData);
3994 cling.setVisibility(View.VISIBLE);
3995 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3996 if (animate) {
3997 cling.buildLayer();
3998 cling.setAlpha(0f);
3999 cling.animate()
4000 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004001 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004002 .setDuration(SHOW_CLING_DURATION)
4003 .setStartDelay(delay)
4004 .start();
4005 } else {
4006 cling.setAlpha(1f);
4007 }
Michael Jurka22143132012-10-04 17:42:38 +02004008 cling.setFocusableInTouchMode(true);
4009 cling.post(new Runnable() {
4010 public void run() {
4011 cling.setFocusable(true);
4012 cling.requestFocus();
4013 }
4014 });
4015 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4016 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004017 }
4018 return cling;
4019 }
Michael Jurka22143132012-10-04 17:42:38 +02004020
Winson Chung7d7541e2011-09-16 20:14:36 -07004021 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004022 // To catch cases where siblings of top-level views are made invisible, just check whether
4023 // the cling is directly set to GONE before dismissing it.
4024 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004025 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004026 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004027 anim.addListener(new AnimatorListenerAdapter() {
4028 public void onAnimationEnd(Animator animation) {
4029 cling.setVisibility(View.GONE);
4030 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004031 // We should update the shared preferences on a background thread
4032 new Thread("dismissClingThread") {
4033 public void run() {
4034 SharedPreferences.Editor editor = mSharedPrefs.edit();
4035 editor.putBoolean(flag, true);
4036 editor.commit();
4037 }
4038 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004039 };
4040 });
4041 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004042 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004043 }
4044 }
Michael Jurka22143132012-10-04 17:42:38 +02004045
Winson Chung9d9d74f2011-09-19 11:49:12 -07004046 private void removeCling(int id) {
4047 final View cling = findViewById(id);
4048 if (cling != null) {
4049 final ViewGroup parent = (ViewGroup) cling.getParent();
4050 parent.post(new Runnable() {
4051 @Override
4052 public void run() {
4053 parent.removeView(cling);
4054 }
4055 });
Michael Jurka22143132012-10-04 17:42:38 +02004056 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004057 }
4058 }
Michael Jurka974c3862012-05-22 22:00:31 -07004059
4060 private boolean skipCustomClingIfNoAccounts() {
4061 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4062 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4063 if (customCling) {
4064 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004065 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004066 Account[] accounts = am.getAccountsByType("com.google");
4067 return accounts.length == 0;
4068 }
4069 return false;
4070 }
4071
Winson Chung7d7541e2011-09-16 20:14:36 -07004072 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004073 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004074 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004075 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4076 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004077 // If we're not using the default workspace layout, replace workspace cling
4078 // with a custom workspace cling (usually specified in an overlay)
4079 // For now, only do this on tablets
4080 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004081 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004082 // Use a custom cling
4083 View cling = findViewById(R.id.workspace_cling);
4084 ViewGroup clingParent = (ViewGroup) cling.getParent();
4085 int clingIndex = clingParent.indexOfChild(cling);
4086 clingParent.removeViewAt(clingIndex);
4087 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4088 clingParent.addView(customCling, clingIndex);
4089 customCling.setId(R.id.workspace_cling);
4090 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004091 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004092 } else {
4093 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004094 }
4095 }
4096 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004097 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004098 if (isClingsEnabled() &&
4099 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004100 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004101 } else {
4102 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004103 }
4104 }
4105 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004106 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004107 if (isClingsEnabled() &&
4108 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4109 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004110 } else {
4111 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004112 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004113 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004114 }
4115 public boolean isFolderClingVisible() {
4116 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004117 if (cling != null) {
4118 return cling.getVisibility() == View.VISIBLE;
4119 }
4120 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004121 }
Winson Chung82f55532011-08-09 14:14:23 -07004122 public void dismissWorkspaceCling(View v) {
4123 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004125 }
4126 public void dismissAllAppsCling(View v) {
4127 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004128 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4129 }
4130 public void dismissFolderCling(View v) {
4131 Cling cling = (Cling) findViewById(R.id.folder_cling);
4132 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004133 }
4134
Winson Chung80baf5a2010-08-09 16:03:15 -07004135 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004136 * Prints out out state for debugging.
4137 */
4138 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004139 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004140 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004141 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4142 Log.d(TAG, "mRestoring=" + mRestoring);
4143 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4144 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004145 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004146 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004147
Winson Chung785d2eb2011-04-14 16:08:02 -07004148 if (mAppsCustomizeContent != null) {
4149 mAppsCustomizeContent.dumpState();
4150 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004151 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004152 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004153
4154 @Override
4155 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4156 super.dump(prefix, fd, writer, args);
4157 writer.println(" ");
4158 writer.println("Debug logs: ");
4159 for (int i = 0; i < sDumpLogs.size(); i++) {
4160 writer.println(" " + sDumpLogs.get(i));
4161 }
4162 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004163
4164 public static void dumpDebugLogsToConsole() {
4165 Log.d(TAG, "");
4166 Log.d(TAG, "*********************");
4167 Log.d(TAG, "Launcher debug logs: ");
4168 for (int i = 0; i < sDumpLogs.size(); i++) {
4169 Log.d(TAG, " " + sDumpLogs.get(i));
4170 }
4171 Log.d(TAG, "*********************");
4172 Log.d(TAG, "");
4173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004174}
Michael Jurka2763be32011-02-24 11:19:57 -08004175
Michael Jurkaabded662011-03-04 12:06:57 -08004176interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004177 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004178 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004179 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004180 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004181 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004182}