blob: ce4a78c3f9a40f03f6b7eff49fdfa9391c98485a [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;
Winson Chung5f8afe62013-08-12 16:19:28 -070053import android.graphics.Point;
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;
Winson Chung892c74d2013-08-22 16:15:50 -070071import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
80import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070090import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070091import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.FileNotFoundException;
106import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700107import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Collection;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700111import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113/**
114 * Default launcher application.
115 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100116public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700117 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700118 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800119 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700120 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Daniel Sandlerf061f822013-06-27 13:59:36 -0400122 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400123 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700124 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400125 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700126
Winson Chung70d72102011-08-12 11:24:35 -0700127 private static final int MENU_GROUP_WALLPAPER = 1;
128 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
129 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700130 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
131 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurka8b805b12012-04-18 14:23:14 -0700140 private static final int REQUEST_BIND_APPWIDGET = 11;
141
Mathew Inwood876a8462013-06-14 14:12:41 +0100142 /**
143 * IntentStarter uses request codes starting with this. This must be greater than all activity
144 * request codes used internally.
145 */
146 protected static final int REQUEST_LAST = 100;
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
149
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800150 static final int SCREEN_COUNT = 5;
151 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Romain Guy98d01652009-06-30 16:21:04 -0700153 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800154 // To turn on these properties, type
155 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
156 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
157 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Winson Chung2672ff92012-05-04 16:22:30 -0700159 // The Intent extra that defines whether to ignore the launch animation
160 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400161 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: int
164 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700165 // Type: int
166 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700168 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
169 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
171 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700172 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: boolean
176 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
177 // Type: long
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700179 // Type: int
180 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
183 // Type: parcelable
184 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100186 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700187 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100188 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700189 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700191
Adam Cohen39a06042013-07-19 14:30:12 -0700192 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700193 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700194
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700195 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700196 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700197 private State mState = State.WORKSPACE;
198 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700201 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
202 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700203 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700204 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800207 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Winson Chunga2413752012-04-03 14:22:34 -0700209 // How long to wait before the new-shortcut animation automatically pans the workspace
210 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700211 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700212
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800213 private final BroadcastReceiver mCloseSystemDialogsReceiver
214 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800215 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private LayoutInflater mInflater;
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700220 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800221 private DragLayer mDragLayer;
222 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700223 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private AppWidgetManager mAppWidgetManager;
226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Michael Jurkac9d95c52011-08-29 14:03:34 -0700228 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700229 private AppWidgetProviderInfo mPendingAddWidgetInfo;
230
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231 private int[] mTmpAddItemCellCoordinates = new int[2];
232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private FolderInfo mFolderInfo;
234
Winson Chung3d503fb2011-07-13 17:25:49 -0700235 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700236 private View mOverviewPanel;
237
Michael Jurka838a4ca2011-02-07 13:33:06 -0800238 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700239
Winson Chungc51db6a2011-10-05 11:44:49 -0700240 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700241 private AppsCustomizeTabHost mAppsCustomizeTabHost;
242 private AppsCustomizePagedView mAppsCustomizeContent;
243 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700246 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
247 // scroll issues (because the workspace may not have been measured yet) and extra work.
248 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
249 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250
251 private SpannableStringBuilder mDefaultKeySsb = null;
252
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253 private boolean mWorkspaceLoading = true;
254
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800255 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 private boolean mRestoring;
257 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700258 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259
Michael Jurka1e2f4652013-07-08 18:03:46 -0700260 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700261 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
262
Winson Chung4a2afa32012-07-19 14:53:05 -0700263 // Keep track of whether the user has left launcher
264 private static boolean sPausedFromUserAction = false;
265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private Bundle mSavedInstanceState;
267
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800269 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800270 private boolean mUserPresent = true;
271 private boolean mVisible = false;
272 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700273 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700275 private static LocaleConfiguration sLocaleConfiguration = null;
276
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700277 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700278
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700279 private Intent mAppMarketIntent = null;
280
Adam Cohended9f8d2010-11-03 13:25:16 -0700281 // Related to the auto-advancing of widgets
282 private final int ADVANCE_MSG = 1;
283 private final int mAdvanceInterval = 20000;
284 private final int mAdvanceStagger = 250;
285 private long mAutoAdvanceSentTime;
286 private long mAutoAdvanceTimeLeft = -1;
287 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
288 new HashMap<View, AppWidgetProviderInfo>();
289
Winson Chung400438b2011-01-16 17:53:48 -0800290 // Determines how long to wait after a rotation before restoring the screen orientation to
291 // match the sensor state.
292 private final int mRestoreScreenOrientationDelay = 500;
293
Michael Jurka4ef207b2010-11-29 17:05:45 -0800294 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700295 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
296 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
297 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800298
Craig Mautner360310b2012-10-26 15:13:08 -0700299 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700300
Adam Cohen1462de32012-07-24 22:34:36 -0700301 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
302
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700303 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
304
Winson Chung46353de2012-02-16 14:05:10 -0800305 // We only want to get the SharedPreferences once since it does an FS stat each time we get
306 // it from the context.
307 private SharedPreferences mSharedPrefs;
308
Adam Cohene25af792013-06-06 23:08:25 -0700309 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
310
Winson Chungf0c6ae02012-03-21 16:10:31 -0700311 // Holds the page that we need to animate to, and the icon views that we need to animate up
312 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700313 private ImageView mFolderIconImageView;
314 private Bitmap mFolderIconBitmap;
315 private Canvas mFolderIconCanvas;
316 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700317
Michael Jurkaddd62e92011-02-16 17:49:14 -0800318 private BubbleTextView mWaitingForResume;
319
Michael Jurka22143132012-10-04 17:42:38 +0200320 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
321 = new HideFromAccessibilityHelper();
322
Michael Jurkac1f5d262011-09-30 19:32:27 -0700323 private Runnable mBuildLayersRunnable = new Runnable() {
324 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700325 if (mWorkspace != null) {
326 mWorkspace.buildPageHardwareLayers();
327 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700328 }
329 };
330
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800331 private static ArrayList<PendingAddArguments> sPendingAddList
332 = new ArrayList<PendingAddArguments>();
333
Michael Jurka0a457bf2012-11-19 14:05:05 -0800334 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
335
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800336 private static class PendingAddArguments {
337 int requestCode;
338 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700339 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700340 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800341 int cellX;
342 int cellY;
343 }
344
Daniel Sandlerff02d492013-08-05 02:12:05 -0400345 private Stats mStats;
346
Michael Jurka0a457bf2012-11-19 14:05:05 -0800347 private static boolean isPropertyEnabled(String propertyName) {
348 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700349 }
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 @Override
352 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700353 if (DEBUG_STRICT_MODE) {
354 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
355 .detectDiskReads()
356 .detectDiskWrites()
357 .detectNetwork() // or .detectAll() for all detectable problems
358 .penaltyLog()
359 .build());
360 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
361 .detectLeakedSqlLiteObjects()
362 .detectLeakedClosableObjects()
363 .penaltyLog()
364 .penaltyDeath()
365 .build());
366 }
367
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400369
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400370 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400371 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700372
373 // Determine the dynamic grid properties
374 Point smallestSize = new Point();
375 Point largestSize = new Point();
376 Point realSize = new Point();
377 Display display = getWindowManager().getDefaultDisplay();
378 display.getCurrentSizeRange(smallestSize, largestSize);
379 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700380 DisplayMetrics dm = new DisplayMetrics();
381 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700382 // Lazy-initialize the dynamic grid
383 DeviceProfile grid = app.initDynamicGrid(this,
384 Math.min(smallestSize.x, smallestSize.y),
385 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700386 realSize.x, realSize.y,
387 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700388
389 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400390 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700391 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800392 mModel = app.setLauncher(this);
393 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400394 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700396
Daniel Sandlerff02d492013-08-05 02:12:05 -0400397 mStats = new Stats(this);
398
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700399 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700400 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
401 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700402
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700403 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
404 // this also ensures that any synchronous binding below doesn't re-trigger another
405 // LauncherModel load.
406 mPaused = false;
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200409 android.os.Debug.startMethodTracing(
410 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
412
413 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 setContentView(R.layout.launcher);
Winson Chung5f8afe62013-08-12 16:19:28 -0700415 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700417 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800419 registerContentObservers();
420
Joe Onorato7c312c12009-08-13 21:36:53 -0700421 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 mSavedState = savedInstanceState;
424 restoreState(mSavedState);
425
Winson Chunga12a2502010-12-20 14:41:35 -0800426 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700427 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200428 mAppsCustomizeContent.onPackagesUpdated(
429 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700430 }
Winson Chunga12a2502010-12-20 14:41:35 -0800431
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 if (PROFILE_STARTUP) {
433 android.os.Debug.stopMethodTracing();
434 }
435
436 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700437 if (sPausedFromUserAction) {
438 // If the user leaves launcher, then we should just load items asynchronously when
439 // they return.
440 mModel.startLoader(true, -1);
441 } else {
442 // We only load the page synchronously if the user rotates (or triggers a
443 // configuration change) while launcher is in the foreground
444 mModel.startLoader(true, mWorkspace.getCurrentPage());
445 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 }
447
448 // For handling default keys
449 mDefaultKeySsb = new SpannableStringBuilder();
450 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800451
452 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
453 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800454
Adam Cohenf9426d52012-06-04 17:26:21 -0700455 updateGlobalIcons();
456
457 // On large interfaces, we want the screen to auto-rotate based on the current orientation
458 unlockScreenOrientation(true);
459 }
460
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 protected void onUserLeaveHint() {
462 super.onUserLeaveHint();
463 sPausedFromUserAction = true;
464 }
465
Winson Chung98ca0f72013-07-29 12:58:51 -0700466 /** To be overriden by subclasses to hint to Launcher that we have custom content */
467 protected boolean hasCustomContentToLeft() {
468 return false;
469 }
470
Adam Cohenf9426d52012-06-04 17:26:21 -0700471 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700472 boolean searchVisible = false;
473 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800474 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700475 int coi = getCurrentOrientationIndexForGlobalIcons();
476 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
477 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700478 updateAppMarketIcon();
479 searchVisible = updateGlobalSearchIcon();
480 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700481 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700482 if (sGlobalSearchIcon[coi] != null) {
483 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700484 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700485 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700486 if (sVoiceSearchIcon[coi] != null) {
487 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700488 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700489 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700490 if (sAppMarketIcon[coi] != null) {
491 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800492 }
Winson Chungadf0c182012-08-23 14:59:07 -0700493 if (mSearchDropTargetBar != null) {
494 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 }
Romain Guycbb89e42009-06-08 15:52:54 -0700497
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700499 if (sLocaleConfiguration == null) {
500 new AsyncTask<Void, Void, LocaleConfiguration>() {
501 @Override
502 protected LocaleConfiguration doInBackground(Void... unused) {
503 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
504 readConfiguration(Launcher.this, localeConfiguration);
505 return localeConfiguration;
506 }
507
508 @Override
509 protected void onPostExecute(LocaleConfiguration result) {
510 sLocaleConfiguration = result;
511 checkForLocaleChange(); // recursive, but now with a locale configuration
512 }
513 }.execute();
514 return;
515 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700516
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 final Configuration configuration = getResources().getConfiguration();
518
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700519 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 final String locale = configuration.locale.toString();
521
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700522 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800523 final int mcc = configuration.mcc;
524
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700525 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 final int mnc = configuration.mnc;
527
Romain Guy84f296c2009-11-04 15:00:44 -0800528 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529
Romain Guy84f296c2009-11-04 15:00:44 -0800530 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700531 sLocaleConfiguration.locale = locale;
532 sLocaleConfiguration.mcc = mcc;
533 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700534
Joe Onorato0589f0f2010-02-08 13:44:00 -0800535 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700536
537 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
538 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700539 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700540 public void run() {
541 writeConfiguration(Launcher.this, localeConfiguration);
542 }
543 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700544 }
545 }
546
547 private static class LocaleConfiguration {
548 public String locale;
549 public int mcc = -1;
550 public int mnc = -1;
551 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700552
Romain Guy98d01652009-06-30 16:21:04 -0700553 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
554 DataInputStream in = null;
555 try {
556 in = new DataInputStream(context.openFileInput(PREFERENCES));
557 configuration.locale = in.readUTF();
558 configuration.mcc = in.readInt();
559 configuration.mnc = in.readInt();
560 } catch (FileNotFoundException e) {
561 // Ignore
562 } catch (IOException e) {
563 // Ignore
564 } finally {
565 if (in != null) {
566 try {
567 in.close();
568 } catch (IOException e) {
569 // Ignore
570 }
571 }
572 }
573 }
574
575 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
576 DataOutputStream out = null;
577 try {
578 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
579 out.writeUTF(configuration.locale);
580 out.writeInt(configuration.mcc);
581 out.writeInt(configuration.mnc);
582 out.flush();
583 } catch (FileNotFoundException e) {
584 // Ignore
585 } catch (IOException e) {
586 //noinspection ResultOfMethodCallIgnored
587 context.getFileStreamPath(PREFERENCES).delete();
588 } finally {
589 if (out != null) {
590 try {
591 out.close();
592 } catch (IOException e) {
593 // Ignore
594 }
595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 }
597 }
598
Bjorn Bringertc459e522013-06-07 19:36:01 +0100599 public LayoutInflater getInflater() {
600 return mInflater;
601 }
602
Adam Cohen716b51e2011-06-30 12:09:54 -0700603 public DragLayer getDragLayer() {
604 return mDragLayer;
605 }
606
Winson Chung36a62fe2012-05-06 18:04:42 -0700607 boolean isDraggingEnabled() {
608 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
609 // that is subsequently removed from the workspace in startBinding().
610 return !mModel.isLoadingWorkspace();
611 }
612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 static int getScreen() {
614 synchronized (sLock) {
615 return sScreen;
616 }
617 }
618
619 static void setScreen(int screen) {
620 synchronized (sLock) {
621 sScreen = screen;
622 }
623 }
624
Winson Chung557d6ed2011-07-08 15:34:52 -0700625 /**
626 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
627 * a configuration step, this allows the proper animations to run after other transitions.
628 */
629 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700630 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800631 switch (args.requestCode) {
632 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700633 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700634 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800635 break;
636 case REQUEST_PICK_SHORTCUT:
637 processShortcut(args.intent);
638 break;
639 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700640 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700641 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700642 result = true;
643 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800644 case REQUEST_CREATE_APPWIDGET:
645 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700647 result = true;
648 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800649 case REQUEST_PICK_WALLPAPER:
650 // We just wanted the activity result here so we can clear mWaitingForResult
651 break;
652 }
Michael Jurka27614d22012-04-02 06:40:22 -0700653 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
654 // if you turned the screen off and then back while in All Apps, Launcher would not
655 // return to the workspace. Clearing mAddInfo.container here fixes this issue
656 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700657 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 }
659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700661 protected void onActivityResult(
662 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700663 // Reset the startActivity waiting flag
664 mWaitingForResult = false;
665
Michael Jurka8b805b12012-04-18 14:23:14 -0700666 if (requestCode == REQUEST_BIND_APPWIDGET) {
667 int appWidgetId = data != null ?
668 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
669 if (resultCode == RESULT_CANCELED) {
670 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
671 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700672 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700673 }
674 return;
675 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700676 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
678 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700679
Adam Cohened66b2b2012-01-23 17:28:51 -0800680 // We have special handling for widgets
681 if (isWidgetDrop) {
682 int appWidgetId = data != null ?
683 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700684 if (appWidgetId < 0) {
685 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
686 "widget configuration activity.");
687 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
688 } else {
689 completeTwoStageWidgetDrop(resultCode, appWidgetId);
690 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800691 return;
692 }
693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 // The pattern used here is that a user PICKs a specific application,
695 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700696
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
698 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700699 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 final PendingAddArguments args = new PendingAddArguments();
701 args.requestCode = requestCode;
702 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700703 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700705 args.cellX = mPendingAddInfo.cellX;
706 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800707 if (isWorkspaceLocked()) {
708 sPendingAddList.add(args);
709 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700710 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800713 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700714 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800715 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
716 null);
717 }
718
719 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700721 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800722 Runnable onCompleteRunnable = null;
723 int animationType = 0;
724
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700725 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800726 if (resultCode == RESULT_OK) {
727 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
728 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700729 mPendingAddWidgetInfo);
730 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 onCompleteRunnable = new Runnable() {
732 @Override
733 public void run() {
734 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700735 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
737 null);
738 }
739 };
740 } else if (resultCode == RESULT_CANCELED) {
741 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
742 onCompleteRunnable = new Runnable() {
743 @Override
744 public void run() {
745 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
746 null);
747 }
748 };
749 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700750 if (mDragLayer.getAnimatedView() != null) {
751 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
752 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
753 animationType, boundWidget, true);
754 } else {
755 // The animated view may be null in the case of a rotation during widget configuration
756 onCompleteRunnable.run();
757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
759
760 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700761 protected void onStop() {
762 super.onStop();
763 FirstFrameAnimatorHelper.setIsVisible(false);
764 }
765
766 @Override
767 protected void onStart() {
768 super.onStart();
769 FirstFrameAnimatorHelper.setIsVisible(true);
770 }
771
772 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200774 long startTime = 0;
775 if (DEBUG_RESUME_TIME) {
776 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400777 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200778 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700780
Winson Chung4a2afa32012-07-19 14:53:05 -0700781 // Restore the previous launcher state
782 if (mOnResumeState == State.WORKSPACE) {
783 showWorkspace(false);
784 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
785 showAllApps(false);
786 }
787 mOnResumeState = State.NONE;
788
Craig Mautner360310b2012-10-26 15:13:08 -0700789 // Background was set to gradient in onPause(), restore to black if in all apps.
790 setWorkspaceBackground(mState == State.WORKSPACE);
791
Winson Chungde0fb8f2012-05-08 14:37:08 -0700792 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700793 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700794
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800795 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700796 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700797 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700799 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700801 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700803 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200804 // We might have postponed some bind calls until onResume (see waitUntilResume) --
805 // execute them here
806 long startTimeCallbacks = 0;
807 if (DEBUG_RESUME_TIME) {
808 startTimeCallbacks = System.currentTimeMillis();
809 }
810
811 if (mAppsCustomizeContent != null) {
812 mAppsCustomizeContent.setBulkBind(true);
813 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700814 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
815 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200816 }
817 if (mAppsCustomizeContent != null) {
818 mAppsCustomizeContent.setBulkBind(false);
819 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700820 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200821 if (DEBUG_RESUME_TIME) {
822 Log.d(TAG, "Time spent processing callbacks in onResume: " +
823 (System.currentTimeMillis() - startTimeCallbacks));
824 }
Michael Jurka447bf842013-05-15 14:52:15 +0200825 }
Michael Jurka54554252013-08-01 12:52:23 +0200826 if (mOnResumeCallbacks.size() > 0) {
827 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
828 mOnResumeCallbacks.get(i).run();
829 }
830 mOnResumeCallbacks.clear();
831 }
Winson Chunge4e50662012-01-23 14:45:13 -0800832
833 // Reset the pressed state of icons that were locked in the press state while activities
834 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800835 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800836 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800837 mWaitingForResume.setStayPressed(false);
838 }
Winson Chunge4e50662012-01-23 14:45:13 -0800839 if (mAppsCustomizeContent != null) {
840 // Resets the previous all apps icon press state
841 mAppsCustomizeContent.resetDrawableState();
842 }
Adam Cohen06dff352012-06-01 17:17:08 -0700843 // It is possible that widgets can receive updates while launcher is not in the foreground.
844 // Consequently, the widgets will be inflated in the orientation of the foreground activity
845 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
846 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700847 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700848
849 // Again, as with the above scenario, it's possible that one or more of the global icons
850 // were updated in the wrong orientation.
851 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200852 if (DEBUG_RESUME_TIME) {
853 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
854 }
Adam Cohen06dff352012-06-01 17:17:08 -0700855 }
856
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700858 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700859 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
860 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
861 // when Launcher resumes and we are still in AllApps.
862 updateWallpaperVisibility(true);
863
Winson Chung997a9232013-07-24 15:33:46 -0700864 // Ensure that items added to Launcher are queued until Launcher returns
865 InstallShortcutReceiver.enableInstallQueue();
866
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700867 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700868 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800869 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700870 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700871 }
Romain Guycbb89e42009-06-08 15:52:54 -0700872
Adam Cohen66a01fd2013-06-11 12:48:00 -0700873 protected void onFinishBindingItems() {
874 }
875
Adam Cohenbffe7452013-07-22 18:21:45 -0700876 QSBScroller mQsbScroller = new QSBScroller() {
877 int scrollY = 0;
878
879 @Override
880 public void setScrollY(int scroll) {
881 scrollY = scroll;
882
883 if (mWorkspace.isOnOrMovingToCustomContent()) {
884 mSearchDropTargetBar.setTranslationY(- scrollY);
885 }
886 }
887 };
888
889 public void resetQSBScroll() {
890 mSearchDropTargetBar.animate().translationY(0).start();
891 }
892
893 public interface CustomContentCallbacks {
894 // Custom content is completely shown
895 public void onShow();
896
897 // Custom content is completely hidden
898 public void onHide();
899 }
900
901 public interface QSBScroller {
902 public void setScrollY(int scrollY);
903 }
904
Adam Cohen66a01fd2013-06-11 12:48:00 -0700905 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700906 public QSBScroller addToCustomContentPage(View customContent) {
907 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700908 }
909
Winson Chung98ca0f72013-07-29 12:58:51 -0700910 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700911 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700912 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700913 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700914 }
915
Adam Cohenedb40762013-07-18 16:45:45 -0700916 // The custom content needs to offset its content to account for the QSB
917 public int getTopOffsetForCustomContent() {
918 return mWorkspace.getPaddingTop();
919 }
920
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700921 @Override
922 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 // Flag the loader to stop early before switching
924 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700925 if (mAppsCustomizeContent != null) {
926 mAppsCustomizeContent.surrender();
927 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800928 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700929 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700930
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800931 // We can't hide the IME if it was forced open. So don't bother
932 /*
933 @Override
934 public void onWindowFocusChanged(boolean hasFocus) {
935 super.onWindowFocusChanged(hasFocus);
936
937 if (hasFocus) {
938 final InputMethodManager inputManager = (InputMethodManager)
939 getSystemService(Context.INPUT_METHOD_SERVICE);
940 WindowManager.LayoutParams lp = getWindow().getAttributes();
941 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
942 android.os.Handler()) {
943 protected void onReceiveResult(int resultCode, Bundle resultData) {
944 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
945 }
946 });
947 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
948 }
949 }
950 */
951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 private boolean acceptFilter() {
953 final InputMethodManager inputManager = (InputMethodManager)
954 getSystemService(Context.INPUT_METHOD_SERVICE);
955 return !inputManager.isFullscreenMode();
956 }
957
958 @Override
959 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700960 final int uniChar = event.getUnicodeChar();
961 final boolean handled = super.onKeyDown(keyCode, event);
962 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
963 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
965 keyCode, event);
966 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700968 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 // showSearchDialog()
970 // If there are multiple keystrokes before the search dialog takes focus,
971 // onSearchRequested() will be called for every keystroke,
972 // but it is idempotent, so it's fine.
973 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 }
975 }
976
Joe Onorato8a9625e2010-01-28 15:55:35 -0800977 // Eat the long press event so the keyboard doesn't come up.
978 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
979 return true;
980 }
981
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 return handled;
983 }
984
Karl Rosaen138a0412009-04-23 19:00:21 -0700985 private String getTypedText() {
986 return mDefaultKeySsb.toString();
987 }
988
989 private void clearTypedText() {
990 mDefaultKeySsb.clear();
991 mDefaultKeySsb.clearSpans();
992 Selection.setSelection(mDefaultKeySsb, 0);
993 }
994
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700996 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
997 * State
998 */
999 private static State intToState(int stateOrdinal) {
1000 State state = State.WORKSPACE;
1001 final State[] stateValues = State.values();
1002 for (int i = 0; i < stateValues.length; i++) {
1003 if (stateValues[i].ordinal() == stateOrdinal) {
1004 state = stateValues[i];
1005 break;
1006 }
1007 }
1008 return state;
1009 }
1010
1011 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 * Restores the previous state, if it exists.
1013 *
1014 * @param savedState The previous state.
1015 */
1016 private void restoreState(Bundle savedState) {
1017 if (savedState == null) {
1018 return;
1019 }
1020
Michael Jurka883f55b2010-10-21 15:47:14 -07001021 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001022 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001023 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001024 }
1025
Winson Chungf0c6ae02012-03-21 16:10:31 -07001026 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001028 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 }
1030
Winson Chung3d503fb2011-07-13 17:25:49 -07001031 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001032 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001033
Winson Chung3d503fb2011-07-13 17:25:49 -07001034 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1035 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001036 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001037 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1038 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001039 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1040 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1041 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001042 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 mRestoring = true;
1044 }
1045
1046 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1047 if (renameFolder) {
1048 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001049 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 mRestoring = true;
1051 }
Winson Chunga12a2502010-12-20 14:41:35 -08001052
Winson Chung785d2eb2011-04-14 16:08:02 -07001053 // Restore the AppsCustomize tab
1054 if (mAppsCustomizeTabHost != null) {
1055 String curTab = savedState.getString("apps_customize_currentTab");
1056 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001057 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001058 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001059 mAppsCustomizeContent.loadAssociatedPages(
1060 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001061 }
1062
Winson Chung5afbf7b2011-07-25 11:53:08 -07001063 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1064 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 }
1067
1068 /**
1069 * Finds all the views we need and configure them properly.
1070 */
1071 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001072 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001073
Craig Mautner360310b2012-10-26 15:13:08 -07001074 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001075 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1076 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001077
1078 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1079 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080
Winson Chung4c98d922011-05-31 16:50:48 -07001081 // Setup the drag layer
1082 mDragLayer.setup(this, dragController);
1083
Winson Chung3d503fb2011-07-13 17:25:49 -07001084 // Setup the hotseat
1085 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1086 if (mHotseat != null) {
1087 mHotseat.setup(this);
1088 }
1089
Adam Cohenf358a4b2013-07-23 16:47:31 -07001090 mOverviewPanel = findViewById(R.id.overview_panel);
1091 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1092 @Override
1093 public void onClick(View arg0) {
1094 showAllApps(true);
1095 }
1096 });
1097 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1098 @Override
1099 public void onClick(View arg0) {
1100 startWallpaper();
1101 }
1102 });
1103
Winson Chung4c98d922011-05-31 16:50:48 -07001104 // Setup the workspace
1105 mWorkspace.setHapticFeedbackEnabled(false);
1106 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001107 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001108 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001109
Winson Chungf0ea4d32011-06-06 14:27:16 -07001110 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001111 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001112
Winson Chungf0ea4d32011-06-06 14:27:16 -07001113 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001114 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001115 mAppsCustomizeContent = (AppsCustomizePagedView)
1116 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1117 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001118
Winson Chung3d503fb2011-07-13 17:25:49 -07001119 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001120 dragController.setDragScoller(mWorkspace);
1121 dragController.setScrollView(mDragLayer);
1122 dragController.setMoveTarget(mWorkspace);
1123 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001124 if (mSearchDropTargetBar != null) {
1125 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001126 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001127
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001128 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001129 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001130 mWeightWatcher = new WeightWatcher(this);
1131 mWeightWatcher.setAlpha(0.5f);
1132 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001133 new FrameLayout.LayoutParams(
1134 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001135 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001136 Gravity.BOTTOM)
1137 );
Adam Cohen39a06042013-07-19 14:30:12 -07001138
1139 boolean show = shouldShowWeightWatcher();
1140 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 }
1143
1144 /**
1145 * Creates a view representing a shortcut.
1146 *
1147 * @param info The data structure describing the shortcut.
1148 *
1149 * @return A View inflated from R.layout.application.
1150 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001151 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001153 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
1155
1156 /**
1157 * Creates a view representing a shortcut inflated from the specified resource.
1158 *
1159 * @param layoutResId The id of the XML layout used to create the shortcut.
1160 * @param parent The group the shortcut belongs to.
1161 * @param info The data structure describing the shortcut.
1162 *
1163 * @return A View inflated from layoutResId.
1164 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001165 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001166 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1167 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 return favorite;
1170 }
1171
1172 /**
1173 * Add an application shortcut to the workspace.
1174 *
1175 * @param data The intent describing the application.
1176 * @param cellInfo The position on screen where to create the shortcut.
1177 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001178 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001179 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001180 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181
Adam Cohenfbba09b2011-07-18 21:43:05 -07001182 // First we check if we already know the exact location where we want to add this item.
1183 if (cellX >= 0 && cellY >= 0) {
1184 cellXY[0] = cellX;
1185 cellXY[1] = cellY;
1186 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001187 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188 return;
1189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001191 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001192
Romain Guy73b979d2009-06-09 12:57:21 -07001193 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001194 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001196 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001197 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001198 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001199 } else {
1200 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 }
1202 }
Romain Guycbb89e42009-06-08 15:52:54 -07001203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 /**
1205 * Add a shortcut to the workspace.
1206 *
1207 * @param data The intent describing the shortcut.
1208 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001210 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001211 int cellY) {
1212 int[] cellXY = mTmpAddItemCellCoordinates;
1213 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001214 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001215
Michael Jurkad3ef3062010-11-23 16:23:58 -08001216 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001217
Adam Cohen558baaf2011-08-15 15:22:57 -07001218 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001219 if (info == null) {
1220 return;
1221 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001222 final View view = createShortcut(info);
1223
Adam Cohenfbba09b2011-07-18 21:43:05 -07001224 // First we check if we already know the exact location where we want to add this item.
1225 if (cellX >= 0 && cellY >= 0) {
1226 cellXY[0] = cellX;
1227 cellXY[1] = cellY;
1228 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001229
1230 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001231 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001232 true, null,null)) {
1233 return;
1234 }
1235 DragObject dragObject = new DragObject();
1236 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001237 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1238 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001239 return;
1240 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001241 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001242 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001243 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001244 foundCellSpan = (result != null);
1245 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001246 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001247 }
1248
1249 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001250 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001251 return;
1252 }
1253
Adam Cohendcd297f2013-06-18 13:13:40 -07001254 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
1256 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001257 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001258 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260 }
1261
Adam Cohen2f093b62012-04-30 18:59:53 -07001262 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1263 int minHeight) {
1264 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001265 // We want to account for the extra amount of padding that we are adding to the widget
1266 // to ensure that it gets the full amount of space that it has requested
1267 int requiredWidth = minWidth + padding.left + padding.right;
1268 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001269 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001270 }
1271
Adam Cohen2f093b62012-04-30 18:59:53 -07001272 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1273 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001274 }
1275
Adam Cohen2f093b62012-04-30 18:59:53 -07001276 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1277 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001278 }
1279
Adam Cohen2f093b62012-04-30 18:59:53 -07001280 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1281 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001282 }
1283
Adam Cohen2f093b62012-04-30 18:59:53 -07001284 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1285 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001286 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001287 }
1288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001290 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001292 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001293 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001295 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001296 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1297 if (appWidgetInfo == null) {
1298 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1299 }
Romain Guycbb89e42009-06-08 15:52:54 -07001300
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001301 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001302 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001303
Adam Cohen2f093b62012-04-30 18:59:53 -07001304 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1305 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001306
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001308 // We have saved the position to which the widget was dragged-- this really only matters
1309 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001310 int[] cellXY = mTmpAddItemCellCoordinates;
1311 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001312 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001313 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001314 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1315 cellXY[0] = mPendingAddInfo.cellX;
1316 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001317 spanXY[0] = mPendingAddInfo.spanX;
1318 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001319 foundCellSpan = true;
1320 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001321 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001322 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001323 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1324 spanXY[1], cellXY, finalSpan);
1325 spanXY[0] = finalSpan[0];
1326 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001327 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001328 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001329 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001330 }
1331
Michael Jurka0280c3b2010-09-17 15:00:07 -07001332 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001333 if (appWidgetId != -1) {
1334 // Deleting an app widget ID is a void call but writes to disk before returning
1335 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001336 new Thread("deleteAppWidgetId") {
1337 public void run() {
1338 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1339 }
1340 }.start();
1341 }
Winson Chung93eef082012-03-23 15:59:27 -07001342 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001343 return;
1344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001346 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001347 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1348 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001349 launcherInfo.spanX = spanXY[0];
1350 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001351 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1352 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001355 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356
1357 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001358 if (hostView == null) {
1359 // Perform actual inflation because we're live
1360 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1361 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1362 } else {
1363 // The AppWidgetHostView has already been inflated and instantiated
1364 launcherInfo.hostView = hostView;
1365 }
Romain Guycbb89e42009-06-08 15:52:54 -07001366
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001368 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001369 launcherInfo.notifyWidgetSizeChanged(this);
1370
Adam Cohendcd297f2013-06-18 13:13:40 -07001371 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001373
1374 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001376 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
Romain Guycbb89e42009-06-08 15:52:54 -07001378
Adam Cohended9f8d2010-11-03 13:25:16 -07001379 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1380 @Override
1381 public void onReceive(Context context, Intent intent) {
1382 final String action = intent.getAction();
1383 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1384 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001385 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001386 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001387
Winson Chungc100e8e2011-08-09 16:02:43 -07001388 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001389 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001390 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1391 mAppsCustomizeTabHost.reset();
1392 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001393 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001394 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1395 mUserPresent = true;
1396 updateRunning();
1397 }
1398 }
1399 };
1400
1401 @Override
1402 public void onAttachedToWindow() {
1403 super.onAttachedToWindow();
1404
1405 // Listen for broadcasts related to user-presence
1406 final IntentFilter filter = new IntentFilter();
1407 filter.addAction(Intent.ACTION_SCREEN_OFF);
1408 filter.addAction(Intent.ACTION_USER_PRESENT);
1409 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001410 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001411 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001412 mVisible = true;
1413 }
1414
1415 @Override
1416 public void onDetachedFromWindow() {
1417 super.onDetachedFromWindow();
1418 mVisible = false;
1419
Adam Cohend113e0c2010-11-11 10:48:05 -08001420 if (mAttached) {
1421 unregisterReceiver(mReceiver);
1422 mAttached = false;
1423 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001424 updateRunning();
1425 }
1426
1427 public void onWindowVisibilityChanged(int visibility) {
1428 mVisible = visibility == View.VISIBLE;
1429 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001430 // The following code used to be in onResume, but it turns out onResume is called when
1431 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1432 // is a more appropriate event to handle
1433 if (mVisible) {
1434 mAppsCustomizeTabHost.onWindowVisible();
1435 if (!mWorkspaceLoading) {
1436 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001437 // We want to let Launcher draw itself at least once before we force it to build
1438 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001439 // apps is nice and speedy.
1440 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001441 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001442 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001443 if (mStarted) return;
1444 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001445 // We delay the layer building a bit in order to give
1446 // other message processing a time to run. In particular
1447 // this avoids a delay in hiding the IME if it was
1448 // currently shown, because doing that may involve
1449 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001450 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001451 final ViewTreeObserver.OnDrawListener listener = this;
1452 mWorkspace.post(new Runnable() {
1453 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001454 if (mWorkspace != null &&
1455 mWorkspace.getViewTreeObserver() != null) {
1456 mWorkspace.getViewTreeObserver().
1457 removeOnDrawListener(listener);
1458 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001459 }
1460 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001461 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001462 }
1463 });
1464 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001465 // When Launcher comes back to foreground, a different Activity might be responsible for
1466 // the app market intent, so refresh the icon
1467 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001468 clearTypedText();
1469 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001470 }
1471
1472 private void sendAdvanceMessage(long delay) {
1473 mHandler.removeMessages(ADVANCE_MSG);
1474 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1475 mHandler.sendMessageDelayed(msg, delay);
1476 mAutoAdvanceSentTime = System.currentTimeMillis();
1477 }
1478
1479 private void updateRunning() {
1480 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1481 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1482 mAutoAdvanceRunning = autoAdvanceRunning;
1483 if (autoAdvanceRunning) {
1484 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1485 sendAdvanceMessage(delay);
1486 } else {
1487 if (!mWidgetsToAdvance.isEmpty()) {
1488 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1489 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1490 }
1491 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001492 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001493 }
1494 }
1495 }
1496
1497 private final Handler mHandler = new Handler() {
1498 @Override
1499 public void handleMessage(Message msg) {
1500 if (msg.what == ADVANCE_MSG) {
1501 int i = 0;
1502 for (View key: mWidgetsToAdvance.keySet()) {
1503 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1504 final int delay = mAdvanceStagger * i;
1505 if (v instanceof Advanceable) {
1506 postDelayed(new Runnable() {
1507 public void run() {
1508 ((Advanceable) v).advance();
1509 }
1510 }, delay);
1511 }
1512 i++;
1513 }
1514 sendAdvanceMessage(mAdvanceInterval);
1515 }
1516 }
1517 };
1518
1519 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001520 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001521 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1522 if (v instanceof Advanceable) {
1523 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001524 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001525 updateRunning();
1526 }
1527 }
1528
1529 void removeWidgetToAutoAdvance(View hostView) {
1530 if (mWidgetsToAdvance.containsKey(hostView)) {
1531 mWidgetsToAdvance.remove(hostView);
1532 updateRunning();
1533 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001534 }
1535
Joe Onorato9c1289c2009-08-17 11:03:03 -04001536 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001537 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001538 launcherInfo.hostView = null;
1539 }
1540
Winson Chung93eef082012-03-23 15:59:27 -07001541 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1542 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1543 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001544 }
1545
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001546 public LauncherAppWidgetHost getAppWidgetHost() {
1547 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
Romain Guycbb89e42009-06-08 15:52:54 -07001549
Winson Chunga9abd0e2010-10-27 17:18:37 -07001550 public LauncherModel getModel() {
1551 return mModel;
1552 }
1553
Bjorn Bringertc459e522013-06-07 19:36:01 +01001554 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001555 getWindow().closeAllPanels();
1556
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001557 // Whatever we were doing is hereby canceled.
1558 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001559 }
1560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 @Override
1562 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001563 long startTime = 0;
1564 if (DEBUG_RESUME_TIME) {
1565 startTime = System.currentTimeMillis();
1566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 super.onNewIntent(intent);
1568
1569 // Close the menu
1570 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001571 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001572 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001573
Jamie Genniscb222e82012-10-23 15:44:26 -07001574 final boolean alreadyOnHome =
1575 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001576 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001577
Jamie Genniscb222e82012-10-23 15:44:26 -07001578 Runnable processIntent = new Runnable() {
1579 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001580 if (mWorkspace == null) {
1581 // Can be cases where mWorkspace is null, this prevents a NPE
1582 return;
1583 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001584 Folder openFolder = mWorkspace.getOpenFolder();
1585 // In all these cases, only animate if we're already on home
1586 mWorkspace.exitWidgetResizeMode();
1587 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1588 openFolder == null) {
1589 mWorkspace.moveToDefaultScreen(true);
1590 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001591
Jamie Genniscb222e82012-10-23 15:44:26 -07001592 closeFolder();
1593 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001594
Jamie Genniscb222e82012-10-23 15:44:26 -07001595 // If we are already on home, then just animate back to the workspace,
1596 // otherwise, just wait until onResume to set the state back to Workspace
1597 if (alreadyOnHome) {
1598 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001599 if (mWorkspace.isInOverviewMode()) {
1600 mWorkspace.exitOverviewMode();
1601 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001602 } else {
1603 mOnResumeState = State.WORKSPACE;
1604 }
1605
1606 final View v = getWindow().peekDecorView();
1607 if (v != null && v.getWindowToken() != null) {
1608 InputMethodManager imm = (InputMethodManager)getSystemService(
1609 INPUT_METHOD_SERVICE);
1610 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1611 }
1612
1613 // Reset AllApps to its initial state
1614 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1615 mAppsCustomizeTabHost.reset();
1616 }
1617 }
1618 };
1619
1620 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1621 // Delay processing of the intent to allow the status bar animation to finish
1622 // first in order to avoid janky animations.
1623 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001624 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001625 // Process the intent immediately.
1626 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001627 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
Michael Jurka447bf842013-05-15 14:52:15 +02001630 if (DEBUG_RESUME_TIME) {
1631 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1632 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 }
1634
1635 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001636 public void onRestoreInstanceState(Bundle state) {
1637 super.onRestoreInstanceState(state);
1638 for (int page: mSynchronouslyBoundPages) {
1639 mWorkspace.restoreInstanceStateForChild(page);
1640 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 }
1642
1643 @Override
1644 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001645 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001646 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647
Michael Jurka883f55b2010-10-21 15:47:14 -07001648 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001649 // We close any open folder since it will not be re-opened, and we need to make sure
1650 // this state is reflected.
1651 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652
Adam Cohendcd297f2013-06-18 13:13:40 -07001653 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001654 mWaitingForResult) {
1655 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001656 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001657 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1658 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001659 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1660 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1661 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 }
1663
1664 if (mFolderInfo != null && mWaitingForResult) {
1665 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1666 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1667 }
Michael Jurka946ad472010-07-09 18:05:18 -07001668
Winson Chung785d2eb2011-04-14 16:08:02 -07001669 // Save the current AppsCustomize tab
1670 if (mAppsCustomizeTabHost != null) {
1671 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1672 if (currentTabTag != null) {
1673 outState.putString("apps_customize_currentTab", currentTabTag);
1674 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001675 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1676 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001677 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 }
1679
1680 @Override
1681 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001683
Winson Chunge7a03942011-08-05 15:05:12 -07001684 // Remove all pending runnables
1685 mHandler.removeMessages(ADVANCE_MSG);
1686 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001687 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001688
Winson Chungcd2b0142011-06-08 16:02:26 -07001689 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001690 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001691 mModel.stopLoader();
1692 app.setLauncher(null);
1693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001695 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001697 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001699 mAppWidgetHost = null;
1700
1701 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702
1703 TextKeyListener.getInstance().release();
1704
Winson Chung81b52252012-08-27 15:34:29 -07001705 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1706 // to prevent leaking Launcher activities on orientation change.
1707 if (mModel != null) {
1708 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1709 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001710
1711 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001712 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001713
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001714 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001715 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1716 mWorkspace.removeAllViews();
1717 mWorkspace = null;
1718 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001719
Michael Jurka2ecf9952012-06-18 12:52:28 -07001720 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
1722
Adam Cohena9cf38f2011-05-02 15:36:58 -07001723 public DragController getDragController() {
1724 return mDragController;
1725 }
1726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 @Override
1728 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001729 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 super.startActivityForResult(intent, requestCode);
1731 }
1732
Winson Chung70d72102011-08-12 11:24:35 -07001733 /**
1734 * Indicates that we want global search for this activity by setting the globalSearch
1735 * argument for {@link #startSearch} to true.
1736 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001738 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001740
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001741 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001742
Karl Rosaen138a0412009-04-23 19:00:21 -07001743 if (initialQuery == null) {
1744 // Use any text typed in the launcher as the initial query
1745 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001746 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 if (appSearchData == null) {
1748 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001749 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 }
Winson Chungadf0c182012-08-23 14:59:07 -07001751 Rect sourceBounds = new Rect();
1752 if (mSearchDropTargetBar != null) {
1753 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1754 }
Romain Guycbb89e42009-06-08 15:52:54 -07001755
Bjorn Bringertc459e522013-06-07 19:36:01 +01001756 startSearch(initialQuery, selectInitialQuery,
1757 appSearchData, sourceBounds);
1758 }
1759
1760 public void startSearch(String initialQuery,
1761 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001762 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001763 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001764 }
1765
1766 /**
1767 * Starts the global search activity. This code is a copied from SearchManager
1768 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001769 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001770 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001771 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001772 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1773 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1774 if (globalSearchActivity == null) {
1775 Log.w(TAG, "No global search activity found.");
1776 return;
1777 }
1778 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1779 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1780 intent.setComponent(globalSearchActivity);
1781 // Make sure that we have a Bundle to put source in
1782 if (appSearchData == null) {
1783 appSearchData = new Bundle();
1784 } else {
1785 appSearchData = new Bundle(appSearchData);
1786 }
1787 // Set source to package name of app that starts global search, if not set already.
1788 if (!appSearchData.containsKey("source")) {
1789 appSearchData.putString("source", getPackageName());
1790 }
1791 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1792 if (!TextUtils.isEmpty(initialQuery)) {
1793 intent.putExtra(SearchManager.QUERY, initialQuery);
1794 }
1795 if (selectInitialQuery) {
1796 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1797 }
1798 intent.setSourceBounds(sourceBounds);
1799 try {
1800 startActivity(intent);
1801 } catch (ActivityNotFoundException ex) {
1802 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1803 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 }
1805
Winson Chung70d72102011-08-12 11:24:35 -07001806 @Override
1807 public boolean onCreateOptionsMenu(Menu menu) {
1808 if (isWorkspaceLocked()) {
1809 return false;
1810 }
1811
1812 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001813
1814 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1815 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1816 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001817 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1818 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1819 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001820 String helpUrl = getString(R.string.help_url);
1821 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001822 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1823 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1824
Winson Chung70d72102011-08-12 11:24:35 -07001825 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1826 .setIcon(android.R.drawable.ic_menu_gallery)
1827 .setAlphabeticShortcut('W');
1828 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1829 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001830 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001831 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001832 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1833 .setIcon(android.R.drawable.ic_menu_preferences)
1834 .setIntent(settings)
1835 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001836 if (!helpUrl.isEmpty()) {
1837 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1838 .setIcon(android.R.drawable.ic_menu_help)
1839 .setIntent(help)
1840 .setAlphabeticShortcut('H');
1841 }
Winson Chung70d72102011-08-12 11:24:35 -07001842 return true;
1843 }
1844
1845 @Override
1846 public boolean onPrepareOptionsMenu(Menu menu) {
1847 super.onPrepareOptionsMenu(menu);
1848
1849 if (mAppsCustomizeTabHost.isTransitioning()) {
1850 return false;
1851 }
1852 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1853 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1854
1855 return true;
1856 }
1857
1858 @Override
1859 public boolean onOptionsItemSelected(MenuItem item) {
1860 switch (item.getItemId()) {
1861 case MENU_WALLPAPER_SETTINGS:
1862 startWallpaper();
1863 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001864 }
1865
1866 return super.onOptionsItemSelected(item);
1867 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001869 @Override
1870 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001871 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001872 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001873 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001874 }
1875
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 public boolean isWorkspaceLocked() {
1877 return mWorkspaceLoading || mWaitingForResult;
1878 }
1879
Michael Jurka0280c3b2010-09-17 15:00:07 -07001880 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001881 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001882 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001883 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1884 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001885 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001886 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001887 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001888
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001889 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1890 AppWidgetProviderInfo appWidgetInfo) {
1891 if (appWidgetInfo.configure != null) {
1892 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001893
Bjorn Bringert7984c942009-12-09 15:38:25 +00001894 // Launch over to configure widget, if needed
1895 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001896 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001897 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001898 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001900 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001901 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001902 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001903 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001904 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 }
1906 }
Romain Guycbb89e42009-06-08 15:52:54 -07001907
Adam Cohenfbba09b2011-07-18 21:43:05 -07001908 /**
1909 * Process a shortcut drop.
1910 *
1911 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001912 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001913 * @param cell The cell it should be added to, optional
1914 * @param position The location on the screen where it was dropped, optional
1915 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001916 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001917 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001918 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001919 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001920 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001922
1923 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001924 mPendingAddInfo.cellX = cell[0];
1925 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001926 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001927
1928 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1929 createShortcutIntent.setComponent(componentName);
1930 processShortcut(createShortcutIntent);
1931 }
1932
Adam Cohenfbba09b2011-07-18 21:43:05 -07001933 /**
1934 * Process a widget drop.
1935 *
1936 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001937 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001938 * @param cell The cell it should be added to, optional
1939 * @param position The location on the screen where it was dropped, optional
1940 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001941 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001942 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001943 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001944 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001945 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001946 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001947 mPendingAddInfo.minSpanX = info.minSpanX;
1948 mPendingAddInfo.minSpanY = info.minSpanY;
1949
Adam Cohenfbba09b2011-07-18 21:43:05 -07001950 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001951 mPendingAddInfo.cellX = cell[0];
1952 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001953 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001954 if (span != null) {
1955 mPendingAddInfo.spanX = span[0];
1956 mPendingAddInfo.spanY = span[1];
1957 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001958
Adam Cohened66b2b2012-01-23 17:28:51 -08001959 AppWidgetHostView hostView = info.boundWidget;
1960 int appWidgetId;
1961 if (hostView != null) {
1962 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001963 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001964 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001965 // In this case, we either need to start an activity to get permission to bind
1966 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001967 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001968 Bundle options = info.bindOptions;
1969
1970 boolean success = false;
1971 if (options != null) {
1972 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1973 info.componentName, options);
1974 } else {
1975 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1976 info.componentName);
1977 }
1978 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001979 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001980 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001981 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001982 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1983 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1984 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001985 // TODO: we need to make sure that this accounts for the options bundle.
1986 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001987 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1988 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001989 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001990 }
1991
Joe Onoratodeb98af2010-02-19 14:59:39 -08001992 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001993 // Handle case where user selected "Applications"
1994 String applicationName = getResources().getString(R.string.group_applications);
1995 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001996
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001997 if (applicationName != null && applicationName.equals(shortcutName)) {
1998 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1999 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002000
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002001 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2002 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002003 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002004 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002005 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002006 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
2009
Winson Chung24ab2f12010-09-16 14:10:47 -07002010 void processWallpaper(Intent intent) {
2011 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2012 }
2013
Adam Cohendcd297f2013-06-18 13:13:40 -07002014 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002015 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002016 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 folderInfo.title = getText(R.string.folder_name);
2018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002020 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002021 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002022 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023
2024 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002025 FolderIcon newFolder =
2026 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002027 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002028 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002029 // Force measure the new folder icon
2030 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2031 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002032 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
Romain Guycbb89e42009-06-08 15:52:54 -07002034
Joe Onorato9c1289c2009-08-17 11:03:03 -04002035 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002036 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002037 }
2038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002040 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurka104c4562013-07-08 18:03:46 -07002042 pickWallpaper.setComponent(
2043 new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName()));
2044 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002047 /**
2048 * Registers various content observers. The current implementation registers
2049 * only a favorites observer to keep track of the favorites applications.
2050 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002051 private void registerContentObservers() {
2052 ContentResolver resolver = getContentResolver();
2053 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2054 true, mWidgetObserver);
2055 }
2056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 @Override
2058 public boolean dispatchKeyEvent(KeyEvent event) {
2059 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2060 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002062 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002063 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002064 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002065 dumpState();
2066 return true;
2067 }
2068 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002069 }
2070 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2071 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002072 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 return true;
2074 }
2075 }
2076
2077 return super.dispatchKeyEvent(event);
2078 }
2079
Joe Onorato88ec0992009-11-19 13:16:06 -08002080 @Override
2081 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002082 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002083 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002084 } else if (mWorkspace.isInOverviewMode()) {
2085 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002086 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002087 Folder openFolder = mWorkspace.getOpenFolder();
2088 if (openFolder.isEditingName()) {
2089 openFolder.dismissEditingName();
2090 } else {
2091 closeFolder();
2092 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002093 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002094 mWorkspace.exitWidgetResizeMode();
2095
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002096 // Back button is a no-op here, but give at least some feedback for the button press
2097 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002098 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002099 }
2100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002102 * Re-listen when widgets are reset.
2103 */
2104 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002105 if (mAppWidgetHost != null) {
2106 mAppWidgetHost.startListening();
2107 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002108 }
2109
2110 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 * Launches the intent referred by the clicked shortcut.
2112 *
2113 * @param v The view representing the clicked shortcut.
2114 */
2115 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002116 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2117 // view has detached (it's possible for this to happen if the view is removed mid touch).
2118 if (v.getWindowToken() == null) {
2119 return;
2120 }
2121
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002122 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002123 return;
2124 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002125
Adam Cohenf358a4b2013-07-23 16:47:31 -07002126 if (v instanceof PageIndicator) {
2127 if (!mWorkspace.isInOverviewMode()) {
2128 mWorkspace.enterOverviewMode();
2129 }
2130 return;
2131 }
2132
2133 if (v instanceof CellLayout) {
2134 if (mWorkspace.isInOverviewMode()) {
2135 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2136 }
2137 }
2138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002140 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002142 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2143 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002144
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002145 // Check for special shortcuts
2146 if (intent.getComponent() != null) {
2147 final String shortcutClass = intent.getComponent().getClassName();
2148
2149 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2150 showAllApps(true);
2151 return;
2152 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2153 MemoryDumpActivity.startDump(this);
2154 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002155 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2156 toggleShowWeightWatcher();
2157 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002158 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002159 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002160
2161 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002162 int[] pos = new int[2];
2163 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002164 intent.setSourceBounds(new Rect(pos[0], pos[1],
2165 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002166
2167 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002168
Daniel Sandlerff02d492013-08-05 02:12:05 -04002169 mStats.recordLaunch(intent, shortcut);
2170
Michael Jurkaddd62e92011-02-16 17:49:14 -08002171 if (success && v instanceof BubbleTextView) {
2172 mWaitingForResume = (BubbleTextView) v;
2173 mWaitingForResume.setStayPressed(true);
2174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002176 if (v instanceof FolderIcon) {
2177 FolderIcon fi = (FolderIcon) v;
2178 handleFolderClick(fi);
2179 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002180 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002181 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002182 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002183 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002184 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 }
2187 }
2188
Michael Jurka0e260592010-06-30 17:07:39 -07002189 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002190 return false;
2191 }
2192
Michael Jurkaaf442092010-06-10 17:01:57 -07002193 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002194 * Event handler for the search button
2195 *
2196 * @param v The view that was clicked.
2197 */
2198 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002199 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2200
Amith Yamasania135ba82011-08-09 17:42:01 -07002201 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002202 }
2203
2204 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002205 * Event handler for the voice button
2206 *
2207 * @param v The view that was clicked.
2208 */
2209 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002210 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002211
Bjorn Bringertc459e522013-06-07 19:36:01 +01002212 startVoice();
2213 }
2214
2215 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002216 try {
2217 final SearchManager searchManager =
2218 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2219 ComponentName activityName = searchManager.getGlobalSearchActivity();
2220 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002221 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002222 if (activityName != null) {
2223 intent.setPackage(activityName.getPackageName());
2224 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002225 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002226 } catch (ActivityNotFoundException e) {
2227 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002228 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002229 startActivitySafely(null, intent, "onClickVoiceButton");
2230 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002231 }
2232
2233 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002234 * Event handler for the "grid" button that appears on the home screen, which
2235 * enters all apps mode.
2236 *
2237 * @param v The view that was clicked.
2238 */
2239 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002240 showAllApps(true);
2241 }
2242
2243 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002244 // Provide the same haptic feedback that the system offers for virtual keys.
2245 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002246 }
2247
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002248 public void onClickAppMarketButton(View v) {
2249 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002250 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002251 } else {
2252 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002253 }
2254 }
2255
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002256 void startApplicationDetailsActivity(ComponentName componentName) {
2257 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002258 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2259 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002260 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002261 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002262 }
2263
Michael Jurka1e2f4652013-07-08 18:03:46 -07002264 // returns true if the activity was started
2265 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2266 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002267 // System applications cannot be installed. For now, show a toast explaining that.
2268 // We may give them the option of disabling apps this way.
2269 int messageId = R.string.uninstall_system_app_text;
2270 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002271 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002272 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002273 String packageName = componentName.getPackageName();
2274 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002275 Intent intent = new Intent(
2276 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002277 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2278 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002279 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002280 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002281 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002282 }
2283
Michael Jurka86a720e2012-05-09 11:23:23 -07002284 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002288 // Only launch using the new animation if the shortcut has not opted out (this is a
2289 // private contract between launcher and may be ignored in the future).
2290 boolean useLaunchAnimation = (v != null) &&
2291 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2292 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002293 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2294 v.getMeasuredWidth(), v.getMeasuredHeight());
2295
2296 startActivity(intent, opts.toBundle());
2297 } else {
2298 startActivity(intent);
2299 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002300 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 } catch (SecurityException e) {
2302 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002303 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002305 "or use the exported attribute for this activity. "
2306 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002308 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002310
Michael Jurka86a720e2012-05-09 11:23:23 -07002311 boolean startActivitySafely(View v, Intent intent, Object tag) {
2312 boolean success = false;
2313 try {
2314 success = startActivity(v, intent, tag);
2315 } catch (ActivityNotFoundException e) {
2316 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2317 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2318 }
2319 return success;
2320 }
2321
Adam Cohena9cf38f2011-05-02 15:36:58 -07002322 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002323 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002324 Folder openFolder = mWorkspace.getFolderForTag(info);
2325
2326 // If the folder info reports that the associated folder is open, then verify that
2327 // it is actually opened. There have been a few instances where this gets out of sync.
2328 if (info.opened && openFolder == null) {
2329 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002331 info.opened = false;
2332 }
2333
Adam Cohenfb91f302012-06-11 15:45:18 -07002334 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 // Close any open folder
2336 closeFolder();
2337 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002338 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 } else {
2340 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002341 int folderScreen;
2342 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002343 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002344 // .. and close it
2345 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002346 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 // Close any folder open on the current screen
2348 closeFolder();
2349 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002350 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 }
2352 }
2353 }
2354 }
2355
Adam Cohen268c4752012-06-06 17:47:33 -07002356 /**
2357 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2358 * in the DragLayer in the exact absolute location of the original FolderIcon.
2359 */
2360 private void copyFolderIconToImage(FolderIcon fi) {
2361 final int width = fi.getMeasuredWidth();
2362 final int height = fi.getMeasuredHeight();
2363
2364 // Lazy load ImageView, Bitmap and Canvas
2365 if (mFolderIconImageView == null) {
2366 mFolderIconImageView = new ImageView(this);
2367 }
2368 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2369 mFolderIconBitmap.getHeight() != height) {
2370 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2371 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2372 }
2373
2374 DragLayer.LayoutParams lp;
2375 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2376 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2377 } else {
2378 lp = new DragLayer.LayoutParams(width, height);
2379 }
2380
Adam Cohen307fe232012-08-16 17:55:58 -07002381 // The layout from which the folder is being opened may be scaled, adjust the starting
2382 // view size by this scale factor.
2383 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002384 lp.customPosition = true;
2385 lp.x = mRectForFolderAnimation.left;
2386 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002387 lp.width = (int) (scale * width);
2388 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002389
2390 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2391 fi.draw(mFolderIconCanvas);
2392 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002393 if (fi.getFolder() != null) {
2394 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2395 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002396 }
Adam Cohen268c4752012-06-06 17:47:33 -07002397 // Just in case this image view is still in the drag layer from a previous animation,
2398 // we remove it and re-add it.
2399 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2400 mDragLayer.removeView(mFolderIconImageView);
2401 }
2402 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002403 if (fi.getFolder() != null) {
2404 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002405 }
Adam Cohen268c4752012-06-06 17:47:33 -07002406 }
2407
Adam Cohen2801caf2011-05-13 20:57:39 -07002408 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002409 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002410 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2411 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2412 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2413
Adam Cohenc51934b2011-07-26 21:07:43 -07002414 FolderInfo info = (FolderInfo) fi.getTag();
2415 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2416 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002417 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2418 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002419 }
2420
Adam Cohen268c4752012-06-06 17:47:33 -07002421 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2422 copyFolderIconToImage(fi);
2423 fi.setVisibility(View.INVISIBLE);
2424
Michael Jurka2ecf9952012-06-18 12:52:28 -07002425 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002426 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002427 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2428 oa.start();
2429 }
2430
Adam Cohen268c4752012-06-06 17:47:33 -07002431 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002432 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002433 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2434 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2435 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2436
Adam Cohen268c4752012-06-06 17:47:33 -07002437 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002438
Adam Cohen268c4752012-06-06 17:47:33 -07002439 // We remove and re-draw the FolderIcon in-case it has changed
2440 mDragLayer.removeView(mFolderIconImageView);
2441 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002442 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002443 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002444 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002445 oa.addListener(new AnimatorListenerAdapter() {
2446 @Override
2447 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002448 if (cl != null) {
2449 cl.clearFolderLeaveBehind();
2450 // Remove the ImageView copy of the FolderIcon and make the original visible.
2451 mDragLayer.removeView(mFolderIconImageView);
2452 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002453 }
2454 }
2455 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002456 oa.start();
2457 }
2458
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002460 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 * is animated relative to the specified View. If the View is null, no animation
2462 * is played.
2463 *
2464 * @param folderInfo The FolderInfo describing the folder to open.
2465 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002466 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002467 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002468 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469
Adam Cohena9cf38f2011-05-02 15:36:58 -07002470 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471
Adam Cohen4554ee12011-08-03 16:13:21 -07002472 // Just verify that the folder hasn't already been added to the DragLayer.
2473 // There was a one-off crash where the folder had a parent already.
2474 if (folder.getParent() == null) {
2475 mDragLayer.addView(folder);
2476 mDragController.addDropTarget((DropTarget) folder);
2477 } else {
2478 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2479 folder.getParent() + ").");
2480 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002481 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002482 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002483
2484 // Notify the accessibility manager that this folder "window" has appeared and occluded
2485 // the workspace items
2486 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2487 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002488 }
2489
2490 public void closeFolder() {
2491 Folder folder = mWorkspace.getOpenFolder();
2492 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002493 if (folder.isEditingName()) {
2494 folder.dismissEditingName();
2495 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002496 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002497
2498 // Dismiss the folder cling
2499 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002500 }
2501 }
2502
2503 void closeFolder(Folder folder) {
2504 folder.getInfo().opened = false;
2505
2506 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2507 if (parent != null) {
2508 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2509 shrinkAndFadeInFolderIcon(fi);
2510 }
2511 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002512
2513 // Notify the accessibility manager that this folder "window" has disappeard and no
2514 // longer occludeds the workspace items
2515 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 }
2517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002519 if (!isDraggingEnabled()) return false;
2520 if (isWorkspaceLocked()) return false;
2521 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522
2523 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002524 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 }
2526
Michael Jurka0280c3b2010-09-17 15:00:07 -07002527 resetAddInfo();
2528 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002530 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 return true;
2532 }
2533
Winson Chung3d503fb2011-07-13 17:25:49 -07002534 // The hotseat touch handling does not go through Workspace, and we always allow long press
2535 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002536 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002537 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2538 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002539 if (itemUnderLongClick == null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07002540 if (mWorkspace.hasNonCustomEmptyScreens()) {
2541 // User long pressed on empty space
2542 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2543 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2544 // Disabling reordering until we sort out some issues.
2545 if (mWorkspace.isInOverviewMode()) {
2546 mWorkspace.startReordering(v);
2547 } else {
2548 mWorkspace.enterOverviewMode();
2549 }
Adam Cohendedbd962013-07-11 14:21:49 -07002550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002552 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002554 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 }
2556 }
2557 }
2558 return true;
2559 }
2560
Winson Chung3d503fb2011-07-13 17:25:49 -07002561 boolean isHotseatLayout(View layout) {
2562 return mHotseat != null && layout != null &&
2563 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2564 }
2565 Hotseat getHotseat() {
2566 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002567 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002568 View getOverviewPanel() {
2569 return mOverviewPanel;
2570 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002571 SearchDropTargetBar getSearchBar() {
2572 return mSearchDropTargetBar;
2573 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002574
Winson Chung3d503fb2011-07-13 17:25:49 -07002575 /**
2576 * Returns the CellLayout of the specified container at the specified screen.
2577 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002578 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002579 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2580 if (mHotseat != null) {
2581 return mHotseat.getLayout();
2582 } else {
2583 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002584 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002585 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002586 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002587 }
2588 }
2589
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 Workspace getWorkspace() {
2591 return mWorkspace;
2592 }
2593
Daniel Sandler843e8602010-06-07 14:59:01 -04002594 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002595 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002596 }
2597
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 /**
2599 * Helper method for the cameraZoomIn/cameraZoomOut animations
2600 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002601 * @param scaleFactor The scale factor used for the zoom
2602 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002603 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002604 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002605 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002606 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002607
Winson Chung18f41f82012-05-09 13:28:10 -07002608 void disableWallpaperIfInAllApps() {
2609 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002610 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002611 if (mAppsCustomizeTabHost != null &&
2612 !mAppsCustomizeTabHost.isTransitioning()) {
2613 updateWallpaperVisibility(false);
2614 }
2615 }
2616 }
2617
Craig Mautner360310b2012-10-26 15:13:08 -07002618 private void setWorkspaceBackground(boolean workspace) {
2619 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002620 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002621 }
2622
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002623 void updateWallpaperVisibility(boolean visible) {
2624 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2625 int curflags = getWindow().getAttributes().flags
2626 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2627 if (wpflags != curflags) {
2628 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2629 }
Craig Mautner360310b2012-10-26 15:13:08 -07002630 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002631 }
2632
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002633 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2634 if (v instanceof LauncherTransitionable) {
2635 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2636 }
2637 }
2638
Michael Jurkabed61d22012-02-14 22:51:29 -08002639 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2640 if (v instanceof LauncherTransitionable) {
2641 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2642 }
Winson Chung70442722012-02-10 15:43:22 -08002643
2644 // Update the workspace transition step as well
2645 dispatchOnLauncherTransitionStep(v, 0f);
2646 }
2647
2648 private void dispatchOnLauncherTransitionStep(View v, float t) {
2649 if (v instanceof LauncherTransitionable) {
2650 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2651 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002652 }
2653
2654 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2655 if (v instanceof LauncherTransitionable) {
2656 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2657 }
Winson Chung70442722012-02-10 15:43:22 -08002658
2659 // Update the workspace transition step as well
2660 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002661 }
2662
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002663 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002664 * Things to test when changing the following seven functions.
2665 * - Home from workspace
2666 * - from center screen
2667 * - from other screens
2668 * - Home from all apps
2669 * - from center screen
2670 * - from other screens
2671 * - Back from all apps
2672 * - from center screen
2673 * - from other screens
2674 * - Launch app from workspace and quit
2675 * - with back
2676 * - with home
2677 * - Launch app from all apps and quit
2678 * - with back
2679 * - with home
2680 * - Go to a screen that's not the default, then all
2681 * apps, and launch and app, and go back
2682 * - with back
2683 * -with home
2684 * - On workspace, long press power and go back
2685 * - with back
2686 * - with home
2687 * - On all apps, long press power and go back
2688 * - with back
2689 * - with home
2690 * - On workspace, power off
2691 * - On all apps, power off
2692 * - Launch an app and turn off the screen while in that app
2693 * - Go back with home key
2694 * - Go back with back key TODO: make this not go to workspace
2695 * - From all apps
2696 * - From workspace
2697 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2698 * - From all apps
2699 * - From the center workspace
2700 * - From another workspace
2701 */
2702
2703 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002704 * Zoom the camera out from the workspace to reveal 'toView'.
2705 * Assumes that the view to show is anchored at either the very top or very bottom
2706 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002707 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002708 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002709 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002710 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002711 mStateAnimation.cancel();
2712 mStateAnimation = null;
2713 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002714 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002715
Winson Chungf0ea4d32011-06-06 14:27:16 -07002716 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2717 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2718 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002719 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002720 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002721 final int startDelay =
2722 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002723
Michael Jurkab3e22d92011-10-31 15:58:33 -07002724 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002725
Michael Jurkad74c9842011-07-10 12:44:21 -07002726 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002727 Animator workspaceAnim =
2728 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002729
2730 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002731 toView.setScaleX(scale);
2732 toView.setScaleY(scale);
2733 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2734 scaleAnim.
2735 scaleX(1f).scaleY(1f).
2736 setDuration(duration).
2737 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002738
Winson Chungf0ea4d32011-06-06 14:27:16 -07002739 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002740 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002741 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002742 .ofFloat(toView, "alpha", 0f, 1f)
2743 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002744 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002745 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2746 @Override
2747 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002748 if (animation == null) {
2749 throw new RuntimeException("animation is null");
2750 }
Winson Chung70442722012-02-10 15:43:22 -08002751 float t = (Float) animation.getAnimatedValue();
2752 dispatchOnLauncherTransitionStep(fromView, t);
2753 dispatchOnLauncherTransitionStep(toView, t);
2754 }
2755 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002756
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002757 // toView should appear right at the end of the workspace shrink
2758 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002759 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002760 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002761 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002762
2763 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002764 boolean animationCancelled = false;
2765
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002766 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002767 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002768 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002769 // Prepare the position
2770 toView.setTranslationX(0.0f);
2771 toView.setTranslationY(0.0f);
2772 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002773 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002774 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002775 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002776 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002777 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2778 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002779
Adam Cohenf4ddea32011-09-12 13:46:42 -07002780 if (!animationCancelled) {
2781 updateWallpaperVisibility(false);
2782 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002783
2784 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002785 if (mSearchDropTargetBar != null) {
2786 mSearchDropTargetBar.hideSearchBar(false);
2787 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002788 }
2789
Adam Cohencff6af82011-09-13 14:51:53 -07002790 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002791 public void onAnimationCancel(Animator animation) {
2792 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002793 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002794 });
2795
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002796 if (workspaceAnim != null) {
2797 mStateAnimation.play(workspaceAnim);
2798 }
Michael Jurka1899a362011-11-03 13:50:45 -07002799
2800 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002801
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002802 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2803 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002804
2805 // If any of the objects being animated haven't been measured/laid out
2806 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002807 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002808 (mWorkspace.getMeasuredWidth() == 0) ||
2809 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002810 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002811 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002812
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002813 final AnimatorSet stateAnimation = mStateAnimation;
2814 final Runnable startAnimRunnable = new Runnable() {
2815 public void run() {
2816 // Check that mStateAnimation hasn't changed while
2817 // we waited for a layout/draw pass
2818 if (mStateAnimation != stateAnimation)
2819 return;
2820 setPivotsForZoom(toView, scale);
2821 dispatchOnLauncherTransitionStart(fromView, animated, false);
2822 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002823 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002824 }
2825 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002826 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002827 final ViewTreeObserver observer = toView.getViewTreeObserver();
2828 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2829 public void onGlobalLayout() {
2830 startAnimRunnable.run();
2831 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2832 }
2833 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002834 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002835 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002836 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002837 } else {
2838 toView.setTranslationX(0.0f);
2839 toView.setTranslationY(0.0f);
2840 toView.setScaleX(1.0f);
2841 toView.setScaleY(1.0f);
2842 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002843 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002844
Daniel Sandlere4f98912013-06-25 15:13:26 -04002845 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002846 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002847 if (mSearchDropTargetBar != null) {
2848 mSearchDropTargetBar.hideSearchBar(false);
2849 }
Michael Jurkaabded662011-03-04 12:06:57 -08002850 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002851 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002852 dispatchOnLauncherTransitionStart(fromView, animated, false);
2853 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002854 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002855 dispatchOnLauncherTransitionStart(toView, animated, false);
2856 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002857 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002858 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002859 }
2860
2861 /**
2862 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002863 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002864 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002865 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002866 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2867 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002868
Michael Jurkab3e22d92011-10-31 15:58:33 -07002869 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002870 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002871 mStateAnimation.cancel();
2872 mStateAnimation = null;
2873 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002874 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002875
Winson Chungf0ea4d32011-06-06 14:27:16 -07002876 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002877 final int fadeOutDuration =
2878 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002879 final float scaleFactor = (float)
2880 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2881 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002882 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002883 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002884 if (toState == State.WORKSPACE) {
2885 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2886 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002887 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002888 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2889 workspaceAnim = mWorkspace.getChangeStateAnimation(
2890 Workspace.State.SPRING_LOADED, animated);
2891 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002892
Michael Jurkab3e22d92011-10-31 15:58:33 -07002893 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002894 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002895 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002896 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002897 final LauncherViewPropertyAnimator scaleAnim =
2898 new LauncherViewPropertyAnimator(fromView);
2899 scaleAnim.
2900 scaleX(scaleFactor).scaleY(scaleFactor).
2901 setDuration(duration).
2902 setInterpolator(new Workspace.ZoomInInterpolator());
2903
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002904 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002905 .ofFloat(fromView, "alpha", 1f, 0f)
2906 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002907 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002908 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2909 @Override
2910 public void onAnimationUpdate(ValueAnimator animation) {
2911 float t = 1f - (Float) animation.getAnimatedValue();
2912 dispatchOnLauncherTransitionStep(fromView, t);
2913 dispatchOnLauncherTransitionStep(toView, t);
2914 }
2915 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002916
Michael Jurka2ecf9952012-06-18 12:52:28 -07002917 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002918
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002919 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2920 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002921 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002922
2923 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002924 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002925 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002926 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002927 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002928 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2929 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002930 if (onCompleteRunnable != null) {
2931 onCompleteRunnable.run();
2932 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002933 mAppsCustomizeContent.updateCurrentPageScroll();
2934 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002935 }
2936 });
2937
Winson Chungf0ea4d32011-06-06 14:27:16 -07002938 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002939 if (workspaceAnim != null) {
2940 mStateAnimation.play(workspaceAnim);
2941 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002942 dispatchOnLauncherTransitionStart(fromView, animated, true);
2943 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002944 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002945 } else {
2946 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002947 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002948 dispatchOnLauncherTransitionStart(fromView, animated, true);
2949 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002950 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002951 dispatchOnLauncherTransitionStart(toView, animated, true);
2952 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002953 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002954 }
2955
Michael Jurkae326f182011-11-21 14:05:46 -08002956 @Override
2957 public void onTrimMemory(int level) {
2958 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002959 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002960 mAppsCustomizeTabHost.onTrimMemory();
2961 }
2962 }
2963
Winson Chung18f41f82012-05-09 13:28:10 -07002964 @Override
2965 public void onWindowFocusChanged(boolean hasFocus) {
2966 if (!hasFocus) {
2967 // When another window occludes launcher (like the notification shade, or recents),
2968 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2969 updateWallpaperVisibility(true);
2970 } else {
2971 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002972 mWorkspace.postDelayed(new Runnable() {
2973 @Override
2974 public void run() {
2975 disableWallpaperIfInAllApps();
2976 }
2977 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002978 }
2979 }
2980
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002981 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002982 showWorkspace(animated, null);
2983 }
2984
2985 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002986 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002987 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002988 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002989 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002990
Winson Chungc7d2b602012-05-16 17:02:20 -07002991 // Show the search bar (only animate if we were showing the drop target bar in spring
2992 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002993 if (mSearchDropTargetBar != null) {
2994 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2995 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002996
Michael Jurkab3e22d92011-10-31 15:58:33 -07002997 // Set focus to the AppsCustomize button
2998 if (mAllAppsButton != null) {
2999 mAllAppsButton.requestFocus();
3000 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003001 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003002
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003003 // Change the state *after* we've called all the transition code
3004 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003005
Winson Chung5fb63472011-02-02 17:03:37 -08003006 // Resume the auto-advance of widgets
3007 mUserPresent = true;
3008 updateRunning();
3009
alanv1d4fde62012-10-17 13:15:47 -07003010 // Send an accessibility event to announce the context change
3011 getWindow().getDecorView()
3012 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003013
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003014 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003015 }
3016
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003017 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003018 }
3019
Michael Jurkab3e22d92011-10-31 15:58:33 -07003020 void showAllApps(boolean animated) {
3021 if (mState != State.WORKSPACE) return;
3022
3023 showAppsCustomizeHelper(animated, false);
3024 mAppsCustomizeTabHost.requestFocus();
3025
Michael Jurkab3e22d92011-10-31 15:58:33 -07003026 // Change the state *after* we've called all the transition code
3027 mState = State.APPS_CUSTOMIZE;
3028
3029 // Pause the auto-advance of widgets until we are out of AllApps
3030 mUserPresent = false;
3031 updateRunning();
3032 closeFolder();
3033
3034 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003035 getWindow().getDecorView()
3036 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003037 }
3038
Adam Cohen7777d962011-08-18 18:58:38 -07003039 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003040 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003041 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003042 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003043 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003044 }
Adam Cohen7777d962011-08-18 18:58:38 -07003045
Adam Cohened66b2b2012-01-23 17:28:51 -08003046 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3047 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003048 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3049
Winson Chunge7a03942011-08-05 15:05:12 -07003050 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003051 @Override
3052 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003053 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003054 // Before we show workspace, hide all apps again because
3055 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3056 // clean up our state transition functions
3057 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003058 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003059 } else {
3060 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003061 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003062 }
3063 }, (extendedDelay ?
3064 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3065 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3066 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003067
Michael Jurkad3ef3062010-11-23 16:23:58 -08003068 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003069 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003070 final boolean animated = true;
3071 final boolean springLoaded = true;
3072 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003073 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003074 }
3075 // Otherwise, we are not in spring loaded mode, so don't do anything.
3076 }
3077
Michael Jurkab3e22d92011-10-31 15:58:33 -07003078 void lockAllApps() {
3079 // TODO
3080 }
3081
3082 void unlockAllApps() {
3083 // TODO
3084 }
3085
Winson Chungf0ea4d32011-06-06 14:27:16 -07003086 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003087 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003088 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003089 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003090 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003091 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003092 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003093 int duration = 0;
3094 if (mSearchDropTargetBar != null) {
3095 duration = mSearchDropTargetBar.getTransitionInDuration();
3096 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003097 mHotseat.animate().alpha(1f).setDuration(duration);
3098 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003099 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003100 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003101 }
3102 }
3103 }
3104
3105 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003106 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003107 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003108 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003109 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003110 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003111 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003112 int duration = 0;
3113 if (mSearchDropTargetBar != null) {
3114 duration = mSearchDropTargetBar.getTransitionOutDuration();
3115 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003116 mHotseat.animate().alpha(0f).setDuration(duration);
3117 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003118 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003119 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003120 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003121 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003122 }
3123
Patrick Dubroy5f445422011-02-18 14:35:21 -08003124 /**
3125 * Add an item from all apps or customize onto the given workspace screen.
3126 * If layout is null, add to the current screen.
3127 */
3128 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003129 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003130 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003131 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003132 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003133
Winson Chungdff8ebb2011-09-08 17:25:31 -07003134 /** Maps the current orientation to an index for referencing orientation correct global icons */
3135 private int getCurrentOrientationIndexForGlobalIcons() {
3136 // default - 0, landscape - 1
3137 switch (getResources().getConfiguration().orientation) {
3138 case Configuration.ORIENTATION_LANDSCAPE:
3139 return 1;
3140 default:
3141 return 0;
3142 }
3143 }
3144
Michael Jurkaae65ee32012-04-30 11:45:54 -07003145 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003146 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003147 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003148 // Look for the toolbar icon specified in the activity meta-data
3149 Bundle metaData = packageManager.getActivityInfo(
3150 activityName, PackageManager.GET_META_DATA).metaData;
3151 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003152 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003153 if (iconResId != 0) {
3154 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003155 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003156 }
3157 }
3158 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003159 // This can happen if the activity defines an invalid drawable
3160 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3161 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003162 } catch (Resources.NotFoundException nfe) {
3163 // This can happen if the activity defines an invalid drawable
3164 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3165 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003166 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003167 return null;
3168 }
3169
3170 // if successful in getting icon, return it; otherwise, set button to use default drawable
3171 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003172 int buttonId, ComponentName activityName, int fallbackDrawableId,
3173 String toolbarResourceName) {
3174 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003175 Resources r = getResources();
3176 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3177 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003178
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003179 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003180 // If we were unable to find the icon via the meta-data, use a generic one
3181 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003182 toolbarIcon = r.getDrawable(fallbackDrawableId);
3183 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003184 if (button != null) {
3185 button.setCompoundDrawables(toolbarIcon, null, null, null);
3186 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003187 return null;
3188 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003189 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003190 if (button != null) {
3191 button.setCompoundDrawables(toolbarIcon, null, null, null);
3192 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003193 return toolbarIcon.getConstantState();
3194 }
3195 }
3196
3197 // if successful in getting icon, return it; otherwise, set button to use default drawable
3198 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003199 int buttonId, ComponentName activityName, int fallbackDrawableId,
3200 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003201 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003202 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003203
Michael Jurka19e0fc52011-07-22 18:00:21 -07003204 if (button != null) {
3205 // If we were unable to find the icon via the meta-data, use a
3206 // generic one
3207 if (toolbarIcon == null) {
3208 button.setImageResource(fallbackDrawableId);
3209 } else {
3210 button.setImageDrawable(toolbarIcon);
3211 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003212 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003213
3214 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3215
Michael Jurka0423dcf2010-10-05 14:56:18 -07003216 }
3217
Winson Chung1b884092012-06-08 17:13:02 -07003218 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003219 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003220 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003221 }
3222
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003223 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003224 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003225 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003226 }
3227
Winson Chungbb185bd2011-11-21 12:31:42 -08003228 private void invalidatePressedFocusedStates(View container, View button) {
3229 if (container instanceof HolographicLinearLayout) {
3230 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3231 layout.invalidatePressedFocusedStates();
3232 } else if (button instanceof HolographicImageView) {
3233 HolographicImageView view = (HolographicImageView) button;
3234 view.invalidatePressedFocusedStates();
3235 }
3236 }
3237
Winson Chungc51db6a2011-10-05 11:44:49 -07003238 private boolean updateGlobalSearchIcon() {
3239 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003240 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003241 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003242 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003243
Winson Chung1cad91e2011-05-25 17:41:01 -07003244 final SearchManager searchManager =
3245 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3246 ComponentName activityName = searchManager.getGlobalSearchActivity();
3247 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003248 int coi = getCurrentOrientationIndexForGlobalIcons();
3249 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003250 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3251 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3252 if (sGlobalSearchIcon[coi] == null) {
3253 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3254 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3255 TOOLBAR_ICON_METADATA_NAME);
3256 }
3257
Winson Chungc51db6a2011-10-05 11:44:49 -07003258 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3259 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003260 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003262 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003263 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003264 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3265 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3266 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003267 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003268 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003269 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003270 }
3271 }
3272
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003273 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003274 final View searchButtonContainer = findViewById(R.id.search_button_container);
3275 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003276 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003277 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003278 }
3279
Winson Chungc51db6a2011-10-05 11:44:49 -07003280 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003281 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003282 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003283
Winson Chungc51db6a2011-10-05 11:44:49 -07003284 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003285 final SearchManager searchManager =
3286 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3287 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3288
3289 ComponentName activityName = null;
3290 if (globalSearchActivity != null) {
3291 // Check if the global search activity handles voice search
3292 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3293 intent.setPackage(globalSearchActivity.getPackageName());
3294 activityName = intent.resolveActivity(getPackageManager());
3295 }
3296
3297 if (activityName == null) {
3298 // Fallback: check if an activity other than the global search activity
3299 // resolves this
3300 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3301 activityName = intent.resolveActivity(getPackageManager());
3302 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003303 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003304 int coi = getCurrentOrientationIndexForGlobalIcons();
3305 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003306 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3307 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3308 if (sVoiceSearchIcon[coi] == null) {
3309 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3310 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3311 TOOLBAR_ICON_METADATA_NAME);
3312 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003313 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003314 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003315 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003316 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003317 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003318 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003319 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003320 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003321 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003322 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003323 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003324 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003325
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003326 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003327 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3328 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003329 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003330 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003331 }
3332
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003333 public void setVoiceButtonProxyVisible(boolean visible) {
3334 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3335 if (voiceButtonProxy != null) {
3336 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3337 }
3338 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003339 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003340 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003341 */
3342 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003343 final View marketButton = findViewById(R.id.market_button);
3344 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3345 // Find the app market activity by resolving an intent.
3346 // (If multiple app markets are installed, it will return the ResolverActivity.)
3347 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003348 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003349 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003350 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003351 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003352 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3353 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003354 marketButton.setVisibility(View.VISIBLE);
3355 } else {
3356 // We should hide and disable the view so that we don't try and restore the visibility
3357 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3358 marketButton.setVisibility(View.GONE);
3359 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003360 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003361 }
3362
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003363 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003364 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3365 Resources r = getResources();
3366 Drawable marketIconDrawable = d.newDrawable(r);
3367 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3368 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3369 marketIconDrawable.setBounds(0, 0, w, h);
3370
3371 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003372 }
3373
Michael Jurkad7c28052012-04-27 15:43:36 -07003374 @Override
3375 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003376 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003377 final List<CharSequence> text = event.getText();
3378 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003379 // Populate event with a fake title based on the current state.
3380 if (mState == State.APPS_CUSTOMIZE) {
3381 text.add(getString(R.string.all_apps_button_label));
3382 } else {
3383 text.add(getString(R.string.all_apps_home_button_label));
3384 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003385 return result;
3386 }
3387
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003388 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003389 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003390 */
3391 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3392 @Override
3393 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003394 closeSystemDialogs();
3395 }
3396 }
3397
3398 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003399 * Receives notifications whenever the appwidgets are reset.
3400 */
3401 private class AppWidgetResetObserver extends ContentObserver {
3402 public AppWidgetResetObserver() {
3403 super(new Handler());
3404 }
3405
3406 @Override
3407 public void onChange(boolean selfChange) {
3408 onAppWidgetReset();
3409 }
3410 }
3411
3412 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003413 * If the activity is currently paused, signal that we need to run the passed Runnable
3414 * in onResume.
3415 *
3416 * This needs to be called from incoming places where resources might have been loaded
3417 * while we are paused. That is becaues the Configuration might be wrong
3418 * when we're not running, and if it comes back to what it was when we
3419 * were paused, we are not restarted.
3420 *
3421 * Implementation of the method from LauncherModel.Callbacks.
3422 *
3423 * @return true if we are currently paused. The caller might be able to
3424 * skip some work in that case since we will come back again.
3425 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003426 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003427 if (mPaused) {
3428 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003429 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003430 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003431 }
3432 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003433 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003434 return true;
3435 } else {
3436 return false;
3437 }
3438 }
3439
Michael Jurkac402cd92013-05-20 15:49:32 +02003440 private boolean waitUntilResume(Runnable run) {
3441 return waitUntilResume(run, false);
3442 }
3443
Michael Jurka1e2f4652013-07-08 18:03:46 -07003444 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003445 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003446 }
3447
Michael Jurka7607c2f2013-04-03 14:33:19 -07003448 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003449 * If the activity is currently paused, signal that we need to re-run the loader
3450 * in onResume.
3451 *
3452 * This needs to be called from incoming places where resources might have been loaded
3453 * while we are paused. That is becaues the Configuration might be wrong
3454 * when we're not running, and if it comes back to what it was when we
3455 * were paused, we are not restarted.
3456 *
3457 * Implementation of the method from LauncherModel.Callbacks.
3458 *
3459 * @return true if we are currently paused. The caller might be able to
3460 * skip some work in that case since we will come back again.
3461 */
3462 public boolean setLoadOnResume() {
3463 if (mPaused) {
3464 Log.i(TAG, "setLoadOnResume");
3465 mOnResumeNeedsLoad = true;
3466 return true;
3467 } else {
3468 return false;
3469 }
3470 }
3471
3472 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003474 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003476 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003477 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003478 } else {
3479 return SCREEN_COUNT / 2;
3480 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003482
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 /**
3484 * Refreshes the shortcuts shown on the workspace.
3485 *
3486 * Implementation of the method from LauncherModel.Callbacks.
3487 */
3488 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003489 // If we're starting binding all over again, clear any bind calls we'd postponed in
3490 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3491 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003492 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003493
Winson Chungd64d1762013-08-20 14:37:16 -07003494 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003495 mWorkspace.clearDropTargets();
Winson Chungd64d1762013-08-20 14:37:16 -07003496 mWorkspace.removeAllViews();
3497
Michael Jurka05bf6442011-11-30 20:28:53 -08003498 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003499 if (mHotseat != null) {
3500 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003501 }
3502 }
3503
Adam Cohendcd297f2013-06-18 13:13:40 -07003504 @Override
3505 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003506 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003507
3508 // Create the new empty page
Winson Chung64359a52013-07-08 17:17:08 -07003509 mWorkspace.addExtraEmptyScreen();
Winson Chung0e6a7132013-08-23 12:55:10 -07003510
3511 // Create the custom content page (this call updates mDefaultScreen which calls
3512 // setCurrentPage() so ensure that all pages are added before calling this)
3513 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3514 mWorkspace.createCustomContentPage();
3515 }
Winson Chung64359a52013-07-08 17:17:08 -07003516 }
3517
3518 @Override
3519 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003520 int count = orderedScreenIds.size();
3521 for (int i = 0; i < count; i++) {
Winson Chung76828c82013-08-19 15:43:29 -07003522 Log.w(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")");
Adam Cohen89bddfa2013-08-20 11:57:13 -07003523 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003524 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003525 }
3526
Adam Cohen39a06042013-07-19 14:30:12 -07003527 private boolean shouldShowWeightWatcher() {
3528 String spKey = LauncherAppState.getSharedPreferencesKey();
3529 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003530 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003531
3532 return show;
3533 }
3534
3535 private void toggleShowWeightWatcher() {
3536 String spKey = LauncherAppState.getSharedPreferencesKey();
3537 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3538 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3539
3540 show = !show;
3541
3542 SharedPreferences.Editor editor = sp.edit();
3543 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3544 editor.commit();
3545
3546 if (mWeightWatcher != null) {
3547 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3548 }
3549 }
3550
Winson Chungd64d1762013-08-20 14:37:16 -07003551 public void bindAppsAdded(final ArrayList<Long> newScreens,
3552 final ArrayList<ItemInfo> addNotAnimated,
3553 final ArrayList<ItemInfo> addAnimated) {
3554 Runnable r = new Runnable() {
3555 public void run() {
3556 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
3557 }
3558 };
3559 if (waitUntilResume(r)) {
3560 return;
3561 }
3562
3563 Log.w(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")");
3564
3565 // Add the new screens
3566 bindAddScreens(newScreens);
3567
3568 // We add the items without animation on non-visible pages, and with
3569 // animations on the new page (which we will try and snap to).
3570 if (!addNotAnimated.isEmpty()) {
3571 bindItems(addNotAnimated, 0,
3572 addNotAnimated.size(), false);
3573 }
3574 if (!addAnimated.isEmpty()) {
3575 bindItems(addAnimated, 0,
3576 addAnimated.size(), true);
3577 }
3578 }
3579
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003580 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003581 * Bind the items start-end from the list.
3582 *
3583 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003584 */
Winson Chung64359a52013-07-08 17:17:08 -07003585 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3586 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003587 Runnable r = new Runnable() {
3588 public void run() {
3589 bindItems(shortcuts, start, end, forceAnimateIcons);
3590 }
3591 };
3592 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003593 return;
3594 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003595
Winson Chungf0c6ae02012-03-21 16:10:31 -07003596 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003597 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3598 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003599 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003600 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003601 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003602 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003604
3605 // Short circuit if we are loading dock items for a configuration which has no dock
3606 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3607 mHotseat == null) {
3608 continue;
3609 }
3610
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 switch (item.itemType) {
3612 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3613 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003614 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003615 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003616
Winson Chung64359a52013-07-08 17:17:08 -07003617 /*
3618 * TODO: FIX collision case
3619 */
Winson Chungce376632013-07-11 16:12:41 -07003620 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3621 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3622 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3623 throw new RuntimeException("OCCUPIED");
3624 }
Winson Chung64359a52013-07-08 17:17:08 -07003625 }
3626
Adam Cohendcd297f2013-06-18 13:13:40 -07003627 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3628 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003629 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003630 // Animate all the applications up now
3631 shortcut.setAlpha(0f);
3632 shortcut.setScaleX(0f);
3633 shortcut.setScaleY(0f);
3634 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003635 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003636 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003638 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003639 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003640 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003641 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003642 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3643 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003644 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003645 default:
3646 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003648 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003649
Winson Chung997a9232013-07-24 15:33:46 -07003650 if (animateIcons) {
3651 // Animate to the correct page
3652 if (newShortcutsScreenId > -1) {
3653 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3654 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3655 if (newShortcutsScreenId != currentScreenId) {
3656 mWorkspace.snapToPage(newScreenIndex);
3657 }
3658 }
3659
Winson Chung64359a52013-07-08 17:17:08 -07003660 // We post the animation slightly delayed to prevent slowdowns when we are loading
3661 // right after we return to launcher.
3662 mWorkspace.postDelayed(new Runnable() {
3663 public void run() {
3664 anim.playTogether(bounceAnims);
3665 anim.start();
3666 }
3667 }, NEW_APPS_ANIMATION_DELAY);
3668 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003669 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003670 }
3671
Joe Onorato9c1289c2009-08-17 11:03:03 -04003672 /**
3673 * Implementation of the method from LauncherModel.Callbacks.
3674 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003675 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003676 Runnable r = new Runnable() {
3677 public void run() {
3678 bindFolders(folders);
3679 }
3680 };
3681 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003682 return;
3683 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003684 sFolders.clear();
3685 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003686 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003687
3688 /**
3689 * Add the views for a widget to the workspace.
3690 *
3691 * Implementation of the method from LauncherModel.Callbacks.
3692 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003693 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003694 Runnable r = new Runnable() {
3695 public void run() {
3696 bindAppWidget(item);
3697 }
3698 };
3699 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003700 return;
3701 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003702
Daniel Sandler843e8602010-06-07 14:59:01 -04003703 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3704 if (DEBUG_WIDGETS) {
3705 Log.d(TAG, "bindAppWidget: " + item);
3706 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 final Workspace workspace = mWorkspace;
3708
3709 final int appWidgetId = item.appWidgetId;
3710 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003711 if (DEBUG_WIDGETS) {
3712 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3713 }
3714
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3716
Joe Onorato9c1289c2009-08-17 11:03:03 -04003717 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003718 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719
Adam Cohendcd297f2013-06-18 13:13:40 -07003720 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003721 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003722 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3723
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 workspace.requestLayout();
3725
Daniel Sandler843e8602010-06-07 14:59:01 -04003726 if (DEBUG_WIDGETS) {
3727 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3728 + (SystemClock.uptimeMillis()-start) + "ms");
3729 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 }
3731
Adam Cohen1462de32012-07-24 22:34:36 -07003732 public void onPageBoundSynchronously(int page) {
3733 mSynchronouslyBoundPages.add(page);
3734 }
3735
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736 /**
3737 * Callback saying that there aren't any more items to bind.
3738 *
3739 * Implementation of the method from LauncherModel.Callbacks.
3740 */
Adam Cohene25af792013-06-06 23:08:25 -07003741 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003742 Runnable r = new Runnable() {
3743 public void run() {
3744 finishBindingItems(upgradePath);
3745 }
3746 };
3747 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003748 return;
3749 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003750 if (mSavedState != null) {
3751 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003752 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003754 mSavedState = null;
3755 }
3756
Adam Cohen1462de32012-07-24 22:34:36 -07003757 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003758
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003759 // If we received the result of any pending adds while the loader was running (e.g. the
3760 // widget configuration forced an orientation change), process them now.
3761 for (int i = 0; i < sPendingAddList.size(); i++) {
3762 completeAdd(sPendingAddList.get(i));
3763 }
3764 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765
Winson Chungc51db6a2011-10-05 11:44:49 -07003766 // Update the market app icon as necessary (the other icons will be managed in response to
3767 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003768 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003769
Winson Chungf0c6ae02012-03-21 16:10:31 -07003770 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003771 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003772 mWorkspace.stripDuplicateApps();
3773 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003774 }
Adam Cohen99894d92013-06-14 11:22:59 -07003775
Adam Cohen66a01fd2013-06-11 12:48:00 -07003776 mWorkspace.post(new Runnable() {
3777 @Override
3778 public void run() {
3779 onFinishBindingItems();
3780 }
3781 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003782 }
3783
Winson Chunga2413752012-04-03 14:22:34 -07003784 private boolean canRunNewAppsAnimation() {
3785 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3786 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3787 }
3788
Winson Chungc9168342013-06-26 14:54:55 -07003789 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3790 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3791 PropertyValuesHolder.ofFloat("alpha", 1f),
3792 PropertyValuesHolder.ofFloat("scaleX", 1f),
3793 PropertyValuesHolder.ofFloat("scaleY", 1f));
3794 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3795 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3796 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3797 return bounceAnim;
3798 }
3799
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003800 @Override
3801 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003802 boolean searchVisible = updateGlobalSearchIcon();
3803 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003804 if (mSearchDropTargetBar != null) {
3805 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3806 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003807 }
3808
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003809 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 * Add the icons for all apps.
3811 *
3812 * Implementation of the method from LauncherModel.Callbacks.
3813 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003814 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003815 if (mIntentsOnWorkspaceFromUpgradePath != null) {
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003816 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3817 getHotseat().addAllAppsFolder(mIconCache, apps,
3818 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3819 }
Winson Chung64359a52013-07-08 17:17:08 -07003820 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003821 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003822 }
3823
3824 /**
3825 * A package was updated.
3826 *
3827 * Implementation of the method from LauncherModel.Callbacks.
3828 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003829 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003830 Runnable r = new Runnable() {
3831 public void run() {
3832 bindAppsUpdated(apps);
3833 }
3834 };
3835 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003836 return;
3837 }
3838
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003839 if (mWorkspace != null) {
3840 mWorkspace.updateShortcuts(apps);
3841 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003842 }
3843
3844 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003845 * A package was uninstalled. We take both the super set of packageNames
3846 * in addition to specific applications to remove, the reason being that
3847 * this can be called when a package is updated as well. In that scenario,
3848 * we only remove specific components from the workspace, where as
3849 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850 *
3851 * Implementation of the method from LauncherModel.Callbacks.
3852 */
Winson Chung83892cc2013-05-01 16:53:33 -07003853 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3854 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003855 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003856 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003857 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003858 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003859 }
Winson Chungd64d1762013-08-20 14:37:16 -07003860 };
3861 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003862 return;
3863 }
3864
Winson Chung64359a52013-07-08 17:17:08 -07003865 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003866 mWorkspace.removeItemsByPackageName(packageNames);
3867 } else {
3868 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003869 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003870
Winson Chunga1820962011-10-03 16:31:06 -07003871 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003872 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 }
Joe Onoratobe386092009-11-17 17:32:16 -08003874
3875 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003876 * A number of packages were updated.
3877 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003878 private ArrayList<Object> mWidgetsAndShortcuts;
3879 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003880 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003881 bindPackagesUpdated(mWidgetsAndShortcuts);
3882 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003883 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003884 };
3885
3886 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3887 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3888 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003889 return;
3890 }
3891
Winson Chung64359a52013-07-08 17:17:08 -07003892 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003893 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003894 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003895 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003896 }
3897
Winson Chung400438b2011-01-16 17:53:48 -08003898 private int mapConfigurationOriActivityInfoOri(int configOri) {
3899 final Display d = getWindowManager().getDefaultDisplay();
3900 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3901 switch (d.getRotation()) {
3902 case Surface.ROTATION_0:
3903 case Surface.ROTATION_180:
3904 // We are currently in the same basic orientation as the natural orientation
3905 naturalOri = configOri;
3906 break;
3907 case Surface.ROTATION_90:
3908 case Surface.ROTATION_270:
3909 // We are currently in the other basic orientation to the natural orientation
3910 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3911 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3912 break;
3913 }
3914
3915 int[] oriMap = {
3916 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3917 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3918 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3919 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3920 };
3921 // Since the map starts at portrait, we need to offset if this device's natural orientation
3922 // is landscape.
3923 int indexOffset = 0;
3924 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3925 indexOffset = 1;
3926 }
3927 return oriMap[(d.getRotation() + indexOffset) % 4];
3928 }
Adam Cohen446e9402011-09-15 18:21:21 -07003929
Winson Chung4b919f82012-05-01 10:44:08 -07003930 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003931 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003932 getResources().getBoolean(R.bool.allow_rotation);
3933 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003934 }
Winson Chung4b919f82012-05-01 10:44:08 -07003935 public void lockScreenOrientation() {
3936 if (isRotationEnabled()) {
3937 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3938 .getConfiguration().orientation));
3939 }
3940 }
3941 public void unlockScreenOrientation(boolean immediate) {
3942 if (isRotationEnabled()) {
3943 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003944 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003945 } else {
3946 mHandler.postDelayed(new Runnable() {
3947 public void run() {
3948 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3949 }
3950 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003951 }
Winson Chung4b919f82012-05-01 10:44:08 -07003952 }
Winson Chung400438b2011-01-16 17:53:48 -08003953 }
3954
Winson Chung82f55532011-08-09 14:14:23 -07003955 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003956 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003957 if (DISABLE_CLINGS) {
3958 return false;
3959 }
3960
Brett Chabot2a9e2282011-08-23 15:03:13 -07003961 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003962 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003963
Michael Jurkae233a8b2013-03-19 13:49:20 +01003964 // Restricted secondary users (child mode) will potentially have very few apps
3965 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003966// boolean supportsLimitedUsers =
3967// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3968// Account[] accounts = AccountManager.get(this).getAccounts();
3969// if (supportsLimitedUsers && accounts.length == 0) {
3970// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3971// Bundle restrictions = um.getUserRestrictions();
3972// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3973// return false;
3974// }
3975// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003976 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003977 }
Michael Jurka22143132012-10-04 17:42:38 +02003978
Winson Chung7d7541e2011-09-16 20:14:36 -07003979 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003980 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003981 if (cling != null) {
3982 cling.init(this, positionData);
3983 cling.setVisibility(View.VISIBLE);
3984 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3985 if (animate) {
3986 cling.buildLayer();
3987 cling.setAlpha(0f);
3988 cling.animate()
3989 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003990 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003991 .setDuration(SHOW_CLING_DURATION)
3992 .setStartDelay(delay)
3993 .start();
3994 } else {
3995 cling.setAlpha(1f);
3996 }
Michael Jurka22143132012-10-04 17:42:38 +02003997 cling.setFocusableInTouchMode(true);
3998 cling.post(new Runnable() {
3999 public void run() {
4000 cling.setFocusable(true);
4001 cling.requestFocus();
4002 }
4003 });
4004 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4005 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004006 }
4007 return cling;
4008 }
Michael Jurka22143132012-10-04 17:42:38 +02004009
Winson Chung7d7541e2011-09-16 20:14:36 -07004010 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004011 // To catch cases where siblings of top-level views are made invisible, just check whether
4012 // the cling is directly set to GONE before dismissing it.
4013 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004014 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004015 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004016 anim.addListener(new AnimatorListenerAdapter() {
4017 public void onAnimationEnd(Animator animation) {
4018 cling.setVisibility(View.GONE);
4019 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004020 // We should update the shared preferences on a background thread
4021 new Thread("dismissClingThread") {
4022 public void run() {
4023 SharedPreferences.Editor editor = mSharedPrefs.edit();
4024 editor.putBoolean(flag, true);
4025 editor.commit();
4026 }
4027 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004028 };
4029 });
4030 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004031 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004032 }
4033 }
Michael Jurka22143132012-10-04 17:42:38 +02004034
Winson Chung9d9d74f2011-09-19 11:49:12 -07004035 private void removeCling(int id) {
4036 final View cling = findViewById(id);
4037 if (cling != null) {
4038 final ViewGroup parent = (ViewGroup) cling.getParent();
4039 parent.post(new Runnable() {
4040 @Override
4041 public void run() {
4042 parent.removeView(cling);
4043 }
4044 });
Michael Jurka22143132012-10-04 17:42:38 +02004045 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004046 }
4047 }
Michael Jurka974c3862012-05-22 22:00:31 -07004048
4049 private boolean skipCustomClingIfNoAccounts() {
4050 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4051 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4052 if (customCling) {
4053 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004054 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004055 Account[] accounts = am.getAccountsByType("com.google");
4056 return accounts.length == 0;
4057 }
4058 return false;
4059 }
4060
Winson Chung7d7541e2011-09-16 20:14:36 -07004061 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004062 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004063 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004064 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4065 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004066 // If we're not using the default workspace layout, replace workspace cling
4067 // with a custom workspace cling (usually specified in an overlay)
4068 // For now, only do this on tablets
4069 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004070 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004071 // Use a custom cling
4072 View cling = findViewById(R.id.workspace_cling);
4073 ViewGroup clingParent = (ViewGroup) cling.getParent();
4074 int clingIndex = clingParent.indexOfChild(cling);
4075 clingParent.removeViewAt(clingIndex);
4076 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4077 clingParent.addView(customCling, clingIndex);
4078 customCling.setId(R.id.workspace_cling);
4079 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004080 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004081 } else {
4082 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004083 }
4084 }
4085 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004086 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004087 if (isClingsEnabled() &&
4088 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004089 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004090 } else {
4091 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004092 }
4093 }
4094 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004095 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004096 if (isClingsEnabled() &&
4097 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4098 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004099 } else {
4100 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004101 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004102 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004103 }
Michael Jurka104c4562013-07-08 18:03:46 -07004104 protected SharedPreferences getSharedPrefs() {
4105 return mSharedPrefs;
4106 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004107 public boolean isFolderClingVisible() {
4108 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004109 if (cling != null) {
4110 return cling.getVisibility() == View.VISIBLE;
4111 }
4112 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004113 }
Winson Chung82f55532011-08-09 14:14:23 -07004114 public void dismissWorkspaceCling(View v) {
4115 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004116 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004117 }
4118 public void dismissAllAppsCling(View v) {
4119 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004120 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4121 }
4122 public void dismissFolderCling(View v) {
4123 Cling cling = (Cling) findViewById(R.id.folder_cling);
4124 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004125 }
4126
Winson Chung80baf5a2010-08-09 16:03:15 -07004127 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004128 * Prints out out state for debugging.
4129 */
4130 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004131 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004132 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004133 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4134 Log.d(TAG, "mRestoring=" + mRestoring);
4135 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4136 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004137 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004138 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004139
Winson Chung785d2eb2011-04-14 16:08:02 -07004140 if (mAppsCustomizeContent != null) {
4141 mAppsCustomizeContent.dumpState();
4142 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004143 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004144 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004145
4146 @Override
4147 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4148 super.dump(prefix, fd, writer, args);
4149 writer.println(" ");
4150 writer.println("Debug logs: ");
4151 for (int i = 0; i < sDumpLogs.size(); i++) {
4152 writer.println(" " + sDumpLogs.get(i));
4153 }
4154 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004155
4156 public static void dumpDebugLogsToConsole() {
4157 Log.d(TAG, "");
4158 Log.d(TAG, "*********************");
4159 Log.d(TAG, "Launcher debug logs: ");
4160 for (int i = 0; i < sDumpLogs.size(); i++) {
4161 Log.d(TAG, " " + sDumpLogs.get(i));
4162 }
4163 Log.d(TAG, "*********************");
4164 Log.d(TAG, "");
4165 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004166}
Michael Jurka2763be32011-02-24 11:19:57 -08004167
Michael Jurkaabded662011-03-04 12:06:57 -08004168interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004169 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004170 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004171 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004172 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004173 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004174}