blob: dd9e793fbcbec18b8e70135627fd72f34f92350f [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;
Cristina Stancu476493b2013-08-07 17:20:14 +0100244 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
248 // scroll issues (because the workspace may not have been measured yet) and extra work.
249 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
250 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
252 private SpannableStringBuilder mDefaultKeySsb = null;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private boolean mWorkspaceLoading = true;
255
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800256 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private boolean mRestoring;
258 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700259 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurka1e2f4652013-07-08 18:03:46 -0700261 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
263
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // Keep track of whether the user has left launcher
265 private static boolean sPausedFromUserAction = false;
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedInstanceState;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800271 private boolean mUserPresent = true;
272 private boolean mVisible = false;
273 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700274 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700276 private static LocaleConfiguration sLocaleConfiguration = null;
277
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700278 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700279
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700280 private Intent mAppMarketIntent = null;
281
Adam Cohended9f8d2010-11-03 13:25:16 -0700282 // Related to the auto-advancing of widgets
283 private final int ADVANCE_MSG = 1;
284 private final int mAdvanceInterval = 20000;
285 private final int mAdvanceStagger = 250;
286 private long mAutoAdvanceSentTime;
287 private long mAutoAdvanceTimeLeft = -1;
288 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
289 new HashMap<View, AppWidgetProviderInfo>();
290
Winson Chung400438b2011-01-16 17:53:48 -0800291 // Determines how long to wait after a rotation before restoring the screen orientation to
292 // match the sensor state.
293 private final int mRestoreScreenOrientationDelay = 500;
294
Michael Jurka4ef207b2010-11-29 17:05:45 -0800295 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700296 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
297 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
298 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800299
Craig Mautner360310b2012-10-26 15:13:08 -0700300 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700301
Adam Cohen1462de32012-07-24 22:34:36 -0700302 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
303
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700304 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
305
Winson Chung46353de2012-02-16 14:05:10 -0800306 // We only want to get the SharedPreferences once since it does an FS stat each time we get
307 // it from the context.
308 private SharedPreferences mSharedPrefs;
309
Adam Cohene25af792013-06-06 23:08:25 -0700310 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
311
Winson Chungf0c6ae02012-03-21 16:10:31 -0700312 // Holds the page that we need to animate to, and the icon views that we need to animate up
313 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700314 private ImageView mFolderIconImageView;
315 private Bitmap mFolderIconBitmap;
316 private Canvas mFolderIconCanvas;
317 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700318
Michael Jurkaddd62e92011-02-16 17:49:14 -0800319 private BubbleTextView mWaitingForResume;
320
Michael Jurka22143132012-10-04 17:42:38 +0200321 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
322 = new HideFromAccessibilityHelper();
323
Michael Jurkac1f5d262011-09-30 19:32:27 -0700324 private Runnable mBuildLayersRunnable = new Runnable() {
325 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700326 if (mWorkspace != null) {
327 mWorkspace.buildPageHardwareLayers();
328 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700329 }
330 };
331
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800332 private static ArrayList<PendingAddArguments> sPendingAddList
333 = new ArrayList<PendingAddArguments>();
334
Michael Jurka0a457bf2012-11-19 14:05:05 -0800335 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
336
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800337 private static class PendingAddArguments {
338 int requestCode;
339 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700340 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700341 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800342 int cellX;
343 int cellY;
344 }
345
Daniel Sandlerff02d492013-08-05 02:12:05 -0400346 private Stats mStats;
347
Michael Jurka0a457bf2012-11-19 14:05:05 -0800348 private static boolean isPropertyEnabled(String propertyName) {
349 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700350 }
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 @Override
353 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700354 if (DEBUG_STRICT_MODE) {
355 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
356 .detectDiskReads()
357 .detectDiskWrites()
358 .detectNetwork() // or .detectAll() for all detectable problems
359 .penaltyLog()
360 .build());
361 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
362 .detectLeakedSqlLiteObjects()
363 .detectLeakedClosableObjects()
364 .penaltyLog()
365 .penaltyDeath()
366 .build());
367 }
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400370
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400371 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400372 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700373
374 // Determine the dynamic grid properties
375 Point smallestSize = new Point();
376 Point largestSize = new Point();
377 Point realSize = new Point();
378 Display display = getWindowManager().getDefaultDisplay();
379 display.getCurrentSizeRange(smallestSize, largestSize);
380 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700381 DisplayMetrics dm = new DisplayMetrics();
382 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700383 // Lazy-initialize the dynamic grid
384 DeviceProfile grid = app.initDynamicGrid(this,
385 Math.min(smallestSize.x, smallestSize.y),
386 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700387 realSize.x, realSize.y,
388 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700389
390 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400391 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700392 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800393 mModel = app.setLauncher(this);
394 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400395 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700397
Daniel Sandlerff02d492013-08-05 02:12:05 -0400398 mStats = new Stats(this);
399
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700400 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700401 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
402 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700403
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700404 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
405 // this also ensures that any synchronous binding below doesn't re-trigger another
406 // LauncherModel load.
407 mPaused = false;
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200410 android.os.Debug.startMethodTracing(
411 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 }
413
414 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 setContentView(R.layout.launcher);
416 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100417 grid.layout(this);
Winson Chung7d7541e2011-09-16 20:14:36 -0700418 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800420 registerContentObservers();
421
Joe Onorato7c312c12009-08-13 21:36:53 -0700422 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700423
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 mSavedState = savedInstanceState;
425 restoreState(mSavedState);
426
Winson Chunga12a2502010-12-20 14:41:35 -0800427 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700428 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200429 mAppsCustomizeContent.onPackagesUpdated(
430 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700431 }
Winson Chunga12a2502010-12-20 14:41:35 -0800432
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 if (PROFILE_STARTUP) {
434 android.os.Debug.stopMethodTracing();
435 }
436
437 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700438 if (sPausedFromUserAction) {
439 // If the user leaves launcher, then we should just load items asynchronously when
440 // they return.
441 mModel.startLoader(true, -1);
442 } else {
443 // We only load the page synchronously if the user rotates (or triggers a
444 // configuration change) while launcher is in the foreground
445 mModel.startLoader(true, mWorkspace.getCurrentPage());
446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 }
448
449 // For handling default keys
450 mDefaultKeySsb = new SpannableStringBuilder();
451 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800452
453 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
454 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800455
Adam Cohenf9426d52012-06-04 17:26:21 -0700456 updateGlobalIcons();
457
458 // On large interfaces, we want the screen to auto-rotate based on the current orientation
459 unlockScreenOrientation(true);
460 }
461
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 protected void onUserLeaveHint() {
463 super.onUserLeaveHint();
464 sPausedFromUserAction = true;
465 }
466
Winson Chung98ca0f72013-07-29 12:58:51 -0700467 /** To be overriden by subclasses to hint to Launcher that we have custom content */
468 protected boolean hasCustomContentToLeft() {
469 return false;
470 }
471
Adam Cohenf9426d52012-06-04 17:26:21 -0700472 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700473 boolean searchVisible = false;
474 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800475 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700476 int coi = getCurrentOrientationIndexForGlobalIcons();
477 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
478 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700479 updateAppMarketIcon();
480 searchVisible = updateGlobalSearchIcon();
481 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700482 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700483 if (sGlobalSearchIcon[coi] != null) {
484 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700485 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700486 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700487 if (sVoiceSearchIcon[coi] != null) {
488 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700489 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700490 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700491 if (sAppMarketIcon[coi] != null) {
492 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800493 }
Winson Chungadf0c182012-08-23 14:59:07 -0700494 if (mSearchDropTargetBar != null) {
495 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 }
Romain Guycbb89e42009-06-08 15:52:54 -0700498
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700500 if (sLocaleConfiguration == null) {
501 new AsyncTask<Void, Void, LocaleConfiguration>() {
502 @Override
503 protected LocaleConfiguration doInBackground(Void... unused) {
504 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
505 readConfiguration(Launcher.this, localeConfiguration);
506 return localeConfiguration;
507 }
508
509 @Override
510 protected void onPostExecute(LocaleConfiguration result) {
511 sLocaleConfiguration = result;
512 checkForLocaleChange(); // recursive, but now with a locale configuration
513 }
514 }.execute();
515 return;
516 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700517
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 final Configuration configuration = getResources().getConfiguration();
519
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700520 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521 final String locale = configuration.locale.toString();
522
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700523 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 final int mcc = configuration.mcc;
525
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700526 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 final int mnc = configuration.mnc;
528
Romain Guy84f296c2009-11-04 15:00:44 -0800529 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530
Romain Guy84f296c2009-11-04 15:00:44 -0800531 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700532 sLocaleConfiguration.locale = locale;
533 sLocaleConfiguration.mcc = mcc;
534 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700535
Joe Onorato0589f0f2010-02-08 13:44:00 -0800536 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700537
538 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
539 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700540 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700541 public void run() {
542 writeConfiguration(Launcher.this, localeConfiguration);
543 }
544 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700545 }
546 }
547
548 private static class LocaleConfiguration {
549 public String locale;
550 public int mcc = -1;
551 public int mnc = -1;
552 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700553
Romain Guy98d01652009-06-30 16:21:04 -0700554 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
555 DataInputStream in = null;
556 try {
557 in = new DataInputStream(context.openFileInput(PREFERENCES));
558 configuration.locale = in.readUTF();
559 configuration.mcc = in.readInt();
560 configuration.mnc = in.readInt();
561 } catch (FileNotFoundException e) {
562 // Ignore
563 } catch (IOException e) {
564 // Ignore
565 } finally {
566 if (in != null) {
567 try {
568 in.close();
569 } catch (IOException e) {
570 // Ignore
571 }
572 }
573 }
574 }
575
576 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
577 DataOutputStream out = null;
578 try {
579 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
580 out.writeUTF(configuration.locale);
581 out.writeInt(configuration.mcc);
582 out.writeInt(configuration.mnc);
583 out.flush();
584 } catch (FileNotFoundException e) {
585 // Ignore
586 } catch (IOException e) {
587 //noinspection ResultOfMethodCallIgnored
588 context.getFileStreamPath(PREFERENCES).delete();
589 } finally {
590 if (out != null) {
591 try {
592 out.close();
593 } catch (IOException e) {
594 // Ignore
595 }
596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 }
598 }
599
Bjorn Bringertc459e522013-06-07 19:36:01 +0100600 public LayoutInflater getInflater() {
601 return mInflater;
602 }
603
Adam Cohen716b51e2011-06-30 12:09:54 -0700604 public DragLayer getDragLayer() {
605 return mDragLayer;
606 }
607
Winson Chung36a62fe2012-05-06 18:04:42 -0700608 boolean isDraggingEnabled() {
609 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
610 // that is subsequently removed from the workspace in startBinding().
611 return !mModel.isLoadingWorkspace();
612 }
613
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 static int getScreen() {
615 synchronized (sLock) {
616 return sScreen;
617 }
618 }
619
620 static void setScreen(int screen) {
621 synchronized (sLock) {
622 sScreen = screen;
623 }
624 }
625
Winson Chung557d6ed2011-07-08 15:34:52 -0700626 /**
627 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
628 * a configuration step, this allows the proper animations to run after other transitions.
629 */
630 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700631 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800632 switch (args.requestCode) {
633 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700634 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700635 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800636 break;
637 case REQUEST_PICK_SHORTCUT:
638 processShortcut(args.intent);
639 break;
640 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700641 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700642 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700643 result = true;
644 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800645 case REQUEST_CREATE_APPWIDGET:
646 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700648 result = true;
649 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800650 case REQUEST_PICK_WALLPAPER:
651 // We just wanted the activity result here so we can clear mWaitingForResult
652 break;
653 }
Michael Jurka27614d22012-04-02 06:40:22 -0700654 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
655 // if you turned the screen off and then back while in All Apps, Launcher would not
656 // return to the workspace. Clearing mAddInfo.container here fixes this issue
657 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700658 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 }
660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700662 protected void onActivityResult(
663 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700664 // Reset the startActivity waiting flag
665 mWaitingForResult = false;
666
Michael Jurka8b805b12012-04-18 14:23:14 -0700667 if (requestCode == REQUEST_BIND_APPWIDGET) {
668 int appWidgetId = data != null ?
669 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
670 if (resultCode == RESULT_CANCELED) {
671 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
672 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700673 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700674 }
675 return;
676 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700677 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800678 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
679 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700680
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 // We have special handling for widgets
682 if (isWidgetDrop) {
683 int appWidgetId = data != null ?
684 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700685 if (appWidgetId < 0) {
686 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
687 "widget configuration activity.");
688 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
689 } else {
690 completeTwoStageWidgetDrop(resultCode, appWidgetId);
691 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 return;
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 // The pattern used here is that a user PICKs a specific application,
696 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
699 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700700 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800701 final PendingAddArguments args = new PendingAddArguments();
702 args.requestCode = requestCode;
703 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700704 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700706 args.cellX = mPendingAddInfo.cellX;
707 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800708 if (isWorkspaceLocked()) {
709 sPendingAddList.add(args);
710 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700711 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800714 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700715 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800716 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
717 null);
718 }
719
720 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800723 Runnable onCompleteRunnable = null;
724 int animationType = 0;
725
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700726 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800727 if (resultCode == RESULT_OK) {
728 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
729 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700730 mPendingAddWidgetInfo);
731 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 onCompleteRunnable = new Runnable() {
733 @Override
734 public void run() {
735 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700736 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
738 null);
739 }
740 };
741 } else if (resultCode == RESULT_CANCELED) {
742 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
743 onCompleteRunnable = new Runnable() {
744 @Override
745 public void run() {
746 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
747 null);
748 }
749 };
750 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700751 if (mDragLayer.getAnimatedView() != null) {
752 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
753 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
754 animationType, boundWidget, true);
755 } else {
756 // The animated view may be null in the case of a rotation during widget configuration
757 onCompleteRunnable.run();
758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
760
761 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700762 protected void onStop() {
763 super.onStop();
764 FirstFrameAnimatorHelper.setIsVisible(false);
765 }
766
767 @Override
768 protected void onStart() {
769 super.onStart();
770 FirstFrameAnimatorHelper.setIsVisible(true);
771 }
772
773 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200775 long startTime = 0;
776 if (DEBUG_RESUME_TIME) {
777 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400778 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700781
Winson Chung4a2afa32012-07-19 14:53:05 -0700782 // Restore the previous launcher state
783 if (mOnResumeState == State.WORKSPACE) {
784 showWorkspace(false);
785 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
786 showAllApps(false);
787 }
788 mOnResumeState = State.NONE;
789
Craig Mautner360310b2012-10-26 15:13:08 -0700790 // Background was set to gradient in onPause(), restore to black if in all apps.
791 setWorkspaceBackground(mState == State.WORKSPACE);
792
Winson Chungde0fb8f2012-05-08 14:37:08 -0700793 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700794 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700795
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800796 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700797 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700798 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700800 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700802 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700804 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200805 // We might have postponed some bind calls until onResume (see waitUntilResume) --
806 // execute them here
807 long startTimeCallbacks = 0;
808 if (DEBUG_RESUME_TIME) {
809 startTimeCallbacks = System.currentTimeMillis();
810 }
811
812 if (mAppsCustomizeContent != null) {
813 mAppsCustomizeContent.setBulkBind(true);
814 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700815 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
816 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200817 }
818 if (mAppsCustomizeContent != null) {
819 mAppsCustomizeContent.setBulkBind(false);
820 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700821 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200822 if (DEBUG_RESUME_TIME) {
823 Log.d(TAG, "Time spent processing callbacks in onResume: " +
824 (System.currentTimeMillis() - startTimeCallbacks));
825 }
Michael Jurka447bf842013-05-15 14:52:15 +0200826 }
Michael Jurka54554252013-08-01 12:52:23 +0200827 if (mOnResumeCallbacks.size() > 0) {
828 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
829 mOnResumeCallbacks.get(i).run();
830 }
831 mOnResumeCallbacks.clear();
832 }
Winson Chunge4e50662012-01-23 14:45:13 -0800833
834 // Reset the pressed state of icons that were locked in the press state while activities
835 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800836 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800837 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800838 mWaitingForResume.setStayPressed(false);
839 }
Winson Chunge4e50662012-01-23 14:45:13 -0800840 if (mAppsCustomizeContent != null) {
841 // Resets the previous all apps icon press state
842 mAppsCustomizeContent.resetDrawableState();
843 }
Adam Cohen06dff352012-06-01 17:17:08 -0700844 // It is possible that widgets can receive updates while launcher is not in the foreground.
845 // Consequently, the widgets will be inflated in the orientation of the foreground activity
846 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
847 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700848 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700849
850 // Again, as with the above scenario, it's possible that one or more of the global icons
851 // were updated in the wrong orientation.
852 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200853 if (DEBUG_RESUME_TIME) {
854 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
855 }
Adam Cohen06dff352012-06-01 17:17:08 -0700856 }
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700859 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700860 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
861 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
862 // when Launcher resumes and we are still in AllApps.
863 updateWallpaperVisibility(true);
864
Winson Chung997a9232013-07-24 15:33:46 -0700865 // Ensure that items added to Launcher are queued until Launcher returns
866 InstallShortcutReceiver.enableInstallQueue();
867
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700868 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700869 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800870 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700871 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700872 }
Romain Guycbb89e42009-06-08 15:52:54 -0700873
Adam Cohen66a01fd2013-06-11 12:48:00 -0700874 protected void onFinishBindingItems() {
875 }
876
Adam Cohenbffe7452013-07-22 18:21:45 -0700877 QSBScroller mQsbScroller = new QSBScroller() {
878 int scrollY = 0;
879
880 @Override
881 public void setScrollY(int scroll) {
882 scrollY = scroll;
883
884 if (mWorkspace.isOnOrMovingToCustomContent()) {
885 mSearchDropTargetBar.setTranslationY(- scrollY);
886 }
887 }
888 };
889
890 public void resetQSBScroll() {
891 mSearchDropTargetBar.animate().translationY(0).start();
892 }
893
894 public interface CustomContentCallbacks {
895 // Custom content is completely shown
896 public void onShow();
897
898 // Custom content is completely hidden
899 public void onHide();
900 }
901
902 public interface QSBScroller {
903 public void setScrollY(int scrollY);
904 }
905
Adam Cohen66a01fd2013-06-11 12:48:00 -0700906 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700907 public QSBScroller addToCustomContentPage(View customContent) {
908 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700909 }
910
Winson Chung98ca0f72013-07-29 12:58:51 -0700911 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700912 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700913 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700914 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700915 }
916
Adam Cohenedb40762013-07-18 16:45:45 -0700917 // The custom content needs to offset its content to account for the QSB
918 public int getTopOffsetForCustomContent() {
919 return mWorkspace.getPaddingTop();
920 }
921
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700922 @Override
923 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 // Flag the loader to stop early before switching
925 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700926 if (mAppsCustomizeContent != null) {
927 mAppsCustomizeContent.surrender();
928 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800929 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700930 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700931
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800932 // We can't hide the IME if it was forced open. So don't bother
933 /*
934 @Override
935 public void onWindowFocusChanged(boolean hasFocus) {
936 super.onWindowFocusChanged(hasFocus);
937
938 if (hasFocus) {
939 final InputMethodManager inputManager = (InputMethodManager)
940 getSystemService(Context.INPUT_METHOD_SERVICE);
941 WindowManager.LayoutParams lp = getWindow().getAttributes();
942 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
943 android.os.Handler()) {
944 protected void onReceiveResult(int resultCode, Bundle resultData) {
945 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
946 }
947 });
948 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
949 }
950 }
951 */
952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 private boolean acceptFilter() {
954 final InputMethodManager inputManager = (InputMethodManager)
955 getSystemService(Context.INPUT_METHOD_SERVICE);
956 return !inputManager.isFullscreenMode();
957 }
958
959 @Override
960 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700961 final int uniChar = event.getUnicodeChar();
962 final boolean handled = super.onKeyDown(keyCode, event);
963 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
964 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
966 keyCode, event);
967 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700969 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 // showSearchDialog()
971 // If there are multiple keystrokes before the search dialog takes focus,
972 // onSearchRequested() will be called for every keystroke,
973 // but it is idempotent, so it's fine.
974 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976 }
977
Joe Onorato8a9625e2010-01-28 15:55:35 -0800978 // Eat the long press event so the keyboard doesn't come up.
979 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
980 return true;
981 }
982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 return handled;
984 }
985
Karl Rosaen138a0412009-04-23 19:00:21 -0700986 private String getTypedText() {
987 return mDefaultKeySsb.toString();
988 }
989
990 private void clearTypedText() {
991 mDefaultKeySsb.clear();
992 mDefaultKeySsb.clearSpans();
993 Selection.setSelection(mDefaultKeySsb, 0);
994 }
995
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700997 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
998 * State
999 */
1000 private static State intToState(int stateOrdinal) {
1001 State state = State.WORKSPACE;
1002 final State[] stateValues = State.values();
1003 for (int i = 0; i < stateValues.length; i++) {
1004 if (stateValues[i].ordinal() == stateOrdinal) {
1005 state = stateValues[i];
1006 break;
1007 }
1008 }
1009 return state;
1010 }
1011
1012 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 * Restores the previous state, if it exists.
1014 *
1015 * @param savedState The previous state.
1016 */
1017 private void restoreState(Bundle savedState) {
1018 if (savedState == null) {
1019 return;
1020 }
1021
Michael Jurka883f55b2010-10-21 15:47:14 -07001022 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001023 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001024 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001025 }
1026
Winson Chungf0c6ae02012-03-21 16:10:31 -07001027 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001029 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 }
1031
Winson Chung3d503fb2011-07-13 17:25:49 -07001032 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001033 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001034
Winson Chung3d503fb2011-07-13 17:25:49 -07001035 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1036 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001037 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001038 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1039 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001040 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1041 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1042 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001043 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 mRestoring = true;
1045 }
1046
1047 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1048 if (renameFolder) {
1049 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001050 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 mRestoring = true;
1052 }
Winson Chunga12a2502010-12-20 14:41:35 -08001053
Winson Chung785d2eb2011-04-14 16:08:02 -07001054 // Restore the AppsCustomize tab
1055 if (mAppsCustomizeTabHost != null) {
1056 String curTab = savedState.getString("apps_customize_currentTab");
1057 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001058 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001059 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001060 mAppsCustomizeContent.loadAssociatedPages(
1061 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001062 }
1063
Winson Chung5afbf7b2011-07-25 11:53:08 -07001064 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1065 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 }
1068
1069 /**
1070 * Finds all the views we need and configure them properly.
1071 */
1072 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001073 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001074
Craig Mautner360310b2012-10-26 15:13:08 -07001075 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001076 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1077 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001078
1079 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1080 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081
Winson Chung4c98d922011-05-31 16:50:48 -07001082 // Setup the drag layer
1083 mDragLayer.setup(this, dragController);
1084
Winson Chung3d503fb2011-07-13 17:25:49 -07001085 // Setup the hotseat
1086 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1087 if (mHotseat != null) {
1088 mHotseat.setup(this);
1089 }
1090
Adam Cohenf358a4b2013-07-23 16:47:31 -07001091 mOverviewPanel = findViewById(R.id.overview_panel);
1092 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1093 @Override
1094 public void onClick(View arg0) {
1095 showAllApps(true);
1096 }
1097 });
1098 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1099 @Override
1100 public void onClick(View arg0) {
1101 startWallpaper();
1102 }
1103 });
1104
Winson Chung4c98d922011-05-31 16:50:48 -07001105 // Setup the workspace
1106 mWorkspace.setHapticFeedbackEnabled(false);
1107 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001108 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001109 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001110
Winson Chungf0ea4d32011-06-06 14:27:16 -07001111 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001112 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001113
Winson Chungf0ea4d32011-06-06 14:27:16 -07001114 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001115 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001116 mAppsCustomizeContent = (AppsCustomizePagedView)
1117 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1118 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001119
Winson Chung3d503fb2011-07-13 17:25:49 -07001120 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001121 dragController.setDragScoller(mWorkspace);
1122 dragController.setScrollView(mDragLayer);
1123 dragController.setMoveTarget(mWorkspace);
1124 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001125 if (mSearchDropTargetBar != null) {
1126 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001127 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001128
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001129 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001130 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001131 mWeightWatcher = new WeightWatcher(this);
1132 mWeightWatcher.setAlpha(0.5f);
1133 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001134 new FrameLayout.LayoutParams(
1135 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001136 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001137 Gravity.BOTTOM)
1138 );
Adam Cohen39a06042013-07-19 14:30:12 -07001139
1140 boolean show = shouldShowWeightWatcher();
1141 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
1144
1145 /**
1146 * Creates a view representing a shortcut.
1147 *
1148 * @param info The data structure describing the shortcut.
1149 *
1150 * @return A View inflated from R.layout.application.
1151 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001152 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001154 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
1156
1157 /**
1158 * Creates a view representing a shortcut inflated from the specified resource.
1159 *
1160 * @param layoutResId The id of the XML layout used to create the shortcut.
1161 * @param parent The group the shortcut belongs to.
1162 * @param info The data structure describing the shortcut.
1163 *
1164 * @return A View inflated from layoutResId.
1165 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001166 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001167 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1168 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 return favorite;
1171 }
1172
1173 /**
1174 * Add an application shortcut to the workspace.
1175 *
1176 * @param data The intent describing the application.
1177 * @param cellInfo The position on screen where to create the shortcut.
1178 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001179 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001180 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001181 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001182
Adam Cohenfbba09b2011-07-18 21:43:05 -07001183 // First we check if we already know the exact location where we want to add this item.
1184 if (cellX >= 0 && cellY >= 0) {
1185 cellXY[0] = cellX;
1186 cellXY[1] = cellY;
1187 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001188 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 return;
1190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001192 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001193
Romain Guy73b979d2009-06-09 12:57:21 -07001194 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001195 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001197 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001198 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001199 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001200 } else {
1201 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 }
1203 }
Romain Guycbb89e42009-06-08 15:52:54 -07001204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 /**
1206 * Add a shortcut to the workspace.
1207 *
1208 * @param data The intent describing the shortcut.
1209 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001211 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001212 int cellY) {
1213 int[] cellXY = mTmpAddItemCellCoordinates;
1214 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001215 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001216
Michael Jurkad3ef3062010-11-23 16:23:58 -08001217 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001218
Adam Cohen558baaf2011-08-15 15:22:57 -07001219 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001220 if (info == null) {
1221 return;
1222 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001223 final View view = createShortcut(info);
1224
Adam Cohenfbba09b2011-07-18 21:43:05 -07001225 // First we check if we already know the exact location where we want to add this item.
1226 if (cellX >= 0 && cellY >= 0) {
1227 cellXY[0] = cellX;
1228 cellXY[1] = cellY;
1229 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001230
1231 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001232 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001233 true, null,null)) {
1234 return;
1235 }
1236 DragObject dragObject = new DragObject();
1237 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001238 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1239 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001240 return;
1241 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001242 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001243 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001244 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001245 foundCellSpan = (result != null);
1246 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001247 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001248 }
1249
1250 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001251 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001252 return;
1253 }
1254
Adam Cohendcd297f2013-06-18 13:13:40 -07001255 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256
1257 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001258 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001259 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261 }
1262
Adam Cohen2f093b62012-04-30 18:59:53 -07001263 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1264 int minHeight) {
1265 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001266 // We want to account for the extra amount of padding that we are adding to the widget
1267 // to ensure that it gets the full amount of space that it has requested
1268 int requiredWidth = minWidth + padding.left + padding.right;
1269 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001270 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001271 }
1272
Adam Cohen2f093b62012-04-30 18:59:53 -07001273 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1274 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001275 }
1276
Adam Cohen2f093b62012-04-30 18:59:53 -07001277 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1278 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001279 }
1280
Adam Cohen2f093b62012-04-30 18:59:53 -07001281 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1282 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001283 }
1284
Adam Cohen2f093b62012-04-30 18:59:53 -07001285 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1286 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001287 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001288 }
1289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001291 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001293 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001294 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001296 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001297 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1298 if (appWidgetInfo == null) {
1299 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1300 }
Romain Guycbb89e42009-06-08 15:52:54 -07001301
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001302 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001303 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001304
Adam Cohen2f093b62012-04-30 18:59:53 -07001305 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1306 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001309 // We have saved the position to which the widget was dragged-- this really only matters
1310 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 int[] cellXY = mTmpAddItemCellCoordinates;
1312 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001313 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1316 cellXY[0] = mPendingAddInfo.cellX;
1317 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001318 spanXY[0] = mPendingAddInfo.spanX;
1319 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001320 foundCellSpan = true;
1321 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001322 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001323 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001324 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1325 spanXY[1], cellXY, finalSpan);
1326 spanXY[0] = finalSpan[0];
1327 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001328 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001329 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001330 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001331 }
1332
Michael Jurka0280c3b2010-09-17 15:00:07 -07001333 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001334 if (appWidgetId != -1) {
1335 // Deleting an app widget ID is a void call but writes to disk before returning
1336 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001337 new Thread("deleteAppWidgetId") {
1338 public void run() {
1339 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1340 }
1341 }.start();
1342 }
Winson Chung93eef082012-03-23 15:59:27 -07001343 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001344 return;
1345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001347 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001348 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1349 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001350 launcherInfo.spanX = spanXY[0];
1351 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001352 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1353 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001356 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357
1358 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001359 if (hostView == null) {
1360 // Perform actual inflation because we're live
1361 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1362 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1363 } else {
1364 // The AppWidgetHostView has already been inflated and instantiated
1365 launcherInfo.hostView = hostView;
1366 }
Romain Guycbb89e42009-06-08 15:52:54 -07001367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001369 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001370 launcherInfo.notifyWidgetSizeChanged(this);
1371
Adam Cohendcd297f2013-06-18 13:13:40 -07001372 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001373 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001374
1375 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001377 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
Romain Guycbb89e42009-06-08 15:52:54 -07001379
Adam Cohended9f8d2010-11-03 13:25:16 -07001380 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1381 @Override
1382 public void onReceive(Context context, Intent intent) {
1383 final String action = intent.getAction();
1384 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1385 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001386 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001387 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001388
Winson Chungc100e8e2011-08-09 16:02:43 -07001389 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001390 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001391 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1392 mAppsCustomizeTabHost.reset();
1393 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001394 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001395 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1396 mUserPresent = true;
1397 updateRunning();
1398 }
1399 }
1400 };
1401
1402 @Override
1403 public void onAttachedToWindow() {
1404 super.onAttachedToWindow();
1405
1406 // Listen for broadcasts related to user-presence
1407 final IntentFilter filter = new IntentFilter();
1408 filter.addAction(Intent.ACTION_SCREEN_OFF);
1409 filter.addAction(Intent.ACTION_USER_PRESENT);
1410 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001411 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001412 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001413 mVisible = true;
1414 }
1415
1416 @Override
1417 public void onDetachedFromWindow() {
1418 super.onDetachedFromWindow();
1419 mVisible = false;
1420
Adam Cohend113e0c2010-11-11 10:48:05 -08001421 if (mAttached) {
1422 unregisterReceiver(mReceiver);
1423 mAttached = false;
1424 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001425 updateRunning();
1426 }
1427
1428 public void onWindowVisibilityChanged(int visibility) {
1429 mVisible = visibility == View.VISIBLE;
1430 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001431 // The following code used to be in onResume, but it turns out onResume is called when
1432 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1433 // is a more appropriate event to handle
1434 if (mVisible) {
1435 mAppsCustomizeTabHost.onWindowVisible();
1436 if (!mWorkspaceLoading) {
1437 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001438 // We want to let Launcher draw itself at least once before we force it to build
1439 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001440 // apps is nice and speedy.
1441 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001442 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001443 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001444 if (mStarted) return;
1445 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001446 // We delay the layer building a bit in order to give
1447 // other message processing a time to run. In particular
1448 // this avoids a delay in hiding the IME if it was
1449 // currently shown, because doing that may involve
1450 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001451 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001452 final ViewTreeObserver.OnDrawListener listener = this;
1453 mWorkspace.post(new Runnable() {
1454 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001455 if (mWorkspace != null &&
1456 mWorkspace.getViewTreeObserver() != null) {
1457 mWorkspace.getViewTreeObserver().
1458 removeOnDrawListener(listener);
1459 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001460 }
1461 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001462 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001463 }
1464 });
1465 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001466 // When Launcher comes back to foreground, a different Activity might be responsible for
1467 // the app market intent, so refresh the icon
1468 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001469 clearTypedText();
1470 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001471 }
1472
1473 private void sendAdvanceMessage(long delay) {
1474 mHandler.removeMessages(ADVANCE_MSG);
1475 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1476 mHandler.sendMessageDelayed(msg, delay);
1477 mAutoAdvanceSentTime = System.currentTimeMillis();
1478 }
1479
1480 private void updateRunning() {
1481 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1482 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1483 mAutoAdvanceRunning = autoAdvanceRunning;
1484 if (autoAdvanceRunning) {
1485 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1486 sendAdvanceMessage(delay);
1487 } else {
1488 if (!mWidgetsToAdvance.isEmpty()) {
1489 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1490 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1491 }
1492 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001493 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001494 }
1495 }
1496 }
1497
1498 private final Handler mHandler = new Handler() {
1499 @Override
1500 public void handleMessage(Message msg) {
1501 if (msg.what == ADVANCE_MSG) {
1502 int i = 0;
1503 for (View key: mWidgetsToAdvance.keySet()) {
1504 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1505 final int delay = mAdvanceStagger * i;
1506 if (v instanceof Advanceable) {
1507 postDelayed(new Runnable() {
1508 public void run() {
1509 ((Advanceable) v).advance();
1510 }
1511 }, delay);
1512 }
1513 i++;
1514 }
1515 sendAdvanceMessage(mAdvanceInterval);
1516 }
1517 }
1518 };
1519
1520 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001521 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001522 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1523 if (v instanceof Advanceable) {
1524 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001525 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001526 updateRunning();
1527 }
1528 }
1529
1530 void removeWidgetToAutoAdvance(View hostView) {
1531 if (mWidgetsToAdvance.containsKey(hostView)) {
1532 mWidgetsToAdvance.remove(hostView);
1533 updateRunning();
1534 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001535 }
1536
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001538 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001539 launcherInfo.hostView = null;
1540 }
1541
Winson Chung93eef082012-03-23 15:59:27 -07001542 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1543 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1544 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001545 }
1546
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001547 public LauncherAppWidgetHost getAppWidgetHost() {
1548 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
Romain Guycbb89e42009-06-08 15:52:54 -07001550
Winson Chunga9abd0e2010-10-27 17:18:37 -07001551 public LauncherModel getModel() {
1552 return mModel;
1553 }
1554
Bjorn Bringertc459e522013-06-07 19:36:01 +01001555 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001556 getWindow().closeAllPanels();
1557
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001558 // Whatever we were doing is hereby canceled.
1559 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001560 }
1561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 @Override
1563 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001564 long startTime = 0;
1565 if (DEBUG_RESUME_TIME) {
1566 startTime = System.currentTimeMillis();
1567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 super.onNewIntent(intent);
1569
1570 // Close the menu
1571 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001572 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001573 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001574
Jamie Genniscb222e82012-10-23 15:44:26 -07001575 final boolean alreadyOnHome =
1576 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001577 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001578
Jamie Genniscb222e82012-10-23 15:44:26 -07001579 Runnable processIntent = new Runnable() {
1580 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001581 if (mWorkspace == null) {
1582 // Can be cases where mWorkspace is null, this prevents a NPE
1583 return;
1584 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001585 Folder openFolder = mWorkspace.getOpenFolder();
1586 // In all these cases, only animate if we're already on home
1587 mWorkspace.exitWidgetResizeMode();
1588 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1589 openFolder == null) {
1590 mWorkspace.moveToDefaultScreen(true);
1591 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001592
Jamie Genniscb222e82012-10-23 15:44:26 -07001593 closeFolder();
1594 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001595
Jamie Genniscb222e82012-10-23 15:44:26 -07001596 // If we are already on home, then just animate back to the workspace,
1597 // otherwise, just wait until onResume to set the state back to Workspace
1598 if (alreadyOnHome) {
1599 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001600 if (mWorkspace.isInOverviewMode()) {
1601 mWorkspace.exitOverviewMode();
1602 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001603 } else {
1604 mOnResumeState = State.WORKSPACE;
1605 }
1606
1607 final View v = getWindow().peekDecorView();
1608 if (v != null && v.getWindowToken() != null) {
1609 InputMethodManager imm = (InputMethodManager)getSystemService(
1610 INPUT_METHOD_SERVICE);
1611 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1612 }
1613
1614 // Reset AllApps to its initial state
1615 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1616 mAppsCustomizeTabHost.reset();
1617 }
1618 }
1619 };
1620
1621 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1622 // Delay processing of the intent to allow the status bar animation to finish
1623 // first in order to avoid janky animations.
1624 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001625 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001626 // Process the intent immediately.
1627 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001628 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001629
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
Michael Jurka447bf842013-05-15 14:52:15 +02001631 if (DEBUG_RESUME_TIME) {
1632 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 }
1635
1636 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001637 public void onRestoreInstanceState(Bundle state) {
1638 super.onRestoreInstanceState(state);
1639 for (int page: mSynchronouslyBoundPages) {
1640 mWorkspace.restoreInstanceStateForChild(page);
1641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 }
1643
1644 @Override
1645 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001646 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001647 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648
Michael Jurka883f55b2010-10-21 15:47:14 -07001649 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001650 // We close any open folder since it will not be re-opened, and we need to make sure
1651 // this state is reflected.
1652 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653
Adam Cohendcd297f2013-06-18 13:13:40 -07001654 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001655 mWaitingForResult) {
1656 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001657 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001658 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1659 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001660 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1661 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1662 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 }
1664
1665 if (mFolderInfo != null && mWaitingForResult) {
1666 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1667 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1668 }
Michael Jurka946ad472010-07-09 18:05:18 -07001669
Winson Chung785d2eb2011-04-14 16:08:02 -07001670 // Save the current AppsCustomize tab
1671 if (mAppsCustomizeTabHost != null) {
1672 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1673 if (currentTabTag != null) {
1674 outState.putString("apps_customize_currentTab", currentTabTag);
1675 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001676 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1677 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
1680
1681 @Override
1682 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001684
Winson Chunge7a03942011-08-05 15:05:12 -07001685 // Remove all pending runnables
1686 mHandler.removeMessages(ADVANCE_MSG);
1687 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001688 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001689
Winson Chungcd2b0142011-06-08 16:02:26 -07001690 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001691 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001692 mModel.stopLoader();
1693 app.setLauncher(null);
1694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001696 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001698 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001700 mAppWidgetHost = null;
1701
1702 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703
1704 TextKeyListener.getInstance().release();
1705
Winson Chung81b52252012-08-27 15:34:29 -07001706 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1707 // to prevent leaking Launcher activities on orientation change.
1708 if (mModel != null) {
1709 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1710 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001711
1712 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001713 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001714
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001715 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001716 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1717 mWorkspace.removeAllViews();
1718 mWorkspace = null;
1719 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001720
Michael Jurka2ecf9952012-06-18 12:52:28 -07001721 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 }
1723
Adam Cohena9cf38f2011-05-02 15:36:58 -07001724 public DragController getDragController() {
1725 return mDragController;
1726 }
1727
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 @Override
1729 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001730 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 super.startActivityForResult(intent, requestCode);
1732 }
1733
Winson Chung70d72102011-08-12 11:24:35 -07001734 /**
1735 * Indicates that we want global search for this activity by setting the globalSearch
1736 * argument for {@link #startSearch} to true.
1737 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001739 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001741
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001742 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001743
Karl Rosaen138a0412009-04-23 19:00:21 -07001744 if (initialQuery == null) {
1745 // Use any text typed in the launcher as the initial query
1746 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 if (appSearchData == null) {
1749 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001750 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 }
Winson Chungadf0c182012-08-23 14:59:07 -07001752 Rect sourceBounds = new Rect();
1753 if (mSearchDropTargetBar != null) {
1754 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1755 }
Romain Guycbb89e42009-06-08 15:52:54 -07001756
Bjorn Bringertc459e522013-06-07 19:36:01 +01001757 startSearch(initialQuery, selectInitialQuery,
1758 appSearchData, sourceBounds);
1759 }
1760
1761 public void startSearch(String initialQuery,
1762 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001763 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001764 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001765 }
1766
1767 /**
1768 * Starts the global search activity. This code is a copied from SearchManager
1769 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001770 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001771 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001772 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001773 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1774 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1775 if (globalSearchActivity == null) {
1776 Log.w(TAG, "No global search activity found.");
1777 return;
1778 }
1779 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1780 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1781 intent.setComponent(globalSearchActivity);
1782 // Make sure that we have a Bundle to put source in
1783 if (appSearchData == null) {
1784 appSearchData = new Bundle();
1785 } else {
1786 appSearchData = new Bundle(appSearchData);
1787 }
1788 // Set source to package name of app that starts global search, if not set already.
1789 if (!appSearchData.containsKey("source")) {
1790 appSearchData.putString("source", getPackageName());
1791 }
1792 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1793 if (!TextUtils.isEmpty(initialQuery)) {
1794 intent.putExtra(SearchManager.QUERY, initialQuery);
1795 }
1796 if (selectInitialQuery) {
1797 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1798 }
1799 intent.setSourceBounds(sourceBounds);
1800 try {
1801 startActivity(intent);
1802 } catch (ActivityNotFoundException ex) {
1803 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 }
1806
Winson Chung70d72102011-08-12 11:24:35 -07001807 @Override
1808 public boolean onCreateOptionsMenu(Menu menu) {
1809 if (isWorkspaceLocked()) {
1810 return false;
1811 }
1812
1813 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001814
1815 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1816 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1817 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001818 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1819 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1820 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001821 String helpUrl = getString(R.string.help_url);
1822 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001823 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1824 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1825
Winson Chung70d72102011-08-12 11:24:35 -07001826 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1827 .setIcon(android.R.drawable.ic_menu_gallery)
1828 .setAlphabeticShortcut('W');
1829 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1830 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001831 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001832 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001833 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1834 .setIcon(android.R.drawable.ic_menu_preferences)
1835 .setIntent(settings)
1836 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001837 if (!helpUrl.isEmpty()) {
1838 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1839 .setIcon(android.R.drawable.ic_menu_help)
1840 .setIntent(help)
1841 .setAlphabeticShortcut('H');
1842 }
Winson Chung70d72102011-08-12 11:24:35 -07001843 return true;
1844 }
1845
1846 @Override
1847 public boolean onPrepareOptionsMenu(Menu menu) {
1848 super.onPrepareOptionsMenu(menu);
1849
1850 if (mAppsCustomizeTabHost.isTransitioning()) {
1851 return false;
1852 }
1853 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1854 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1855
1856 return true;
1857 }
1858
1859 @Override
1860 public boolean onOptionsItemSelected(MenuItem item) {
1861 switch (item.getItemId()) {
1862 case MENU_WALLPAPER_SETTINGS:
1863 startWallpaper();
1864 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001865 }
1866
1867 return super.onOptionsItemSelected(item);
1868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001870 @Override
1871 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001872 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001873 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001874 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001875 }
1876
Joe Onorato9c1289c2009-08-17 11:03:03 -04001877 public boolean isWorkspaceLocked() {
1878 return mWorkspaceLoading || mWaitingForResult;
1879 }
1880
Michael Jurka0280c3b2010-09-17 15:00:07 -07001881 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001882 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001883 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001884 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1885 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001886 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001887 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001888 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001889
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001890 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1891 AppWidgetProviderInfo appWidgetInfo) {
1892 if (appWidgetInfo.configure != null) {
1893 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001894
Bjorn Bringert7984c942009-12-09 15:38:25 +00001895 // Launch over to configure widget, if needed
1896 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001897 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001898 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001899 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001901 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001902 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001903 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001904 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001905 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
1907 }
Romain Guycbb89e42009-06-08 15:52:54 -07001908
Adam Cohenfbba09b2011-07-18 21:43:05 -07001909 /**
1910 * Process a shortcut drop.
1911 *
1912 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001913 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001914 * @param cell The cell it should be added to, optional
1915 * @param position The location on the screen where it was dropped, optional
1916 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001917 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001918 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001919 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001920 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001921 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001922 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001923
1924 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001925 mPendingAddInfo.cellX = cell[0];
1926 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001927 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001928
1929 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1930 createShortcutIntent.setComponent(componentName);
1931 processShortcut(createShortcutIntent);
1932 }
1933
Adam Cohenfbba09b2011-07-18 21:43:05 -07001934 /**
1935 * Process a widget drop.
1936 *
1937 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001938 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001939 * @param cell The cell it should be added to, optional
1940 * @param position The location on the screen where it was dropped, optional
1941 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001942 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001943 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001944 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001945 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001946 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001947 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001948 mPendingAddInfo.minSpanX = info.minSpanX;
1949 mPendingAddInfo.minSpanY = info.minSpanY;
1950
Adam Cohenfbba09b2011-07-18 21:43:05 -07001951 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001952 mPendingAddInfo.cellX = cell[0];
1953 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001954 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001955 if (span != null) {
1956 mPendingAddInfo.spanX = span[0];
1957 mPendingAddInfo.spanY = span[1];
1958 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001959
Adam Cohened66b2b2012-01-23 17:28:51 -08001960 AppWidgetHostView hostView = info.boundWidget;
1961 int appWidgetId;
1962 if (hostView != null) {
1963 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001964 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001965 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001966 // In this case, we either need to start an activity to get permission to bind
1967 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001968 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001969 Bundle options = info.bindOptions;
1970
1971 boolean success = false;
1972 if (options != null) {
1973 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1974 info.componentName, options);
1975 } else {
1976 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1977 info.componentName);
1978 }
1979 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001980 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001981 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001982 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001983 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1984 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1985 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001986 // TODO: we need to make sure that this accounts for the options bundle.
1987 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001988 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1989 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001990 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001991 }
1992
Joe Onoratodeb98af2010-02-19 14:59:39 -08001993 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001994 // Handle case where user selected "Applications"
1995 String applicationName = getResources().getString(R.string.group_applications);
1996 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001997
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001998 if (applicationName != null && applicationName.equals(shortcutName)) {
1999 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2000 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002001
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002002 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2003 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002004 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002005 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002006 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002007 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
Winson Chung24ab2f12010-09-16 14:10:47 -07002011 void processWallpaper(Intent intent) {
2012 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2013 }
2014
Adam Cohendcd297f2013-06-18 13:13:40 -07002015 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002016 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002017 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 folderInfo.title = getText(R.string.folder_name);
2019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002021 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002022 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002023 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024
2025 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002026 FolderIcon newFolder =
2027 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002028 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002029 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002030 // Force measure the new folder icon
2031 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2032 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002033 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
Romain Guycbb89e42009-06-08 15:52:54 -07002035
Joe Onorato9c1289c2009-08-17 11:03:03 -04002036 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002037 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002038 }
2039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002041 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurka104c4562013-07-08 18:03:46 -07002043 pickWallpaper.setComponent(
2044 new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName()));
2045 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
2047
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002048 /**
2049 * Registers various content observers. The current implementation registers
2050 * only a favorites observer to keep track of the favorites applications.
2051 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002052 private void registerContentObservers() {
2053 ContentResolver resolver = getContentResolver();
2054 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2055 true, mWidgetObserver);
2056 }
2057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 @Override
2059 public boolean dispatchKeyEvent(KeyEvent event) {
2060 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2061 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002063 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002064 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002065 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002066 dumpState();
2067 return true;
2068 }
2069 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002070 }
2071 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2072 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002073 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 return true;
2075 }
2076 }
2077
2078 return super.dispatchKeyEvent(event);
2079 }
2080
Joe Onorato88ec0992009-11-19 13:16:06 -08002081 @Override
2082 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002083 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002084 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002085 } else if (mWorkspace.isInOverviewMode()) {
2086 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002087 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002088 Folder openFolder = mWorkspace.getOpenFolder();
2089 if (openFolder.isEditingName()) {
2090 openFolder.dismissEditingName();
2091 } else {
2092 closeFolder();
2093 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002094 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002095 mWorkspace.exitWidgetResizeMode();
2096
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002097 // Back button is a no-op here, but give at least some feedback for the button press
2098 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002099 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002100 }
2101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002103 * Re-listen when widgets are reset.
2104 */
2105 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002106 if (mAppWidgetHost != null) {
2107 mAppWidgetHost.startListening();
2108 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002109 }
2110
2111 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 * Launches the intent referred by the clicked shortcut.
2113 *
2114 * @param v The view representing the clicked shortcut.
2115 */
2116 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002117 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2118 // view has detached (it's possible for this to happen if the view is removed mid touch).
2119 if (v.getWindowToken() == null) {
2120 return;
2121 }
2122
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002123 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002124 return;
2125 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002126
Adam Cohenf358a4b2013-07-23 16:47:31 -07002127 if (v instanceof PageIndicator) {
2128 if (!mWorkspace.isInOverviewMode()) {
2129 mWorkspace.enterOverviewMode();
2130 }
2131 return;
2132 }
2133
2134 if (v instanceof CellLayout) {
2135 if (mWorkspace.isInOverviewMode()) {
2136 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2137 }
2138 }
2139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002141 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002143 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2144 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002145
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002146 // Check for special shortcuts
2147 if (intent.getComponent() != null) {
2148 final String shortcutClass = intent.getComponent().getClassName();
2149
2150 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2151 showAllApps(true);
2152 return;
2153 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2154 MemoryDumpActivity.startDump(this);
2155 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002156 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2157 toggleShowWeightWatcher();
2158 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002159 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002160 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002161
2162 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002163 int[] pos = new int[2];
2164 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002165 intent.setSourceBounds(new Rect(pos[0], pos[1],
2166 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002167
2168 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002169
Daniel Sandlerff02d492013-08-05 02:12:05 -04002170 mStats.recordLaunch(intent, shortcut);
2171
Michael Jurkaddd62e92011-02-16 17:49:14 -08002172 if (success && v instanceof BubbleTextView) {
2173 mWaitingForResume = (BubbleTextView) v;
2174 mWaitingForResume.setStayPressed(true);
2175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002177 if (v instanceof FolderIcon) {
2178 FolderIcon fi = (FolderIcon) v;
2179 handleFolderClick(fi);
2180 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002181 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002182 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002183 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002184 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002185 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 }
2188 }
2189
Michael Jurka0e260592010-06-30 17:07:39 -07002190 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002191 return false;
2192 }
2193
Michael Jurkaaf442092010-06-10 17:01:57 -07002194 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002195 * Event handler for the search button
2196 *
2197 * @param v The view that was clicked.
2198 */
2199 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002200 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2201
Amith Yamasania135ba82011-08-09 17:42:01 -07002202 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002203 }
2204
2205 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002206 * Event handler for the voice button
2207 *
2208 * @param v The view that was clicked.
2209 */
2210 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002211 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002212
Bjorn Bringertc459e522013-06-07 19:36:01 +01002213 startVoice();
2214 }
2215
2216 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002217 try {
2218 final SearchManager searchManager =
2219 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2220 ComponentName activityName = searchManager.getGlobalSearchActivity();
2221 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002222 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002223 if (activityName != null) {
2224 intent.setPackage(activityName.getPackageName());
2225 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002226 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002227 } catch (ActivityNotFoundException e) {
2228 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002229 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002230 startActivitySafely(null, intent, "onClickVoiceButton");
2231 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002232 }
2233
2234 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002235 * Event handler for the "grid" button that appears on the home screen, which
2236 * enters all apps mode.
2237 *
2238 * @param v The view that was clicked.
2239 */
2240 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002241 showAllApps(true);
2242 }
2243
2244 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002245 // Provide the same haptic feedback that the system offers for virtual keys.
2246 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002247 }
2248
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002249 public void onClickAppMarketButton(View v) {
2250 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002251 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002252 } else {
2253 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002254 }
2255 }
2256
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002257 void startApplicationDetailsActivity(ComponentName componentName) {
2258 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002259 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2260 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002261 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002262 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002263 }
2264
Michael Jurka1e2f4652013-07-08 18:03:46 -07002265 // returns true if the activity was started
2266 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2267 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002268 // System applications cannot be installed. For now, show a toast explaining that.
2269 // We may give them the option of disabling apps this way.
2270 int messageId = R.string.uninstall_system_app_text;
2271 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002272 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002273 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002274 String packageName = componentName.getPackageName();
2275 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002276 Intent intent = new Intent(
2277 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002278 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2279 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002280 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002281 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002282 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002283 }
2284
Michael Jurka86a720e2012-05-09 11:23:23 -07002285 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002289 // Only launch using the new animation if the shortcut has not opted out (this is a
2290 // private contract between launcher and may be ignored in the future).
2291 boolean useLaunchAnimation = (v != null) &&
2292 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2293 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002294 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2295 v.getMeasuredWidth(), v.getMeasuredHeight());
2296
2297 startActivity(intent, opts.toBundle());
2298 } else {
2299 startActivity(intent);
2300 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002301 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 } catch (SecurityException e) {
2303 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002304 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002306 "or use the exported attribute for this activity. "
2307 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002309 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002310 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002311
Michael Jurka86a720e2012-05-09 11:23:23 -07002312 boolean startActivitySafely(View v, Intent intent, Object tag) {
2313 boolean success = false;
2314 try {
2315 success = startActivity(v, intent, tag);
2316 } catch (ActivityNotFoundException e) {
2317 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2318 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2319 }
2320 return success;
2321 }
2322
Adam Cohena9cf38f2011-05-02 15:36:58 -07002323 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002324 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002325 Folder openFolder = mWorkspace.getFolderForTag(info);
2326
2327 // If the folder info reports that the associated folder is open, then verify that
2328 // it is actually opened. There have been a few instances where this gets out of sync.
2329 if (info.opened && openFolder == null) {
2330 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002332 info.opened = false;
2333 }
2334
Adam Cohenfb91f302012-06-11 15:45:18 -07002335 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 // Close any open folder
2337 closeFolder();
2338 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002339 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340 } else {
2341 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 int folderScreen;
2343 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002344 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 // .. and close it
2346 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002347 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 // Close any folder open on the current screen
2349 closeFolder();
2350 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002351 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 }
2353 }
2354 }
2355 }
2356
Adam Cohen268c4752012-06-06 17:47:33 -07002357 /**
2358 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2359 * in the DragLayer in the exact absolute location of the original FolderIcon.
2360 */
2361 private void copyFolderIconToImage(FolderIcon fi) {
2362 final int width = fi.getMeasuredWidth();
2363 final int height = fi.getMeasuredHeight();
2364
2365 // Lazy load ImageView, Bitmap and Canvas
2366 if (mFolderIconImageView == null) {
2367 mFolderIconImageView = new ImageView(this);
2368 }
2369 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2370 mFolderIconBitmap.getHeight() != height) {
2371 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2372 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2373 }
2374
2375 DragLayer.LayoutParams lp;
2376 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2377 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2378 } else {
2379 lp = new DragLayer.LayoutParams(width, height);
2380 }
2381
Adam Cohen307fe232012-08-16 17:55:58 -07002382 // The layout from which the folder is being opened may be scaled, adjust the starting
2383 // view size by this scale factor.
2384 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002385 lp.customPosition = true;
2386 lp.x = mRectForFolderAnimation.left;
2387 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002388 lp.width = (int) (scale * width);
2389 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002390
2391 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2392 fi.draw(mFolderIconCanvas);
2393 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002394 if (fi.getFolder() != null) {
2395 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2396 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002397 }
Adam Cohen268c4752012-06-06 17:47:33 -07002398 // Just in case this image view is still in the drag layer from a previous animation,
2399 // we remove it and re-add it.
2400 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2401 mDragLayer.removeView(mFolderIconImageView);
2402 }
2403 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002404 if (fi.getFolder() != null) {
2405 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002406 }
Adam Cohen268c4752012-06-06 17:47:33 -07002407 }
2408
Adam Cohen2801caf2011-05-13 20:57:39 -07002409 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002410 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002411 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2412 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2413 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2414
Adam Cohenc51934b2011-07-26 21:07:43 -07002415 FolderInfo info = (FolderInfo) fi.getTag();
2416 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2417 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002418 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2419 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002420 }
2421
Adam Cohen268c4752012-06-06 17:47:33 -07002422 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2423 copyFolderIconToImage(fi);
2424 fi.setVisibility(View.INVISIBLE);
2425
Michael Jurka2ecf9952012-06-18 12:52:28 -07002426 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002427 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002428 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2429 oa.start();
2430 }
2431
Adam Cohen268c4752012-06-06 17:47:33 -07002432 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002433 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002434 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2435 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2436 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2437
Adam Cohen268c4752012-06-06 17:47:33 -07002438 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002439
Adam Cohen268c4752012-06-06 17:47:33 -07002440 // We remove and re-draw the FolderIcon in-case it has changed
2441 mDragLayer.removeView(mFolderIconImageView);
2442 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002443 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002444 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002445 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002446 oa.addListener(new AnimatorListenerAdapter() {
2447 @Override
2448 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002449 if (cl != null) {
2450 cl.clearFolderLeaveBehind();
2451 // Remove the ImageView copy of the FolderIcon and make the original visible.
2452 mDragLayer.removeView(mFolderIconImageView);
2453 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002454 }
2455 }
2456 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002457 oa.start();
2458 }
2459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002461 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002462 * is animated relative to the specified View. If the View is null, no animation
2463 * is played.
2464 *
2465 * @param folderInfo The FolderInfo describing the folder to open.
2466 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002467 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002468 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002469 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470
Adam Cohena9cf38f2011-05-02 15:36:58 -07002471 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002472
Adam Cohen4554ee12011-08-03 16:13:21 -07002473 // Just verify that the folder hasn't already been added to the DragLayer.
2474 // There was a one-off crash where the folder had a parent already.
2475 if (folder.getParent() == null) {
2476 mDragLayer.addView(folder);
2477 mDragController.addDropTarget((DropTarget) folder);
2478 } else {
2479 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2480 folder.getParent() + ").");
2481 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002482 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002483 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002484
2485 // Notify the accessibility manager that this folder "window" has appeared and occluded
2486 // the workspace items
2487 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2488 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002489 }
2490
2491 public void closeFolder() {
2492 Folder folder = mWorkspace.getOpenFolder();
2493 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002494 if (folder.isEditingName()) {
2495 folder.dismissEditingName();
2496 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002497 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002498
2499 // Dismiss the folder cling
2500 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002501 }
2502 }
2503
2504 void closeFolder(Folder folder) {
2505 folder.getInfo().opened = false;
2506
2507 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2508 if (parent != null) {
2509 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2510 shrinkAndFadeInFolderIcon(fi);
2511 }
2512 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002513
2514 // Notify the accessibility manager that this folder "window" has disappeard and no
2515 // longer occludeds the workspace items
2516 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 }
2518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002520 if (!isDraggingEnabled()) return false;
2521 if (isWorkspaceLocked()) return false;
2522 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523
2524 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002525 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 }
2527
Michael Jurka0280c3b2010-09-17 15:00:07 -07002528 resetAddInfo();
2529 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002531 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002532 return true;
2533 }
2534
Winson Chung3d503fb2011-07-13 17:25:49 -07002535 // The hotseat touch handling does not go through Workspace, and we always allow long press
2536 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002537 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002538 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2539 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002540 if (itemUnderLongClick == null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07002541 if (mWorkspace.hasNonCustomEmptyScreens()) {
2542 // User long pressed on empty space
2543 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2544 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2545 // Disabling reordering until we sort out some issues.
2546 if (mWorkspace.isInOverviewMode()) {
2547 mWorkspace.startReordering(v);
2548 } else {
2549 mWorkspace.enterOverviewMode();
2550 }
Adam Cohendedbd962013-07-11 14:21:49 -07002551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002553 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002555 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 }
2557 }
2558 }
2559 return true;
2560 }
2561
Winson Chung3d503fb2011-07-13 17:25:49 -07002562 boolean isHotseatLayout(View layout) {
2563 return mHotseat != null && layout != null &&
2564 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2565 }
2566 Hotseat getHotseat() {
2567 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002568 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002569 View getOverviewPanel() {
2570 return mOverviewPanel;
2571 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002572 SearchDropTargetBar getSearchBar() {
2573 return mSearchDropTargetBar;
2574 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002575
Winson Chung3d503fb2011-07-13 17:25:49 -07002576 /**
2577 * Returns the CellLayout of the specified container at the specified screen.
2578 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002579 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002580 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2581 if (mHotseat != null) {
2582 return mHotseat.getLayout();
2583 } else {
2584 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002585 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002586 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002587 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002588 }
2589 }
2590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 Workspace getWorkspace() {
2592 return mWorkspace;
2593 }
2594
Daniel Sandler843e8602010-06-07 14:59:01 -04002595 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002596 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002597 }
2598
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002599 /**
2600 * Helper method for the cameraZoomIn/cameraZoomOut animations
2601 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 * @param scaleFactor The scale factor used for the zoom
2603 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002604 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002606 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002608
Winson Chung18f41f82012-05-09 13:28:10 -07002609 void disableWallpaperIfInAllApps() {
2610 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002611 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002612 if (mAppsCustomizeTabHost != null &&
2613 !mAppsCustomizeTabHost.isTransitioning()) {
2614 updateWallpaperVisibility(false);
2615 }
2616 }
2617 }
2618
Craig Mautner360310b2012-10-26 15:13:08 -07002619 private void setWorkspaceBackground(boolean workspace) {
2620 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002621 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002622 }
2623
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002624 void updateWallpaperVisibility(boolean visible) {
2625 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2626 int curflags = getWindow().getAttributes().flags
2627 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2628 if (wpflags != curflags) {
2629 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2630 }
Craig Mautner360310b2012-10-26 15:13:08 -07002631 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002632 }
2633
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002634 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2635 if (v instanceof LauncherTransitionable) {
2636 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2637 }
2638 }
2639
Michael Jurkabed61d22012-02-14 22:51:29 -08002640 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2641 if (v instanceof LauncherTransitionable) {
2642 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2643 }
Winson Chung70442722012-02-10 15:43:22 -08002644
2645 // Update the workspace transition step as well
2646 dispatchOnLauncherTransitionStep(v, 0f);
2647 }
2648
2649 private void dispatchOnLauncherTransitionStep(View v, float t) {
2650 if (v instanceof LauncherTransitionable) {
2651 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2652 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002653 }
2654
2655 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2656 if (v instanceof LauncherTransitionable) {
2657 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2658 }
Winson Chung70442722012-02-10 15:43:22 -08002659
2660 // Update the workspace transition step as well
2661 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002662 }
2663
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002664 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002665 * Things to test when changing the following seven functions.
2666 * - Home from workspace
2667 * - from center screen
2668 * - from other screens
2669 * - Home from all apps
2670 * - from center screen
2671 * - from other screens
2672 * - Back from all apps
2673 * - from center screen
2674 * - from other screens
2675 * - Launch app from workspace and quit
2676 * - with back
2677 * - with home
2678 * - Launch app from all apps and quit
2679 * - with back
2680 * - with home
2681 * - Go to a screen that's not the default, then all
2682 * apps, and launch and app, and go back
2683 * - with back
2684 * -with home
2685 * - On workspace, long press power and go back
2686 * - with back
2687 * - with home
2688 * - On all apps, long press power and go back
2689 * - with back
2690 * - with home
2691 * - On workspace, power off
2692 * - On all apps, power off
2693 * - Launch an app and turn off the screen while in that app
2694 * - Go back with home key
2695 * - Go back with back key TODO: make this not go to workspace
2696 * - From all apps
2697 * - From workspace
2698 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2699 * - From all apps
2700 * - From the center workspace
2701 * - From another workspace
2702 */
2703
2704 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002705 * Zoom the camera out from the workspace to reveal 'toView'.
2706 * Assumes that the view to show is anchored at either the very top or very bottom
2707 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002708 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002709 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002710 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002711 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002712 mStateAnimation.cancel();
2713 mStateAnimation = null;
2714 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002715 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002716
Winson Chungf0ea4d32011-06-06 14:27:16 -07002717 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2718 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2719 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002720 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002721 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002722 final int startDelay =
2723 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002724
Michael Jurkab3e22d92011-10-31 15:58:33 -07002725 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002726
Michael Jurkad74c9842011-07-10 12:44:21 -07002727 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002728 Animator workspaceAnim =
2729 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002730
2731 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002732 toView.setScaleX(scale);
2733 toView.setScaleY(scale);
2734 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2735 scaleAnim.
2736 scaleX(1f).scaleY(1f).
2737 setDuration(duration).
2738 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002739
Winson Chungf0ea4d32011-06-06 14:27:16 -07002740 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002741 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002742 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002743 .ofFloat(toView, "alpha", 0f, 1f)
2744 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002745 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002746 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2747 @Override
2748 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002749 if (animation == null) {
2750 throw new RuntimeException("animation is null");
2751 }
Winson Chung70442722012-02-10 15:43:22 -08002752 float t = (Float) animation.getAnimatedValue();
2753 dispatchOnLauncherTransitionStep(fromView, t);
2754 dispatchOnLauncherTransitionStep(toView, t);
2755 }
2756 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002757
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002758 // toView should appear right at the end of the workspace shrink
2759 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002760 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002761 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002762 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002763
2764 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002765 boolean animationCancelled = false;
2766
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002767 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002768 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002769 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002770 // Prepare the position
2771 toView.setTranslationX(0.0f);
2772 toView.setTranslationY(0.0f);
2773 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002774 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002775 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002776 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002777 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002778 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2779 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002780
Adam Cohenf4ddea32011-09-12 13:46:42 -07002781 if (!animationCancelled) {
2782 updateWallpaperVisibility(false);
2783 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002784
2785 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002786 if (mSearchDropTargetBar != null) {
2787 mSearchDropTargetBar.hideSearchBar(false);
2788 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002789 }
2790
Adam Cohencff6af82011-09-13 14:51:53 -07002791 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002792 public void onAnimationCancel(Animator animation) {
2793 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002794 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002795 });
2796
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002797 if (workspaceAnim != null) {
2798 mStateAnimation.play(workspaceAnim);
2799 }
Michael Jurka1899a362011-11-03 13:50:45 -07002800
2801 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002802
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002803 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2804 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002805
2806 // If any of the objects being animated haven't been measured/laid out
2807 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002808 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002809 (mWorkspace.getMeasuredWidth() == 0) ||
2810 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002811 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002812 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002813
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002814 final AnimatorSet stateAnimation = mStateAnimation;
2815 final Runnable startAnimRunnable = new Runnable() {
2816 public void run() {
2817 // Check that mStateAnimation hasn't changed while
2818 // we waited for a layout/draw pass
2819 if (mStateAnimation != stateAnimation)
2820 return;
2821 setPivotsForZoom(toView, scale);
2822 dispatchOnLauncherTransitionStart(fromView, animated, false);
2823 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002824 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002825 }
2826 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002827 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002828 final ViewTreeObserver observer = toView.getViewTreeObserver();
2829 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2830 public void onGlobalLayout() {
2831 startAnimRunnable.run();
2832 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2833 }
2834 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002835 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002836 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002837 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002838 } else {
2839 toView.setTranslationX(0.0f);
2840 toView.setTranslationY(0.0f);
2841 toView.setScaleX(1.0f);
2842 toView.setScaleY(1.0f);
2843 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002844 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002845
Daniel Sandlere4f98912013-06-25 15:13:26 -04002846 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002847 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002848 if (mSearchDropTargetBar != null) {
2849 mSearchDropTargetBar.hideSearchBar(false);
2850 }
Michael Jurkaabded662011-03-04 12:06:57 -08002851 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002852 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002853 dispatchOnLauncherTransitionStart(fromView, animated, false);
2854 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002855 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002856 dispatchOnLauncherTransitionStart(toView, animated, false);
2857 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002858 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002859 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002860 }
2861
2862 /**
2863 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002864 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002865 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002866 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002867 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2868 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002869
Michael Jurkab3e22d92011-10-31 15:58:33 -07002870 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002871 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002872 mStateAnimation.cancel();
2873 mStateAnimation = null;
2874 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002875 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002876
Winson Chungf0ea4d32011-06-06 14:27:16 -07002877 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002878 final int fadeOutDuration =
2879 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002880 final float scaleFactor = (float)
2881 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2882 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002883 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002884 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002885 if (toState == State.WORKSPACE) {
2886 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2887 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002888 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002889 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2890 workspaceAnim = mWorkspace.getChangeStateAnimation(
2891 Workspace.State.SPRING_LOADED, animated);
2892 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002893
Michael Jurkab3e22d92011-10-31 15:58:33 -07002894 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002895 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002896 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002897 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002898 final LauncherViewPropertyAnimator scaleAnim =
2899 new LauncherViewPropertyAnimator(fromView);
2900 scaleAnim.
2901 scaleX(scaleFactor).scaleY(scaleFactor).
2902 setDuration(duration).
2903 setInterpolator(new Workspace.ZoomInInterpolator());
2904
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002905 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002906 .ofFloat(fromView, "alpha", 1f, 0f)
2907 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002908 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002909 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2910 @Override
2911 public void onAnimationUpdate(ValueAnimator animation) {
2912 float t = 1f - (Float) animation.getAnimatedValue();
2913 dispatchOnLauncherTransitionStep(fromView, t);
2914 dispatchOnLauncherTransitionStep(toView, t);
2915 }
2916 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002917
Michael Jurka2ecf9952012-06-18 12:52:28 -07002918 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002919
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002920 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2921 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002922 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002923
2924 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002925 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002926 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002927 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002928 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002929 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2930 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002931 if (onCompleteRunnable != null) {
2932 onCompleteRunnable.run();
2933 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002934 mAppsCustomizeContent.updateCurrentPageScroll();
2935 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002936 }
2937 });
2938
Winson Chungf0ea4d32011-06-06 14:27:16 -07002939 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002940 if (workspaceAnim != null) {
2941 mStateAnimation.play(workspaceAnim);
2942 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002943 dispatchOnLauncherTransitionStart(fromView, animated, true);
2944 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002945 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002946 } else {
2947 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002948 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002949 dispatchOnLauncherTransitionStart(fromView, animated, true);
2950 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002951 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002952 dispatchOnLauncherTransitionStart(toView, animated, true);
2953 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002954 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002955 }
2956
Michael Jurkae326f182011-11-21 14:05:46 -08002957 @Override
2958 public void onTrimMemory(int level) {
2959 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002960 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002961 mAppsCustomizeTabHost.onTrimMemory();
2962 }
2963 }
2964
Winson Chung18f41f82012-05-09 13:28:10 -07002965 @Override
2966 public void onWindowFocusChanged(boolean hasFocus) {
2967 if (!hasFocus) {
2968 // When another window occludes launcher (like the notification shade, or recents),
2969 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2970 updateWallpaperVisibility(true);
2971 } else {
2972 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002973 mWorkspace.postDelayed(new Runnable() {
2974 @Override
2975 public void run() {
2976 disableWallpaperIfInAllApps();
2977 }
2978 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002979 }
2980 }
2981
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002982 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002983 showWorkspace(animated, null);
2984 }
2985
2986 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002987 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002988 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002989 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002990 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002991
Winson Chungc7d2b602012-05-16 17:02:20 -07002992 // Show the search bar (only animate if we were showing the drop target bar in spring
2993 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002994 if (mSearchDropTargetBar != null) {
2995 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2996 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002997
Michael Jurkab3e22d92011-10-31 15:58:33 -07002998 // Set focus to the AppsCustomize button
2999 if (mAllAppsButton != null) {
3000 mAllAppsButton.requestFocus();
3001 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003002 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003003
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003004 // Change the state *after* we've called all the transition code
3005 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003006
Winson Chung5fb63472011-02-02 17:03:37 -08003007 // Resume the auto-advance of widgets
3008 mUserPresent = true;
3009 updateRunning();
3010
alanv1d4fde62012-10-17 13:15:47 -07003011 // Send an accessibility event to announce the context change
3012 getWindow().getDecorView()
3013 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003014
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003015 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003016 }
3017
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003018 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003019 }
3020
Michael Jurkab3e22d92011-10-31 15:58:33 -07003021 void showAllApps(boolean animated) {
3022 if (mState != State.WORKSPACE) return;
3023
3024 showAppsCustomizeHelper(animated, false);
3025 mAppsCustomizeTabHost.requestFocus();
3026
Michael Jurkab3e22d92011-10-31 15:58:33 -07003027 // Change the state *after* we've called all the transition code
3028 mState = State.APPS_CUSTOMIZE;
3029
3030 // Pause the auto-advance of widgets until we are out of AllApps
3031 mUserPresent = false;
3032 updateRunning();
3033 closeFolder();
3034
3035 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003036 getWindow().getDecorView()
3037 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003038 }
3039
Adam Cohen7777d962011-08-18 18:58:38 -07003040 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003041 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003042 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003043 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003044 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003045 }
Adam Cohen7777d962011-08-18 18:58:38 -07003046
Adam Cohened66b2b2012-01-23 17:28:51 -08003047 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3048 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003049 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3050
Winson Chunge7a03942011-08-05 15:05:12 -07003051 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003052 @Override
3053 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003054 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003055 // Before we show workspace, hide all apps again because
3056 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3057 // clean up our state transition functions
3058 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003059 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003060 } else {
3061 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003062 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003063 }
3064 }, (extendedDelay ?
3065 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3066 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3067 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003068
Michael Jurkad3ef3062010-11-23 16:23:58 -08003069 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003070 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003071 final boolean animated = true;
3072 final boolean springLoaded = true;
3073 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003074 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003075 }
3076 // Otherwise, we are not in spring loaded mode, so don't do anything.
3077 }
3078
Michael Jurkab3e22d92011-10-31 15:58:33 -07003079 void lockAllApps() {
3080 // TODO
3081 }
3082
3083 void unlockAllApps() {
3084 // TODO
3085 }
3086
Winson Chungf0ea4d32011-06-06 14:27:16 -07003087 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003088 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003089 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003090 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003091 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003092 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003093 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003094 int duration = 0;
3095 if (mSearchDropTargetBar != null) {
3096 duration = mSearchDropTargetBar.getTransitionInDuration();
3097 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003098 mHotseat.animate().alpha(1f).setDuration(duration);
3099 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003100 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003101 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003102 }
3103 }
3104 }
3105
3106 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003107 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003108 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003109 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003110 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003111 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003112 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003113 int duration = 0;
3114 if (mSearchDropTargetBar != null) {
3115 duration = mSearchDropTargetBar.getTransitionOutDuration();
3116 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003117 mHotseat.animate().alpha(0f).setDuration(duration);
3118 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003119 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003120 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003121 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003122 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003123 }
3124
Patrick Dubroy5f445422011-02-18 14:35:21 -08003125 /**
3126 * Add an item from all apps or customize onto the given workspace screen.
3127 * If layout is null, add to the current screen.
3128 */
3129 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003130 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003131 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003132 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003133 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003134
Winson Chungdff8ebb2011-09-08 17:25:31 -07003135 /** Maps the current orientation to an index for referencing orientation correct global icons */
3136 private int getCurrentOrientationIndexForGlobalIcons() {
3137 // default - 0, landscape - 1
3138 switch (getResources().getConfiguration().orientation) {
3139 case Configuration.ORIENTATION_LANDSCAPE:
3140 return 1;
3141 default:
3142 return 0;
3143 }
3144 }
3145
Michael Jurkaae65ee32012-04-30 11:45:54 -07003146 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003147 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003148 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003149 // Look for the toolbar icon specified in the activity meta-data
3150 Bundle metaData = packageManager.getActivityInfo(
3151 activityName, PackageManager.GET_META_DATA).metaData;
3152 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003153 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003154 if (iconResId != 0) {
3155 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003156 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003157 }
3158 }
3159 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003160 // This can happen if the activity defines an invalid drawable
3161 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3162 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003163 } catch (Resources.NotFoundException nfe) {
3164 // This can happen if the activity defines an invalid drawable
3165 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3166 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003167 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003168 return null;
3169 }
3170
3171 // if successful in getting icon, return it; otherwise, set button to use default drawable
3172 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003173 int buttonId, ComponentName activityName, int fallbackDrawableId,
3174 String toolbarResourceName) {
3175 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003176 Resources r = getResources();
3177 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3178 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003179
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003180 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003181 // If we were unable to find the icon via the meta-data, use a generic one
3182 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003183 toolbarIcon = r.getDrawable(fallbackDrawableId);
3184 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003185 if (button != null) {
3186 button.setCompoundDrawables(toolbarIcon, null, null, null);
3187 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003188 return null;
3189 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003190 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003191 if (button != null) {
3192 button.setCompoundDrawables(toolbarIcon, null, null, null);
3193 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003194 return toolbarIcon.getConstantState();
3195 }
3196 }
3197
3198 // if successful in getting icon, return it; otherwise, set button to use default drawable
3199 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003200 int buttonId, ComponentName activityName, int fallbackDrawableId,
3201 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003202 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003203 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003204
Michael Jurka19e0fc52011-07-22 18:00:21 -07003205 if (button != null) {
3206 // If we were unable to find the icon via the meta-data, use a
3207 // generic one
3208 if (toolbarIcon == null) {
3209 button.setImageResource(fallbackDrawableId);
3210 } else {
3211 button.setImageDrawable(toolbarIcon);
3212 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003213 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003214
3215 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3216
Michael Jurka0423dcf2010-10-05 14:56:18 -07003217 }
3218
Winson Chung1b884092012-06-08 17:13:02 -07003219 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003220 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003221 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003222 }
3223
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003224 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003225 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003226 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003227 }
3228
Winson Chungbb185bd2011-11-21 12:31:42 -08003229 private void invalidatePressedFocusedStates(View container, View button) {
3230 if (container instanceof HolographicLinearLayout) {
3231 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3232 layout.invalidatePressedFocusedStates();
3233 } else if (button instanceof HolographicImageView) {
3234 HolographicImageView view = (HolographicImageView) button;
3235 view.invalidatePressedFocusedStates();
3236 }
3237 }
3238
Cristina Stancu476493b2013-08-07 17:20:14 +01003239 public View getQsbBar() {
3240 if (mQsbBar == null) {
3241 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3242 }
3243 return mQsbBar;
3244 }
3245
3246 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003247 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003248 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003249 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003250 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003251
Winson Chung1cad91e2011-05-25 17:41:01 -07003252 final SearchManager searchManager =
3253 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3254 ComponentName activityName = searchManager.getGlobalSearchActivity();
3255 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003256 int coi = getCurrentOrientationIndexForGlobalIcons();
3257 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003258 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3259 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3260 if (sGlobalSearchIcon[coi] == null) {
3261 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3262 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3263 TOOLBAR_ICON_METADATA_NAME);
3264 }
3265
Winson Chungc51db6a2011-10-05 11:44:49 -07003266 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3267 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003268 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003269 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003270 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003271 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003272 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3273 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3274 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003275 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003276 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003277 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003278 }
3279 }
3280
Cristina Stancu476493b2013-08-07 17:20:14 +01003281 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003282 final View searchButtonContainer = findViewById(R.id.search_button_container);
3283 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003284 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003285 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003286 }
3287
Cristina Stancu476493b2013-08-07 17:20:14 +01003288 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003289 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003290 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003291
Winson Chungc51db6a2011-10-05 11:44:49 -07003292 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003293 final SearchManager searchManager =
3294 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3295 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3296
3297 ComponentName activityName = null;
3298 if (globalSearchActivity != null) {
3299 // Check if the global search activity handles voice search
3300 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3301 intent.setPackage(globalSearchActivity.getPackageName());
3302 activityName = intent.resolveActivity(getPackageManager());
3303 }
3304
3305 if (activityName == null) {
3306 // Fallback: check if an activity other than the global search activity
3307 // resolves this
3308 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3309 activityName = intent.resolveActivity(getPackageManager());
3310 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003311 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003312 int coi = getCurrentOrientationIndexForGlobalIcons();
3313 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003314 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3315 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3316 if (sVoiceSearchIcon[coi] == null) {
3317 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3318 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3319 TOOLBAR_ICON_METADATA_NAME);
3320 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003321 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003322 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003323 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003324 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003325 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003326 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003327 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003328 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003329 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003330 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003331 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003332 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003333
Cristina Stancu476493b2013-08-07 17:20:14 +01003334 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003335 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3336 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003337 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003338 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003339 }
3340
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003341 public void setVoiceButtonProxyVisible(boolean visible) {
3342 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3343 if (voiceButtonProxy != null) {
3344 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3345 }
3346 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003347 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003348 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003349 */
3350 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003351 final View marketButton = findViewById(R.id.market_button);
3352 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3353 // Find the app market activity by resolving an intent.
3354 // (If multiple app markets are installed, it will return the ResolverActivity.)
3355 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003356 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003357 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003358 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003359 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003360 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3361 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003362 marketButton.setVisibility(View.VISIBLE);
3363 } else {
3364 // We should hide and disable the view so that we don't try and restore the visibility
3365 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3366 marketButton.setVisibility(View.GONE);
3367 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003368 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003369 }
3370
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003371 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003372 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3373 Resources r = getResources();
3374 Drawable marketIconDrawable = d.newDrawable(r);
3375 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3376 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3377 marketIconDrawable.setBounds(0, 0, w, h);
3378
3379 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003380 }
3381
Michael Jurkad7c28052012-04-27 15:43:36 -07003382 @Override
3383 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003384 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003385 final List<CharSequence> text = event.getText();
3386 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003387 // Populate event with a fake title based on the current state.
3388 if (mState == State.APPS_CUSTOMIZE) {
3389 text.add(getString(R.string.all_apps_button_label));
3390 } else {
3391 text.add(getString(R.string.all_apps_home_button_label));
3392 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003393 return result;
3394 }
3395
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003396 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003397 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003398 */
3399 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3400 @Override
3401 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003402 closeSystemDialogs();
3403 }
3404 }
3405
3406 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003407 * Receives notifications whenever the appwidgets are reset.
3408 */
3409 private class AppWidgetResetObserver extends ContentObserver {
3410 public AppWidgetResetObserver() {
3411 super(new Handler());
3412 }
3413
3414 @Override
3415 public void onChange(boolean selfChange) {
3416 onAppWidgetReset();
3417 }
3418 }
3419
3420 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003421 * If the activity is currently paused, signal that we need to run the passed Runnable
3422 * in onResume.
3423 *
3424 * This needs to be called from incoming places where resources might have been loaded
3425 * while we are paused. That is becaues the Configuration might be wrong
3426 * when we're not running, and if it comes back to what it was when we
3427 * were paused, we are not restarted.
3428 *
3429 * Implementation of the method from LauncherModel.Callbacks.
3430 *
3431 * @return true if we are currently paused. The caller might be able to
3432 * skip some work in that case since we will come back again.
3433 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003434 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003435 if (mPaused) {
3436 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003437 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003438 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003439 }
3440 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003441 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003442 return true;
3443 } else {
3444 return false;
3445 }
3446 }
3447
Michael Jurkac402cd92013-05-20 15:49:32 +02003448 private boolean waitUntilResume(Runnable run) {
3449 return waitUntilResume(run, false);
3450 }
3451
Michael Jurka1e2f4652013-07-08 18:03:46 -07003452 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003453 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003454 }
3455
Michael Jurka7607c2f2013-04-03 14:33:19 -07003456 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003457 * If the activity is currently paused, signal that we need to re-run the loader
3458 * in onResume.
3459 *
3460 * This needs to be called from incoming places where resources might have been loaded
3461 * while we are paused. That is becaues the Configuration might be wrong
3462 * when we're not running, and if it comes back to what it was when we
3463 * were paused, we are not restarted.
3464 *
3465 * Implementation of the method from LauncherModel.Callbacks.
3466 *
3467 * @return true if we are currently paused. The caller might be able to
3468 * skip some work in that case since we will come back again.
3469 */
3470 public boolean setLoadOnResume() {
3471 if (mPaused) {
3472 Log.i(TAG, "setLoadOnResume");
3473 mOnResumeNeedsLoad = true;
3474 return true;
3475 } else {
3476 return false;
3477 }
3478 }
3479
3480 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003482 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003484 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003485 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003486 } else {
3487 return SCREEN_COUNT / 2;
3488 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003490
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 /**
3492 * Refreshes the shortcuts shown on the workspace.
3493 *
3494 * Implementation of the method from LauncherModel.Callbacks.
3495 */
3496 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003497 // If we're starting binding all over again, clear any bind calls we'd postponed in
3498 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3499 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003500 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003501
Winson Chungd64d1762013-08-20 14:37:16 -07003502 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003503 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003504 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003505
Michael Jurka05bf6442011-11-30 20:28:53 -08003506 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003507 if (mHotseat != null) {
3508 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003509 }
3510 }
3511
Adam Cohendcd297f2013-06-18 13:13:40 -07003512 @Override
3513 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003514 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003515
3516 // Create the new empty page
Winson Chung64359a52013-07-08 17:17:08 -07003517 mWorkspace.addExtraEmptyScreen();
Winson Chung0e6a7132013-08-23 12:55:10 -07003518
3519 // Create the custom content page (this call updates mDefaultScreen which calls
3520 // setCurrentPage() so ensure that all pages are added before calling this)
3521 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3522 mWorkspace.createCustomContentPage();
3523 }
Winson Chung64359a52013-07-08 17:17:08 -07003524 }
3525
3526 @Override
3527 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003528 int count = orderedScreenIds.size();
3529 for (int i = 0; i < count; i++) {
Winson Chung76828c82013-08-19 15:43:29 -07003530 Log.w(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")");
Adam Cohen89bddfa2013-08-20 11:57:13 -07003531 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003532 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003533 }
3534
Adam Cohen39a06042013-07-19 14:30:12 -07003535 private boolean shouldShowWeightWatcher() {
3536 String spKey = LauncherAppState.getSharedPreferencesKey();
3537 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003538 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003539
3540 return show;
3541 }
3542
3543 private void toggleShowWeightWatcher() {
3544 String spKey = LauncherAppState.getSharedPreferencesKey();
3545 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3546 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3547
3548 show = !show;
3549
3550 SharedPreferences.Editor editor = sp.edit();
3551 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3552 editor.commit();
3553
3554 if (mWeightWatcher != null) {
3555 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3556 }
3557 }
3558
Winson Chungd64d1762013-08-20 14:37:16 -07003559 public void bindAppsAdded(final ArrayList<Long> newScreens,
3560 final ArrayList<ItemInfo> addNotAnimated,
3561 final ArrayList<ItemInfo> addAnimated) {
3562 Runnable r = new Runnable() {
3563 public void run() {
3564 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
3565 }
3566 };
3567 if (waitUntilResume(r)) {
3568 return;
3569 }
3570
3571 Log.w(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")");
3572
3573 // Add the new screens
3574 bindAddScreens(newScreens);
3575
3576 // We add the items without animation on non-visible pages, and with
3577 // animations on the new page (which we will try and snap to).
3578 if (!addNotAnimated.isEmpty()) {
3579 bindItems(addNotAnimated, 0,
3580 addNotAnimated.size(), false);
3581 }
3582 if (!addAnimated.isEmpty()) {
3583 bindItems(addAnimated, 0,
3584 addAnimated.size(), true);
3585 }
3586 }
3587
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003588 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 * Bind the items start-end from the list.
3590 *
3591 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003592 */
Winson Chung64359a52013-07-08 17:17:08 -07003593 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3594 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003595 Runnable r = new Runnable() {
3596 public void run() {
3597 bindItems(shortcuts, start, end, forceAnimateIcons);
3598 }
3599 };
3600 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003601 return;
3602 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003603
Winson Chungf0c6ae02012-03-21 16:10:31 -07003604 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003605 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3606 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003607 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003608 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003609 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003610 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003612
3613 // Short circuit if we are loading dock items for a configuration which has no dock
3614 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3615 mHotseat == null) {
3616 continue;
3617 }
3618
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 switch (item.itemType) {
3620 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3621 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003622 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003623 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003624
Winson Chung64359a52013-07-08 17:17:08 -07003625 /*
3626 * TODO: FIX collision case
3627 */
Winson Chungce376632013-07-11 16:12:41 -07003628 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3629 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3630 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3631 throw new RuntimeException("OCCUPIED");
3632 }
Winson Chung64359a52013-07-08 17:17:08 -07003633 }
3634
Adam Cohendcd297f2013-06-18 13:13:40 -07003635 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3636 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003637 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003638 // Animate all the applications up now
3639 shortcut.setAlpha(0f);
3640 shortcut.setScaleX(0f);
3641 shortcut.setScaleY(0f);
3642 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003643 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003644 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003645 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003646 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003647 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003648 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003649 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003650 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3651 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003653 default:
3654 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003656 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003657
Winson Chung997a9232013-07-24 15:33:46 -07003658 if (animateIcons) {
3659 // Animate to the correct page
3660 if (newShortcutsScreenId > -1) {
3661 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3662 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3663 if (newShortcutsScreenId != currentScreenId) {
3664 mWorkspace.snapToPage(newScreenIndex);
3665 }
3666 }
3667
Winson Chung64359a52013-07-08 17:17:08 -07003668 // We post the animation slightly delayed to prevent slowdowns when we are loading
3669 // right after we return to launcher.
3670 mWorkspace.postDelayed(new Runnable() {
3671 public void run() {
3672 anim.playTogether(bounceAnims);
3673 anim.start();
3674 }
3675 }, NEW_APPS_ANIMATION_DELAY);
3676 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003677 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003678 }
3679
Joe Onorato9c1289c2009-08-17 11:03:03 -04003680 /**
3681 * Implementation of the method from LauncherModel.Callbacks.
3682 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003683 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003684 Runnable r = new Runnable() {
3685 public void run() {
3686 bindFolders(folders);
3687 }
3688 };
3689 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003690 return;
3691 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003692 sFolders.clear();
3693 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003694 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695
3696 /**
3697 * Add the views for a widget to the workspace.
3698 *
3699 * Implementation of the method from LauncherModel.Callbacks.
3700 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003701 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003702 Runnable r = new Runnable() {
3703 public void run() {
3704 bindAppWidget(item);
3705 }
3706 };
3707 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003708 return;
3709 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003710
Daniel Sandler843e8602010-06-07 14:59:01 -04003711 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3712 if (DEBUG_WIDGETS) {
3713 Log.d(TAG, "bindAppWidget: " + item);
3714 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 final Workspace workspace = mWorkspace;
3716
3717 final int appWidgetId = item.appWidgetId;
3718 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003719 if (DEBUG_WIDGETS) {
3720 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3721 }
3722
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3724
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003726 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727
Adam Cohendcd297f2013-06-18 13:13:40 -07003728 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003730 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3731
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 workspace.requestLayout();
3733
Daniel Sandler843e8602010-06-07 14:59:01 -04003734 if (DEBUG_WIDGETS) {
3735 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3736 + (SystemClock.uptimeMillis()-start) + "ms");
3737 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003738 }
3739
Adam Cohen1462de32012-07-24 22:34:36 -07003740 public void onPageBoundSynchronously(int page) {
3741 mSynchronouslyBoundPages.add(page);
3742 }
3743
Joe Onorato9c1289c2009-08-17 11:03:03 -04003744 /**
3745 * Callback saying that there aren't any more items to bind.
3746 *
3747 * Implementation of the method from LauncherModel.Callbacks.
3748 */
Adam Cohene25af792013-06-06 23:08:25 -07003749 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003750 Runnable r = new Runnable() {
3751 public void run() {
3752 finishBindingItems(upgradePath);
3753 }
3754 };
3755 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003756 return;
3757 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003758 if (mSavedState != null) {
3759 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003760 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 mSavedState = null;
3763 }
3764
Adam Cohen1462de32012-07-24 22:34:36 -07003765 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003766
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003767 // If we received the result of any pending adds while the loader was running (e.g. the
3768 // widget configuration forced an orientation change), process them now.
3769 for (int i = 0; i < sPendingAddList.size(); i++) {
3770 completeAdd(sPendingAddList.get(i));
3771 }
3772 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773
Winson Chungc51db6a2011-10-05 11:44:49 -07003774 // Update the market app icon as necessary (the other icons will be managed in response to
3775 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003776 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003777
Winson Chungf0c6ae02012-03-21 16:10:31 -07003778 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003779 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003780 mWorkspace.stripDuplicateApps();
3781 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003782 }
Adam Cohen99894d92013-06-14 11:22:59 -07003783
Adam Cohen66a01fd2013-06-11 12:48:00 -07003784 mWorkspace.post(new Runnable() {
3785 @Override
3786 public void run() {
3787 onFinishBindingItems();
3788 }
3789 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003790 }
3791
Winson Chunga2413752012-04-03 14:22:34 -07003792 private boolean canRunNewAppsAnimation() {
3793 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3794 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3795 }
3796
Winson Chungc9168342013-06-26 14:54:55 -07003797 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3798 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3799 PropertyValuesHolder.ofFloat("alpha", 1f),
3800 PropertyValuesHolder.ofFloat("scaleX", 1f),
3801 PropertyValuesHolder.ofFloat("scaleY", 1f));
3802 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3803 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3804 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3805 return bounceAnim;
3806 }
3807
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003808 @Override
3809 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003810 boolean searchVisible = updateGlobalSearchIcon();
3811 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003812 if (mSearchDropTargetBar != null) {
3813 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3814 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003815 }
3816
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003817 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818 * Add the icons for all apps.
3819 *
3820 * Implementation of the method from LauncherModel.Callbacks.
3821 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003822 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003823 if (mIntentsOnWorkspaceFromUpgradePath != null) {
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003824 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3825 getHotseat().addAllAppsFolder(mIconCache, apps,
3826 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3827 }
Winson Chung64359a52013-07-08 17:17:08 -07003828 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003830 }
3831
3832 /**
3833 * A package was updated.
3834 *
3835 * Implementation of the method from LauncherModel.Callbacks.
3836 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003837 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003838 Runnable r = new Runnable() {
3839 public void run() {
3840 bindAppsUpdated(apps);
3841 }
3842 };
3843 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003844 return;
3845 }
3846
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003847 if (mWorkspace != null) {
3848 mWorkspace.updateShortcuts(apps);
3849 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850 }
3851
3852 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003853 * A package was uninstalled. We take both the super set of packageNames
3854 * in addition to specific applications to remove, the reason being that
3855 * this can be called when a package is updated as well. In that scenario,
3856 * we only remove specific components from the workspace, where as
3857 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003858 *
3859 * Implementation of the method from LauncherModel.Callbacks.
3860 */
Winson Chung83892cc2013-05-01 16:53:33 -07003861 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3862 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003863 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003864 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003865 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003866 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003867 }
Winson Chungd64d1762013-08-20 14:37:16 -07003868 };
3869 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003870 return;
3871 }
3872
Winson Chung64359a52013-07-08 17:17:08 -07003873 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003874 mWorkspace.removeItemsByPackageName(packageNames);
3875 } else {
3876 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003877 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003878
Winson Chunga1820962011-10-03 16:31:06 -07003879 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003880 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 }
Joe Onoratobe386092009-11-17 17:32:16 -08003882
3883 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003884 * A number of packages were updated.
3885 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003886 private ArrayList<Object> mWidgetsAndShortcuts;
3887 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003888 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003889 bindPackagesUpdated(mWidgetsAndShortcuts);
3890 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003891 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003892 };
3893
3894 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3895 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3896 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003897 return;
3898 }
3899
Winson Chung64359a52013-07-08 17:17:08 -07003900 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003901 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003902 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003903 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003904 }
3905
Winson Chung400438b2011-01-16 17:53:48 -08003906 private int mapConfigurationOriActivityInfoOri(int configOri) {
3907 final Display d = getWindowManager().getDefaultDisplay();
3908 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3909 switch (d.getRotation()) {
3910 case Surface.ROTATION_0:
3911 case Surface.ROTATION_180:
3912 // We are currently in the same basic orientation as the natural orientation
3913 naturalOri = configOri;
3914 break;
3915 case Surface.ROTATION_90:
3916 case Surface.ROTATION_270:
3917 // We are currently in the other basic orientation to the natural orientation
3918 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3919 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3920 break;
3921 }
3922
3923 int[] oriMap = {
3924 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3925 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3926 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3927 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3928 };
3929 // Since the map starts at portrait, we need to offset if this device's natural orientation
3930 // is landscape.
3931 int indexOffset = 0;
3932 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3933 indexOffset = 1;
3934 }
3935 return oriMap[(d.getRotation() + indexOffset) % 4];
3936 }
Adam Cohen446e9402011-09-15 18:21:21 -07003937
Winson Chung4b919f82012-05-01 10:44:08 -07003938 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003939 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003940 getResources().getBoolean(R.bool.allow_rotation);
3941 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003942 }
Winson Chung4b919f82012-05-01 10:44:08 -07003943 public void lockScreenOrientation() {
3944 if (isRotationEnabled()) {
3945 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3946 .getConfiguration().orientation));
3947 }
3948 }
3949 public void unlockScreenOrientation(boolean immediate) {
3950 if (isRotationEnabled()) {
3951 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003952 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003953 } else {
3954 mHandler.postDelayed(new Runnable() {
3955 public void run() {
3956 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3957 }
3958 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003959 }
Winson Chung4b919f82012-05-01 10:44:08 -07003960 }
Winson Chung400438b2011-01-16 17:53:48 -08003961 }
3962
Winson Chung82f55532011-08-09 14:14:23 -07003963 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003964 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003965 if (DISABLE_CLINGS) {
3966 return false;
3967 }
3968
Brett Chabot2a9e2282011-08-23 15:03:13 -07003969 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003970 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003971
Michael Jurkae233a8b2013-03-19 13:49:20 +01003972 // Restricted secondary users (child mode) will potentially have very few apps
3973 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003974// boolean supportsLimitedUsers =
3975// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3976// Account[] accounts = AccountManager.get(this).getAccounts();
3977// if (supportsLimitedUsers && accounts.length == 0) {
3978// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3979// Bundle restrictions = um.getUserRestrictions();
3980// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3981// return false;
3982// }
3983// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003984 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003985 }
Michael Jurka22143132012-10-04 17:42:38 +02003986
Winson Chung7d7541e2011-09-16 20:14:36 -07003987 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003988 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003989 if (cling != null) {
3990 cling.init(this, positionData);
3991 cling.setVisibility(View.VISIBLE);
3992 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3993 if (animate) {
3994 cling.buildLayer();
3995 cling.setAlpha(0f);
3996 cling.animate()
3997 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003998 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003999 .setDuration(SHOW_CLING_DURATION)
4000 .setStartDelay(delay)
4001 .start();
4002 } else {
4003 cling.setAlpha(1f);
4004 }
Michael Jurka22143132012-10-04 17:42:38 +02004005 cling.setFocusableInTouchMode(true);
4006 cling.post(new Runnable() {
4007 public void run() {
4008 cling.setFocusable(true);
4009 cling.requestFocus();
4010 }
4011 });
4012 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4013 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004014 }
4015 return cling;
4016 }
Michael Jurka22143132012-10-04 17:42:38 +02004017
Winson Chung7d7541e2011-09-16 20:14:36 -07004018 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004019 // To catch cases where siblings of top-level views are made invisible, just check whether
4020 // the cling is directly set to GONE before dismissing it.
4021 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004022 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004023 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004024 anim.addListener(new AnimatorListenerAdapter() {
4025 public void onAnimationEnd(Animator animation) {
4026 cling.setVisibility(View.GONE);
4027 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004028 // We should update the shared preferences on a background thread
4029 new Thread("dismissClingThread") {
4030 public void run() {
4031 SharedPreferences.Editor editor = mSharedPrefs.edit();
4032 editor.putBoolean(flag, true);
4033 editor.commit();
4034 }
4035 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004036 };
4037 });
4038 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004039 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004040 }
4041 }
Michael Jurka22143132012-10-04 17:42:38 +02004042
Winson Chung9d9d74f2011-09-19 11:49:12 -07004043 private void removeCling(int id) {
4044 final View cling = findViewById(id);
4045 if (cling != null) {
4046 final ViewGroup parent = (ViewGroup) cling.getParent();
4047 parent.post(new Runnable() {
4048 @Override
4049 public void run() {
4050 parent.removeView(cling);
4051 }
4052 });
Michael Jurka22143132012-10-04 17:42:38 +02004053 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004054 }
4055 }
Michael Jurka974c3862012-05-22 22:00:31 -07004056
4057 private boolean skipCustomClingIfNoAccounts() {
4058 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4059 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4060 if (customCling) {
4061 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004062 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004063 Account[] accounts = am.getAccountsByType("com.google");
4064 return accounts.length == 0;
4065 }
4066 return false;
4067 }
4068
Winson Chung7d7541e2011-09-16 20:14:36 -07004069 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004070 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004071 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004072 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4073 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004074 // If we're not using the default workspace layout, replace workspace cling
4075 // with a custom workspace cling (usually specified in an overlay)
4076 // For now, only do this on tablets
4077 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004078 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004079 // Use a custom cling
4080 View cling = findViewById(R.id.workspace_cling);
4081 ViewGroup clingParent = (ViewGroup) cling.getParent();
4082 int clingIndex = clingParent.indexOfChild(cling);
4083 clingParent.removeViewAt(clingIndex);
4084 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4085 clingParent.addView(customCling, clingIndex);
4086 customCling.setId(R.id.workspace_cling);
4087 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004088 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004089 } else {
4090 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004091 }
4092 }
4093 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004094 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004095 if (isClingsEnabled() &&
4096 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004097 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004098 } else {
4099 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004100 }
4101 }
4102 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004103 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004104 if (isClingsEnabled() &&
4105 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4106 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004107 } else {
4108 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004109 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004110 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004111 }
Michael Jurka104c4562013-07-08 18:03:46 -07004112 protected SharedPreferences getSharedPrefs() {
4113 return mSharedPrefs;
4114 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004115 public boolean isFolderClingVisible() {
4116 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004117 if (cling != null) {
4118 return cling.getVisibility() == View.VISIBLE;
4119 }
4120 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004121 }
Winson Chung82f55532011-08-09 14:14:23 -07004122 public void dismissWorkspaceCling(View v) {
4123 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004125 }
4126 public void dismissAllAppsCling(View v) {
4127 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004128 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4129 }
4130 public void dismissFolderCling(View v) {
4131 Cling cling = (Cling) findViewById(R.id.folder_cling);
4132 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004133 }
4134
Winson Chung80baf5a2010-08-09 16:03:15 -07004135 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004136 * Prints out out state for debugging.
4137 */
4138 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004139 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004140 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004141 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4142 Log.d(TAG, "mRestoring=" + mRestoring);
4143 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4144 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004145 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004146 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004147
Winson Chung785d2eb2011-04-14 16:08:02 -07004148 if (mAppsCustomizeContent != null) {
4149 mAppsCustomizeContent.dumpState();
4150 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004151 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004152 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004153
4154 @Override
4155 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4156 super.dump(prefix, fd, writer, args);
4157 writer.println(" ");
4158 writer.println("Debug logs: ");
4159 for (int i = 0; i < sDumpLogs.size(); i++) {
4160 writer.println(" " + sDumpLogs.get(i));
4161 }
4162 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004163
4164 public static void dumpDebugLogsToConsole() {
4165 Log.d(TAG, "");
4166 Log.d(TAG, "*********************");
4167 Log.d(TAG, "Launcher debug logs: ");
4168 for (int i = 0; i < sDumpLogs.size(); i++) {
4169 Log.d(TAG, " " + sDumpLogs.get(i));
4170 }
4171 Log.d(TAG, "*********************");
4172 Log.d(TAG, "");
4173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004174}
Michael Jurka2763be32011-02-24 11:19:57 -08004175
Michael Jurkaabded662011-03-04 12:06:57 -08004176interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004177 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004178 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004179 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004180 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004181 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004182}