blob: 321c4e7121559d3326ac339703f017cf257c1b6b [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Adam Cohen96d30a12013-07-16 18:13:21 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
79import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070089import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Daniel Sandler325dc232013-06-05 22:57:57 -040098import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100118public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700120 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Daniel Sandlerf061f822013-06-27 13:59:36 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700126 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
Winson Chung70d72102011-08-12 11:24:35 -0700129 private static final int MENU_GROUP_WALLPAPER = 1;
130 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
131 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700132 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
133 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chung2672ff92012-05-04 16:22:30 -0700161 // The Intent extra that defines whether to ignore the launch animation
162 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400163 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
166 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700167 // Type: int
168 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
171 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: boolean
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
179 // Type: long
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
185 // Type: parcelable
186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100188 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700189 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700191 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100192 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700193
Adam Cohen39a06042013-07-19 14:30:12 -0700194 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
195
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700196 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700197 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700198 private State mState = State.WORKSPACE;
199 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800200 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700203 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
204 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700205 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700206 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800209 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
212 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700213 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700214
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800215 private final BroadcastReceiver mCloseSystemDialogsReceiver
216 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800217 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private LayoutInflater mInflater;
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800222 private View mQsbDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700223 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private DragLayer mDragLayer;
225 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700226 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700228 private AppWidgetManager mAppWidgetManager;
229 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Michael Jurkac9d95c52011-08-29 14:03:34 -0700231 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700232 private AppWidgetProviderInfo mPendingAddWidgetInfo;
233
Michael Jurka0280c3b2010-09-17 15:00:07 -0700234 private int[] mTmpAddItemCellCoordinates = new int[2];
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private FolderInfo mFolderInfo;
237
Winson Chung3d503fb2011-07-13 17:25:49 -0700238 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800239 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700240
Winson Chungc51db6a2011-10-05 11:44:49 -0700241 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700242 private AppsCustomizeTabHost mAppsCustomizeTabHost;
243 private AppsCustomizePagedView mAppsCustomizeContent;
244 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
248 // scroll issues (because the workspace may not have been measured yet) and extra work.
249 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
250 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
252 private SpannableStringBuilder mDefaultKeySsb = null;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private boolean mWorkspaceLoading = true;
255
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800256 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private boolean mRestoring;
258 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700259 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurka1e2f4652013-07-08 18:03:46 -0700261 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
263
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // Keep track of whether the user has left launcher
265 private static boolean sPausedFromUserAction = false;
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedInstanceState;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800271 private boolean mUserPresent = true;
272 private boolean mVisible = false;
273 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700274 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700276 private static LocaleConfiguration sLocaleConfiguration = null;
277
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700278 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700279
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700280 private Intent mAppMarketIntent = null;
281
Adam Cohended9f8d2010-11-03 13:25:16 -0700282 // Related to the auto-advancing of widgets
283 private final int ADVANCE_MSG = 1;
284 private final int mAdvanceInterval = 20000;
285 private final int mAdvanceStagger = 250;
286 private long mAutoAdvanceSentTime;
287 private long mAutoAdvanceTimeLeft = -1;
288 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
289 new HashMap<View, AppWidgetProviderInfo>();
290
Winson Chung400438b2011-01-16 17:53:48 -0800291 // Determines how long to wait after a rotation before restoring the screen orientation to
292 // match the sensor state.
293 private final int mRestoreScreenOrientationDelay = 500;
294
Michael Jurka4ef207b2010-11-29 17:05:45 -0800295 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700296 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
297 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
298 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800299
Craig Mautner360310b2012-10-26 15:13:08 -0700300 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700301
Adam Cohen1462de32012-07-24 22:34:36 -0700302 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
303
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700304 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
305
Winson Chung46353de2012-02-16 14:05:10 -0800306 // We only want to get the SharedPreferences once since it does an FS stat each time we get
307 // it from the context.
308 private SharedPreferences mSharedPrefs;
309
Adam Cohene25af792013-06-06 23:08:25 -0700310 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
311
Winson Chungf0c6ae02012-03-21 16:10:31 -0700312 // Holds the page that we need to animate to, and the icon views that we need to animate up
313 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700314 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700315 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700316 private ImageView mFolderIconImageView;
317 private Bitmap mFolderIconBitmap;
318 private Canvas mFolderIconCanvas;
319 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700320
Michael Jurkaddd62e92011-02-16 17:49:14 -0800321 private BubbleTextView mWaitingForResume;
322
Michael Jurka22143132012-10-04 17:42:38 +0200323 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
324 = new HideFromAccessibilityHelper();
325
Michael Jurkac1f5d262011-09-30 19:32:27 -0700326 private Runnable mBuildLayersRunnable = new Runnable() {
327 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700328 if (mWorkspace != null) {
329 mWorkspace.buildPageHardwareLayers();
330 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700331 }
332 };
333
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800334 private static ArrayList<PendingAddArguments> sPendingAddList
335 = new ArrayList<PendingAddArguments>();
336
Michael Jurka0a457bf2012-11-19 14:05:05 -0800337 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
338
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800339 private static class PendingAddArguments {
340 int requestCode;
341 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700342 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700343 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800344 int cellX;
345 int cellY;
346 }
347
Michael Jurka0a457bf2012-11-19 14:05:05 -0800348 private static boolean isPropertyEnabled(String propertyName) {
349 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700350 }
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 @Override
353 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700354 if (DEBUG_STRICT_MODE) {
355 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
356 .detectDiskReads()
357 .detectDiskWrites()
358 .detectNetwork() // or .detectAll() for all detectable problems
359 .penaltyLog()
360 .build());
361 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
362 .detectLeakedSqlLiteObjects()
363 .detectLeakedClosableObjects()
364 .penaltyLog()
365 .penaltyDeath()
366 .build());
367 }
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400370
371 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
372 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400373 LauncherAppState app = LauncherAppState.getInstance();
374 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700375 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800376 mModel = app.setLauncher(this);
377 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400378 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700380
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
383 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700384
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700385 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
386 // this also ensures that any synchronous binding below doesn't re-trigger another
387 // LauncherModel load.
388 mPaused = false;
389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200391 android.os.Debug.startMethodTracing(
392 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
394
395 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 setContentView(R.layout.launcher);
397 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700398 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800400 registerContentObservers();
401
Joe Onorato7c312c12009-08-13 21:36:53 -0700402 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 mSavedState = savedInstanceState;
405 restoreState(mSavedState);
406
Winson Chunga12a2502010-12-20 14:41:35 -0800407 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700408 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200409 mAppsCustomizeContent.onPackagesUpdated(
410 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700411 }
Winson Chunga12a2502010-12-20 14:41:35 -0800412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 if (PROFILE_STARTUP) {
414 android.os.Debug.stopMethodTracing();
415 }
416
417 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700418 if (sPausedFromUserAction) {
419 // If the user leaves launcher, then we should just load items asynchronously when
420 // they return.
421 mModel.startLoader(true, -1);
422 } else {
423 // We only load the page synchronously if the user rotates (or triggers a
424 // configuration change) while launcher is in the foreground
425 mModel.startLoader(true, mWorkspace.getCurrentPage());
426 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 }
428
429 // For handling default keys
430 mDefaultKeySsb = new SpannableStringBuilder();
431 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800432
433 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
434 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800435
Adam Cohenf9426d52012-06-04 17:26:21 -0700436 updateGlobalIcons();
437
438 // On large interfaces, we want the screen to auto-rotate based on the current orientation
439 unlockScreenOrientation(true);
440 }
441
Winson Chung4a2afa32012-07-19 14:53:05 -0700442 protected void onUserLeaveHint() {
443 super.onUserLeaveHint();
444 sPausedFromUserAction = true;
445 }
446
Adam Cohenf9426d52012-06-04 17:26:21 -0700447 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700448 boolean searchVisible = false;
449 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800450 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700451 int coi = getCurrentOrientationIndexForGlobalIcons();
452 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
453 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700454 updateAppMarketIcon();
455 searchVisible = updateGlobalSearchIcon();
456 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700457 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700458 if (sGlobalSearchIcon[coi] != null) {
459 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700460 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700461 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700462 if (sVoiceSearchIcon[coi] != null) {
463 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700464 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700465 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700466 if (sAppMarketIcon[coi] != null) {
467 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800468 }
Winson Chungadf0c182012-08-23 14:59:07 -0700469 if (mSearchDropTargetBar != null) {
470 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
Romain Guycbb89e42009-06-08 15:52:54 -0700473
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700475 if (sLocaleConfiguration == null) {
476 new AsyncTask<Void, Void, LocaleConfiguration>() {
477 @Override
478 protected LocaleConfiguration doInBackground(Void... unused) {
479 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
480 readConfiguration(Launcher.this, localeConfiguration);
481 return localeConfiguration;
482 }
483
484 @Override
485 protected void onPostExecute(LocaleConfiguration result) {
486 sLocaleConfiguration = result;
487 checkForLocaleChange(); // recursive, but now with a locale configuration
488 }
489 }.execute();
490 return;
491 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700492
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 final Configuration configuration = getResources().getConfiguration();
494
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 final String locale = configuration.locale.toString();
497
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700498 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 final int mcc = configuration.mcc;
500
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700501 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 final int mnc = configuration.mnc;
503
Romain Guy84f296c2009-11-04 15:00:44 -0800504 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505
Romain Guy84f296c2009-11-04 15:00:44 -0800506 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700507 sLocaleConfiguration.locale = locale;
508 sLocaleConfiguration.mcc = mcc;
509 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700510
Joe Onorato0589f0f2010-02-08 13:44:00 -0800511 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700512
513 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
514 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700515 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700516 public void run() {
517 writeConfiguration(Launcher.this, localeConfiguration);
518 }
519 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700520 }
521 }
522
523 private static class LocaleConfiguration {
524 public String locale;
525 public int mcc = -1;
526 public int mnc = -1;
527 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700528
Romain Guy98d01652009-06-30 16:21:04 -0700529 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
530 DataInputStream in = null;
531 try {
532 in = new DataInputStream(context.openFileInput(PREFERENCES));
533 configuration.locale = in.readUTF();
534 configuration.mcc = in.readInt();
535 configuration.mnc = in.readInt();
536 } catch (FileNotFoundException e) {
537 // Ignore
538 } catch (IOException e) {
539 // Ignore
540 } finally {
541 if (in != null) {
542 try {
543 in.close();
544 } catch (IOException e) {
545 // Ignore
546 }
547 }
548 }
549 }
550
551 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
552 DataOutputStream out = null;
553 try {
554 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
555 out.writeUTF(configuration.locale);
556 out.writeInt(configuration.mcc);
557 out.writeInt(configuration.mnc);
558 out.flush();
559 } catch (FileNotFoundException e) {
560 // Ignore
561 } catch (IOException e) {
562 //noinspection ResultOfMethodCallIgnored
563 context.getFileStreamPath(PREFERENCES).delete();
564 } finally {
565 if (out != null) {
566 try {
567 out.close();
568 } catch (IOException e) {
569 // Ignore
570 }
571 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 }
573 }
574
Bjorn Bringertc459e522013-06-07 19:36:01 +0100575 public LayoutInflater getInflater() {
576 return mInflater;
577 }
578
Adam Cohen716b51e2011-06-30 12:09:54 -0700579 public DragLayer getDragLayer() {
580 return mDragLayer;
581 }
582
Winson Chung36a62fe2012-05-06 18:04:42 -0700583 boolean isDraggingEnabled() {
584 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
585 // that is subsequently removed from the workspace in startBinding().
586 return !mModel.isLoadingWorkspace();
587 }
588
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 static int getScreen() {
590 synchronized (sLock) {
591 return sScreen;
592 }
593 }
594
595 static void setScreen(int screen) {
596 synchronized (sLock) {
597 sScreen = screen;
598 }
599 }
600
Winson Chung557d6ed2011-07-08 15:34:52 -0700601 /**
602 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
603 * a configuration step, this allows the proper animations to run after other transitions.
604 */
605 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700606 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800607 switch (args.requestCode) {
608 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700609 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700610 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800611 break;
612 case REQUEST_PICK_SHORTCUT:
613 processShortcut(args.intent);
614 break;
615 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700616 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700617 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700618 result = true;
619 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800620 case REQUEST_CREATE_APPWIDGET:
621 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700622 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700623 result = true;
624 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800625 case REQUEST_PICK_WALLPAPER:
626 // We just wanted the activity result here so we can clear mWaitingForResult
627 break;
628 }
Michael Jurka27614d22012-04-02 06:40:22 -0700629 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
630 // if you turned the screen off and then back while in All Apps, Launcher would not
631 // return to the workspace. Clearing mAddInfo.container here fixes this issue
632 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700633 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800634 }
635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700637 protected void onActivityResult(
638 final int requestCode, final int resultCode, final Intent data) {
639 if (requestCode == REQUEST_BIND_APPWIDGET) {
640 int appWidgetId = data != null ?
641 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
642 if (resultCode == RESULT_CANCELED) {
643 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
644 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700645 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700646 }
647 return;
648 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700649 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
651 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700652 mWaitingForResult = false;
653
Adam Cohened66b2b2012-01-23 17:28:51 -0800654 // We have special handling for widgets
655 if (isWidgetDrop) {
656 int appWidgetId = data != null ?
657 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700658 if (appWidgetId < 0) {
659 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
660 "widget configuration activity.");
661 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
662 } else {
663 completeTwoStageWidgetDrop(resultCode, appWidgetId);
664 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800665 return;
666 }
667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 // The pattern used here is that a user PICKs a specific application,
669 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
672 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700673 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800674 final PendingAddArguments args = new PendingAddArguments();
675 args.requestCode = requestCode;
676 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700677 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700678 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700679 args.cellX = mPendingAddInfo.cellX;
680 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 if (isWorkspaceLocked()) {
682 sPendingAddList.add(args);
683 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700684 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700688 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800689 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
690 null);
691 }
692
693 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700694 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700695 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800696 Runnable onCompleteRunnable = null;
697 int animationType = 0;
698
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700699 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800700 if (resultCode == RESULT_OK) {
701 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
702 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700703 mPendingAddWidgetInfo);
704 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800705 onCompleteRunnable = new Runnable() {
706 @Override
707 public void run() {
708 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800710 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
711 null);
712 }
713 };
714 } else if (resultCode == RESULT_CANCELED) {
715 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
716 onCompleteRunnable = new Runnable() {
717 @Override
718 public void run() {
719 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
720 null);
721 }
722 };
723 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700724 if (mDragLayer.getAnimatedView() != null) {
725 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
726 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
727 animationType, boundWidget, true);
728 } else {
729 // The animated view may be null in the case of a rotation during widget configuration
730 onCompleteRunnable.run();
731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
733
734 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700735 protected void onStop() {
736 super.onStop();
737 FirstFrameAnimatorHelper.setIsVisible(false);
738 }
739
740 @Override
741 protected void onStart() {
742 super.onStart();
743 FirstFrameAnimatorHelper.setIsVisible(true);
744 }
745
746 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200748 long startTime = 0;
749 if (DEBUG_RESUME_TIME) {
750 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400751 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200752 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700754
Winson Chung4a2afa32012-07-19 14:53:05 -0700755 // Restore the previous launcher state
756 if (mOnResumeState == State.WORKSPACE) {
757 showWorkspace(false);
758 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
759 showAllApps(false);
760 }
761 mOnResumeState = State.NONE;
762
Craig Mautner360310b2012-10-26 15:13:08 -0700763 // Background was set to gradient in onPause(), restore to black if in all apps.
764 setWorkspaceBackground(mState == State.WORKSPACE);
765
Winson Chungde0fb8f2012-05-08 14:37:08 -0700766 // Process any items that were added while Launcher was away
767 InstallShortcutReceiver.flushInstallQueue(this);
768
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800769 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700770 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700771 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700773 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400774 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700775 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700777 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200778 // We might have postponed some bind calls until onResume (see waitUntilResume) --
779 // execute them here
780 long startTimeCallbacks = 0;
781 if (DEBUG_RESUME_TIME) {
782 startTimeCallbacks = System.currentTimeMillis();
783 }
784
785 if (mAppsCustomizeContent != null) {
786 mAppsCustomizeContent.setBulkBind(true);
787 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700788 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
789 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200790 }
791 if (mAppsCustomizeContent != null) {
792 mAppsCustomizeContent.setBulkBind(false);
793 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700794 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200795 if (DEBUG_RESUME_TIME) {
796 Log.d(TAG, "Time spent processing callbacks in onResume: " +
797 (System.currentTimeMillis() - startTimeCallbacks));
798 }
Michael Jurka447bf842013-05-15 14:52:15 +0200799 }
Winson Chunge4e50662012-01-23 14:45:13 -0800800
801 // Reset the pressed state of icons that were locked in the press state while activities
802 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800803 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800804 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800805 mWaitingForResume.setStayPressed(false);
806 }
Winson Chunge4e50662012-01-23 14:45:13 -0800807 if (mAppsCustomizeContent != null) {
808 // Resets the previous all apps icon press state
809 mAppsCustomizeContent.resetDrawableState();
810 }
Adam Cohen06dff352012-06-01 17:17:08 -0700811 // It is possible that widgets can receive updates while launcher is not in the foreground.
812 // Consequently, the widgets will be inflated in the orientation of the foreground activity
813 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
814 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700815 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700816
817 // Again, as with the above scenario, it's possible that one or more of the global icons
818 // were updated in the wrong orientation.
819 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200820 if (DEBUG_RESUME_TIME) {
821 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
822 }
Adam Cohen06dff352012-06-01 17:17:08 -0700823 }
824
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700826 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700827 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
828 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
829 // when Launcher resumes and we are still in AllApps.
830 updateWallpaperVisibility(true);
831
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700832 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700833 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800834 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700835 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700836 }
Romain Guycbb89e42009-06-08 15:52:54 -0700837
Adam Cohen66a01fd2013-06-11 12:48:00 -0700838 protected void onFinishBindingItems() {
839 }
840
Adam Cohenbffe7452013-07-22 18:21:45 -0700841 QSBScroller mQsbScroller = new QSBScroller() {
842 int scrollY = 0;
843
844 @Override
845 public void setScrollY(int scroll) {
846 scrollY = scroll;
847
848 if (mWorkspace.isOnOrMovingToCustomContent()) {
849 mSearchDropTargetBar.setTranslationY(- scrollY);
850 }
851 }
852 };
853
854 public void resetQSBScroll() {
855 mSearchDropTargetBar.animate().translationY(0).start();
856 }
857
858 public interface CustomContentCallbacks {
859 // Custom content is completely shown
860 public void onShow();
861
862 // Custom content is completely hidden
863 public void onHide();
864 }
865
866 public interface QSBScroller {
867 public void setScrollY(int scrollY);
868 }
869
Adam Cohen66a01fd2013-06-11 12:48:00 -0700870 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Adam Cohenbffe7452013-07-22 18:21:45 -0700871 public QSBScroller addCustomContentToLeft(View customContent) {
872 return addCustomContentToLeft(customContent, null);
873 }
874
875 public QSBScroller addCustomContentToLeft(View customContent,
876 CustomContentCallbacks callbacks) {
877 mWorkspace.addCustomContentToLeft(customContent, callbacks);
878 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700879 }
880
Adam Cohenedb40762013-07-18 16:45:45 -0700881 // The custom content needs to offset its content to account for the QSB
882 public int getTopOffsetForCustomContent() {
883 return mWorkspace.getPaddingTop();
884 }
885
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700886 @Override
887 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888 // Flag the loader to stop early before switching
889 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700890 if (mAppsCustomizeContent != null) {
891 mAppsCustomizeContent.surrender();
892 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800893 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700894 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700895
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800896 // We can't hide the IME if it was forced open. So don't bother
897 /*
898 @Override
899 public void onWindowFocusChanged(boolean hasFocus) {
900 super.onWindowFocusChanged(hasFocus);
901
902 if (hasFocus) {
903 final InputMethodManager inputManager = (InputMethodManager)
904 getSystemService(Context.INPUT_METHOD_SERVICE);
905 WindowManager.LayoutParams lp = getWindow().getAttributes();
906 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
907 android.os.Handler()) {
908 protected void onReceiveResult(int resultCode, Bundle resultData) {
909 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
910 }
911 });
912 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
913 }
914 }
915 */
916
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 private boolean acceptFilter() {
918 final InputMethodManager inputManager = (InputMethodManager)
919 getSystemService(Context.INPUT_METHOD_SERVICE);
920 return !inputManager.isFullscreenMode();
921 }
922
923 @Override
924 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700925 final int uniChar = event.getUnicodeChar();
926 final boolean handled = super.onKeyDown(keyCode, event);
927 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
928 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
930 keyCode, event);
931 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700932 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700933 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700934 // showSearchDialog()
935 // If there are multiple keystrokes before the search dialog takes focus,
936 // onSearchRequested() will be called for every keystroke,
937 // but it is idempotent, so it's fine.
938 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940 }
941
Joe Onorato8a9625e2010-01-28 15:55:35 -0800942 // Eat the long press event so the keyboard doesn't come up.
943 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
944 return true;
945 }
946
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 return handled;
948 }
949
Karl Rosaen138a0412009-04-23 19:00:21 -0700950 private String getTypedText() {
951 return mDefaultKeySsb.toString();
952 }
953
954 private void clearTypedText() {
955 mDefaultKeySsb.clear();
956 mDefaultKeySsb.clearSpans();
957 Selection.setSelection(mDefaultKeySsb, 0);
958 }
959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700961 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
962 * State
963 */
964 private static State intToState(int stateOrdinal) {
965 State state = State.WORKSPACE;
966 final State[] stateValues = State.values();
967 for (int i = 0; i < stateValues.length; i++) {
968 if (stateValues[i].ordinal() == stateOrdinal) {
969 state = stateValues[i];
970 break;
971 }
972 }
973 return state;
974 }
975
976 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 * Restores the previous state, if it exists.
978 *
979 * @param savedState The previous state.
980 */
981 private void restoreState(Bundle savedState) {
982 if (savedState == null) {
983 return;
984 }
985
Michael Jurka883f55b2010-10-21 15:47:14 -0700986 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700987 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700988 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800989 }
990
Winson Chungf0c6ae02012-03-21 16:10:31 -0700991 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700993 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 }
995
Winson Chung3d503fb2011-07-13 17:25:49 -0700996 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700997 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700998
Winson Chung3d503fb2011-07-13 17:25:49 -0700999 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1000 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1003 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001004 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1005 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1006 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001007 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 mRestoring = true;
1009 }
1010
1011 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1012 if (renameFolder) {
1013 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001014 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 mRestoring = true;
1016 }
Winson Chunga12a2502010-12-20 14:41:35 -08001017
Winson Chung785d2eb2011-04-14 16:08:02 -07001018 // Restore the AppsCustomize tab
1019 if (mAppsCustomizeTabHost != null) {
1020 String curTab = savedState.getString("apps_customize_currentTab");
1021 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001022 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001023 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001024 mAppsCustomizeContent.loadAssociatedPages(
1025 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001026 }
1027
Winson Chung5afbf7b2011-07-25 11:53:08 -07001028 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1029 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 }
1032
1033 /**
1034 * Finds all the views we need and configure them properly.
1035 */
1036 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001037 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001038
Craig Mautner360310b2012-10-26 15:13:08 -07001039 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001040 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1041 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001042 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001043
1044 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1045 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046
Winson Chung4c98d922011-05-31 16:50:48 -07001047 // Setup the drag layer
1048 mDragLayer.setup(this, dragController);
1049
Winson Chung3d503fb2011-07-13 17:25:49 -07001050 // Setup the hotseat
1051 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1052 if (mHotseat != null) {
1053 mHotseat.setup(this);
1054 }
1055
Winson Chung4c98d922011-05-31 16:50:48 -07001056 // Setup the workspace
1057 mWorkspace.setHapticFeedbackEnabled(false);
1058 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001059 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001060 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001061
Winson Chungf0ea4d32011-06-06 14:27:16 -07001062 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001063 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001064
Winson Chungf0ea4d32011-06-06 14:27:16 -07001065 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001066 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001067 mAppsCustomizeContent = (AppsCustomizePagedView)
1068 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1069 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001070
Winson Chung3d503fb2011-07-13 17:25:49 -07001071 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001072 dragController.setDragScoller(mWorkspace);
1073 dragController.setScrollView(mDragLayer);
1074 dragController.setMoveTarget(mWorkspace);
1075 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001076 if (mSearchDropTargetBar != null) {
1077 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001078 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001079
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001080 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001081 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001082 mWeightWatcher = new WeightWatcher(this);
1083 mWeightWatcher.setAlpha(0.5f);
1084 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001085 new FrameLayout.LayoutParams(
1086 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001087 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001088 Gravity.BOTTOM)
1089 );
Adam Cohen39a06042013-07-19 14:30:12 -07001090
1091 boolean show = shouldShowWeightWatcher();
1092 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
1095
1096 /**
1097 * Creates a view representing a shortcut.
1098 *
1099 * @param info The data structure describing the shortcut.
1100 *
1101 * @return A View inflated from R.layout.application.
1102 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001103 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001105 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 }
1107
1108 /**
1109 * Creates a view representing a shortcut inflated from the specified resource.
1110 *
1111 * @param layoutResId The id of the XML layout used to create the shortcut.
1112 * @param parent The group the shortcut belongs to.
1113 * @param info The data structure describing the shortcut.
1114 *
1115 * @return A View inflated from layoutResId.
1116 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001117 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001118 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1119 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 return favorite;
1122 }
1123
1124 /**
1125 * Add an application shortcut to the workspace.
1126 *
1127 * @param data The intent describing the application.
1128 * @param cellInfo The position on screen where to create the shortcut.
1129 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001130 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001131 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001132 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001133
Adam Cohenfbba09b2011-07-18 21:43:05 -07001134 // First we check if we already know the exact location where we want to add this item.
1135 if (cellX >= 0 && cellY >= 0) {
1136 cellXY[0] = cellX;
1137 cellXY[1] = cellY;
1138 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001139 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 return;
1141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001143 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001144
Romain Guy73b979d2009-06-09 12:57:21 -07001145 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001146 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001148 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001149 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001150 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001151 } else {
1152 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
1154 }
Romain Guycbb89e42009-06-08 15:52:54 -07001155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 /**
1157 * Add a shortcut to the workspace.
1158 *
1159 * @param data The intent describing the shortcut.
1160 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001162 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001163 int cellY) {
1164 int[] cellXY = mTmpAddItemCellCoordinates;
1165 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001166 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001167
Michael Jurkad3ef3062010-11-23 16:23:58 -08001168 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001169
Adam Cohen558baaf2011-08-15 15:22:57 -07001170 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001171 if (info == null) {
1172 return;
1173 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001174 final View view = createShortcut(info);
1175
Adam Cohenfbba09b2011-07-18 21:43:05 -07001176 // First we check if we already know the exact location where we want to add this item.
1177 if (cellX >= 0 && cellY >= 0) {
1178 cellXY[0] = cellX;
1179 cellXY[1] = cellY;
1180 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001181
1182 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001183 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001184 true, null,null)) {
1185 return;
1186 }
1187 DragObject dragObject = new DragObject();
1188 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001189 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1190 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001191 return;
1192 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001193 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001194 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001195 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001196 foundCellSpan = (result != null);
1197 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001198 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001199 }
1200
1201 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001202 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001203 return;
1204 }
1205
Adam Cohendcd297f2013-06-18 13:13:40 -07001206 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207
1208 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001209 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001210 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
1212 }
1213
Adam Cohen2f093b62012-04-30 18:59:53 -07001214 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1215 int minHeight) {
1216 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001217 // We want to account for the extra amount of padding that we are adding to the widget
1218 // to ensure that it gets the full amount of space that it has requested
1219 int requiredWidth = minWidth + padding.left + padding.right;
1220 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001221 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001222 }
1223
Adam Cohen2f093b62012-04-30 18:59:53 -07001224 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1225 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001226 }
1227
Adam Cohen2f093b62012-04-30 18:59:53 -07001228 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1229 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001230 }
1231
Adam Cohen2f093b62012-04-30 18:59:53 -07001232 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1233 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001234 }
1235
Adam Cohen2f093b62012-04-30 18:59:53 -07001236 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1237 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001238 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001239 }
1240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001242 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001244 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001245 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001247 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001248 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1249 if (appWidgetInfo == null) {
1250 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1251 }
Romain Guycbb89e42009-06-08 15:52:54 -07001252
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001253 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001254 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001255
Adam Cohen2f093b62012-04-30 18:59:53 -07001256 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1257 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001258
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001260 // We have saved the position to which the widget was dragged-- this really only matters
1261 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001262 int[] cellXY = mTmpAddItemCellCoordinates;
1263 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001264 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001265 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001266 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1267 cellXY[0] = mPendingAddInfo.cellX;
1268 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001269 spanXY[0] = mPendingAddInfo.spanX;
1270 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001271 foundCellSpan = true;
1272 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001273 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001274 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001275 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1276 spanXY[1], cellXY, finalSpan);
1277 spanXY[0] = finalSpan[0];
1278 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001279 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001280 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001281 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001282 }
1283
Michael Jurka0280c3b2010-09-17 15:00:07 -07001284 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001285 if (appWidgetId != -1) {
1286 // Deleting an app widget ID is a void call but writes to disk before returning
1287 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001288 new Thread("deleteAppWidgetId") {
1289 public void run() {
1290 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1291 }
1292 }.start();
1293 }
Winson Chung93eef082012-03-23 15:59:27 -07001294 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001295 return;
1296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001298 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001299 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1300 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001301 launcherInfo.spanX = spanXY[0];
1302 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001303 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1304 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001305
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001307 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308
1309 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001310 if (hostView == null) {
1311 // Perform actual inflation because we're live
1312 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1313 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1314 } else {
1315 // The AppWidgetHostView has already been inflated and instantiated
1316 launcherInfo.hostView = hostView;
1317 }
Romain Guycbb89e42009-06-08 15:52:54 -07001318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001320 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001321 launcherInfo.notifyWidgetSizeChanged(this);
1322
Adam Cohendcd297f2013-06-18 13:13:40 -07001323 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001325
1326 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001328 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
Romain Guycbb89e42009-06-08 15:52:54 -07001330
Adam Cohended9f8d2010-11-03 13:25:16 -07001331 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1332 @Override
1333 public void onReceive(Context context, Intent intent) {
1334 final String action = intent.getAction();
1335 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1336 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001337 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001338 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001339
Winson Chungc100e8e2011-08-09 16:02:43 -07001340 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001341 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001342 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1343 mAppsCustomizeTabHost.reset();
1344 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001345 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001346 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1347 mUserPresent = true;
1348 updateRunning();
1349 }
1350 }
1351 };
1352
1353 @Override
1354 public void onAttachedToWindow() {
1355 super.onAttachedToWindow();
1356
1357 // Listen for broadcasts related to user-presence
1358 final IntentFilter filter = new IntentFilter();
1359 filter.addAction(Intent.ACTION_SCREEN_OFF);
1360 filter.addAction(Intent.ACTION_USER_PRESENT);
1361 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001362 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001363 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001364 mVisible = true;
1365 }
1366
1367 @Override
1368 public void onDetachedFromWindow() {
1369 super.onDetachedFromWindow();
1370 mVisible = false;
1371
Adam Cohend113e0c2010-11-11 10:48:05 -08001372 if (mAttached) {
1373 unregisterReceiver(mReceiver);
1374 mAttached = false;
1375 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001376 updateRunning();
1377 }
1378
1379 public void onWindowVisibilityChanged(int visibility) {
1380 mVisible = visibility == View.VISIBLE;
1381 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001382 // The following code used to be in onResume, but it turns out onResume is called when
1383 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1384 // is a more appropriate event to handle
1385 if (mVisible) {
1386 mAppsCustomizeTabHost.onWindowVisible();
1387 if (!mWorkspaceLoading) {
1388 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001389 // We want to let Launcher draw itself at least once before we force it to build
1390 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001391 // apps is nice and speedy.
1392 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001393 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001394 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001395 if (mStarted) return;
1396 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001397 // We delay the layer building a bit in order to give
1398 // other message processing a time to run. In particular
1399 // this avoids a delay in hiding the IME if it was
1400 // currently shown, because doing that may involve
1401 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001402 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001403 final ViewTreeObserver.OnDrawListener listener = this;
1404 mWorkspace.post(new Runnable() {
1405 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001406 if (mWorkspace != null &&
1407 mWorkspace.getViewTreeObserver() != null) {
1408 mWorkspace.getViewTreeObserver().
1409 removeOnDrawListener(listener);
1410 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001411 }
1412 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001413 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001414 }
1415 });
1416 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001417 // When Launcher comes back to foreground, a different Activity might be responsible for
1418 // the app market intent, so refresh the icon
1419 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001420 clearTypedText();
1421 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001422 }
1423
1424 private void sendAdvanceMessage(long delay) {
1425 mHandler.removeMessages(ADVANCE_MSG);
1426 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1427 mHandler.sendMessageDelayed(msg, delay);
1428 mAutoAdvanceSentTime = System.currentTimeMillis();
1429 }
1430
1431 private void updateRunning() {
1432 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1433 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1434 mAutoAdvanceRunning = autoAdvanceRunning;
1435 if (autoAdvanceRunning) {
1436 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1437 sendAdvanceMessage(delay);
1438 } else {
1439 if (!mWidgetsToAdvance.isEmpty()) {
1440 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1441 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1442 }
1443 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001444 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001445 }
1446 }
1447 }
1448
1449 private final Handler mHandler = new Handler() {
1450 @Override
1451 public void handleMessage(Message msg) {
1452 if (msg.what == ADVANCE_MSG) {
1453 int i = 0;
1454 for (View key: mWidgetsToAdvance.keySet()) {
1455 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1456 final int delay = mAdvanceStagger * i;
1457 if (v instanceof Advanceable) {
1458 postDelayed(new Runnable() {
1459 public void run() {
1460 ((Advanceable) v).advance();
1461 }
1462 }, delay);
1463 }
1464 i++;
1465 }
1466 sendAdvanceMessage(mAdvanceInterval);
1467 }
1468 }
1469 };
1470
1471 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001472 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001473 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1474 if (v instanceof Advanceable) {
1475 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001476 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001477 updateRunning();
1478 }
1479 }
1480
1481 void removeWidgetToAutoAdvance(View hostView) {
1482 if (mWidgetsToAdvance.containsKey(hostView)) {
1483 mWidgetsToAdvance.remove(hostView);
1484 updateRunning();
1485 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001486 }
1487
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001489 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001490 launcherInfo.hostView = null;
1491 }
1492
Winson Chung93eef082012-03-23 15:59:27 -07001493 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1494 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1495 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001496 }
1497
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001498 public LauncherAppWidgetHost getAppWidgetHost() {
1499 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 }
Romain Guycbb89e42009-06-08 15:52:54 -07001501
Winson Chunga9abd0e2010-10-27 17:18:37 -07001502 public LauncherModel getModel() {
1503 return mModel;
1504 }
1505
Bjorn Bringertc459e522013-06-07 19:36:01 +01001506 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001507 getWindow().closeAllPanels();
1508
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001509 // Whatever we were doing is hereby canceled.
1510 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001511 }
1512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 @Override
1514 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001515 long startTime = 0;
1516 if (DEBUG_RESUME_TIME) {
1517 startTime = System.currentTimeMillis();
1518 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 super.onNewIntent(intent);
1520
1521 // Close the menu
1522 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001523 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001524 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001525
Jamie Genniscb222e82012-10-23 15:44:26 -07001526 final boolean alreadyOnHome =
1527 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001528 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001529
Jamie Genniscb222e82012-10-23 15:44:26 -07001530 Runnable processIntent = new Runnable() {
1531 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001532 if (mWorkspace == null) {
1533 // Can be cases where mWorkspace is null, this prevents a NPE
1534 return;
1535 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001536 Folder openFolder = mWorkspace.getOpenFolder();
1537 // In all these cases, only animate if we're already on home
1538 mWorkspace.exitWidgetResizeMode();
1539 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1540 openFolder == null) {
1541 mWorkspace.moveToDefaultScreen(true);
1542 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001543
Jamie Genniscb222e82012-10-23 15:44:26 -07001544 closeFolder();
1545 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001546
Jamie Genniscb222e82012-10-23 15:44:26 -07001547 // If we are already on home, then just animate back to the workspace,
1548 // otherwise, just wait until onResume to set the state back to Workspace
1549 if (alreadyOnHome) {
1550 showWorkspace(true);
1551 } else {
1552 mOnResumeState = State.WORKSPACE;
1553 }
1554
1555 final View v = getWindow().peekDecorView();
1556 if (v != null && v.getWindowToken() != null) {
1557 InputMethodManager imm = (InputMethodManager)getSystemService(
1558 INPUT_METHOD_SERVICE);
1559 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1560 }
1561
1562 // Reset AllApps to its initial state
1563 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1564 mAppsCustomizeTabHost.reset();
1565 }
1566 }
1567 };
1568
1569 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1570 // Delay processing of the intent to allow the status bar animation to finish
1571 // first in order to avoid janky animations.
1572 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001573 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001574 // Process the intent immediately.
1575 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001576 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 }
Michael Jurka447bf842013-05-15 14:52:15 +02001579 if (DEBUG_RESUME_TIME) {
1580 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1581 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
1583
1584 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001585 public void onRestoreInstanceState(Bundle state) {
1586 super.onRestoreInstanceState(state);
1587 for (int page: mSynchronouslyBoundPages) {
1588 mWorkspace.restoreInstanceStateForChild(page);
1589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
1591
1592 @Override
1593 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001594 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001595 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596
Michael Jurka883f55b2010-10-21 15:47:14 -07001597 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001598 // We close any open folder since it will not be re-opened, and we need to make sure
1599 // this state is reflected.
1600 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601
Adam Cohendcd297f2013-06-18 13:13:40 -07001602 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001603 mWaitingForResult) {
1604 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001605 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001606 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1607 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001608 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1609 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1610 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 }
1612
1613 if (mFolderInfo != null && mWaitingForResult) {
1614 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1615 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1616 }
Michael Jurka946ad472010-07-09 18:05:18 -07001617
Winson Chung785d2eb2011-04-14 16:08:02 -07001618 // Save the current AppsCustomize tab
1619 if (mAppsCustomizeTabHost != null) {
1620 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1621 if (currentTabTag != null) {
1622 outState.putString("apps_customize_currentTab", currentTabTag);
1623 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001624 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1625 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 }
1628
1629 @Override
1630 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001632
Winson Chunge7a03942011-08-05 15:05:12 -07001633 // Remove all pending runnables
1634 mHandler.removeMessages(ADVANCE_MSG);
1635 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001636 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001637
Winson Chungcd2b0142011-06-08 16:02:26 -07001638 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001639 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001640 mModel.stopLoader();
1641 app.setLauncher(null);
1642
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001644 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001646 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001648 mAppWidgetHost = null;
1649
1650 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651
1652 TextKeyListener.getInstance().release();
1653
Winson Chung81b52252012-08-27 15:34:29 -07001654 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1655 // to prevent leaking Launcher activities on orientation change.
1656 if (mModel != null) {
1657 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1658 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001659
1660 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001661 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001662
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001663 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001664 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1665 mWorkspace.removeAllViews();
1666 mWorkspace = null;
1667 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001668
Michael Jurka2ecf9952012-06-18 12:52:28 -07001669 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 }
1671
Adam Cohena9cf38f2011-05-02 15:36:58 -07001672 public DragController getDragController() {
1673 return mDragController;
1674 }
1675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 @Override
1677 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001678 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 super.startActivityForResult(intent, requestCode);
1680 }
1681
Winson Chung70d72102011-08-12 11:24:35 -07001682 /**
1683 * Indicates that we want global search for this activity by setting the globalSearch
1684 * argument for {@link #startSearch} to true.
1685 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001687 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001689
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001690 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001691
Karl Rosaen138a0412009-04-23 19:00:21 -07001692 if (initialQuery == null) {
1693 // Use any text typed in the launcher as the initial query
1694 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 if (appSearchData == null) {
1697 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001698 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 }
Winson Chungadf0c182012-08-23 14:59:07 -07001700 Rect sourceBounds = new Rect();
1701 if (mSearchDropTargetBar != null) {
1702 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1703 }
Romain Guycbb89e42009-06-08 15:52:54 -07001704
Bjorn Bringertc459e522013-06-07 19:36:01 +01001705 startSearch(initialQuery, selectInitialQuery,
1706 appSearchData, sourceBounds);
1707 }
1708
1709 public void startSearch(String initialQuery,
1710 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001711 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001712 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001713 }
1714
1715 /**
1716 * Starts the global search activity. This code is a copied from SearchManager
1717 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001718 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001719 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001720 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001721 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1722 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1723 if (globalSearchActivity == null) {
1724 Log.w(TAG, "No global search activity found.");
1725 return;
1726 }
1727 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1728 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1729 intent.setComponent(globalSearchActivity);
1730 // Make sure that we have a Bundle to put source in
1731 if (appSearchData == null) {
1732 appSearchData = new Bundle();
1733 } else {
1734 appSearchData = new Bundle(appSearchData);
1735 }
1736 // Set source to package name of app that starts global search, if not set already.
1737 if (!appSearchData.containsKey("source")) {
1738 appSearchData.putString("source", getPackageName());
1739 }
1740 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1741 if (!TextUtils.isEmpty(initialQuery)) {
1742 intent.putExtra(SearchManager.QUERY, initialQuery);
1743 }
1744 if (selectInitialQuery) {
1745 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1746 }
1747 intent.setSourceBounds(sourceBounds);
1748 try {
1749 startActivity(intent);
1750 } catch (ActivityNotFoundException ex) {
1751 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1752 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 }
1754
Winson Chung70d72102011-08-12 11:24:35 -07001755 @Override
1756 public boolean onCreateOptionsMenu(Menu menu) {
1757 if (isWorkspaceLocked()) {
1758 return false;
1759 }
1760
1761 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001762
1763 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1764 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1765 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001766 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1767 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1768 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001769 String helpUrl = getString(R.string.help_url);
1770 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001771 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1772 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1773
Winson Chung70d72102011-08-12 11:24:35 -07001774 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1775 .setIcon(android.R.drawable.ic_menu_gallery)
1776 .setAlphabeticShortcut('W');
1777 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1778 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001779 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001780 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001781 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1782 .setIcon(android.R.drawable.ic_menu_preferences)
1783 .setIntent(settings)
1784 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001785 if (!helpUrl.isEmpty()) {
1786 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1787 .setIcon(android.R.drawable.ic_menu_help)
1788 .setIntent(help)
1789 .setAlphabeticShortcut('H');
1790 }
Winson Chung70d72102011-08-12 11:24:35 -07001791 return true;
1792 }
1793
1794 @Override
1795 public boolean onPrepareOptionsMenu(Menu menu) {
1796 super.onPrepareOptionsMenu(menu);
1797
1798 if (mAppsCustomizeTabHost.isTransitioning()) {
1799 return false;
1800 }
1801 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1802 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1803
1804 return true;
1805 }
1806
1807 @Override
1808 public boolean onOptionsItemSelected(MenuItem item) {
1809 switch (item.getItemId()) {
1810 case MENU_WALLPAPER_SETTINGS:
1811 startWallpaper();
1812 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001813 }
1814
1815 return super.onOptionsItemSelected(item);
1816 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001818 @Override
1819 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001820 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001821 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001822 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001823 }
1824
Joe Onorato9c1289c2009-08-17 11:03:03 -04001825 public boolean isWorkspaceLocked() {
1826 return mWorkspaceLoading || mWaitingForResult;
1827 }
1828
Michael Jurka0280c3b2010-09-17 15:00:07 -07001829 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001830 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001831 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001832 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1833 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001834 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001835 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001836 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001837
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001838 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1839 AppWidgetProviderInfo appWidgetInfo) {
1840 if (appWidgetInfo.configure != null) {
1841 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001842
Bjorn Bringert7984c942009-12-09 15:38:25 +00001843 // Launch over to configure widget, if needed
1844 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001845 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001846 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001847 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001849 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001850 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001851 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001852 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001853 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 }
1855 }
Romain Guycbb89e42009-06-08 15:52:54 -07001856
Adam Cohenfbba09b2011-07-18 21:43:05 -07001857 /**
1858 * Process a shortcut drop.
1859 *
1860 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001861 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001862 * @param cell The cell it should be added to, optional
1863 * @param position The location on the screen where it was dropped, optional
1864 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001865 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001866 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001867 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001868 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001869 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001870 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001871
1872 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001873 mPendingAddInfo.cellX = cell[0];
1874 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001875 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001876
1877 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1878 createShortcutIntent.setComponent(componentName);
1879 processShortcut(createShortcutIntent);
1880 }
1881
Adam Cohenfbba09b2011-07-18 21:43:05 -07001882 /**
1883 * Process a widget drop.
1884 *
1885 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001886 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001887 * @param cell The cell it should be added to, optional
1888 * @param position The location on the screen where it was dropped, optional
1889 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001890 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001891 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001892 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001893 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001894 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001895 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001896 mPendingAddInfo.minSpanX = info.minSpanX;
1897 mPendingAddInfo.minSpanY = info.minSpanY;
1898
Adam Cohenfbba09b2011-07-18 21:43:05 -07001899 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001900 mPendingAddInfo.cellX = cell[0];
1901 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001902 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001903 if (span != null) {
1904 mPendingAddInfo.spanX = span[0];
1905 mPendingAddInfo.spanY = span[1];
1906 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001907
Adam Cohened66b2b2012-01-23 17:28:51 -08001908 AppWidgetHostView hostView = info.boundWidget;
1909 int appWidgetId;
1910 if (hostView != null) {
1911 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001912 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001913 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001914 // In this case, we either need to start an activity to get permission to bind
1915 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001916 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001917 Bundle options = info.bindOptions;
1918
1919 boolean success = false;
1920 if (options != null) {
1921 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1922 info.componentName, options);
1923 } else {
1924 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1925 info.componentName);
1926 }
1927 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001928 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001929 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001930 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001931 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1932 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1933 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001934 // TODO: we need to make sure that this accounts for the options bundle.
1935 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001936 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1937 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001938 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001939 }
1940
Joe Onoratodeb98af2010-02-19 14:59:39 -08001941 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001942 // Handle case where user selected "Applications"
1943 String applicationName = getResources().getString(R.string.group_applications);
1944 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001945
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001946 if (applicationName != null && applicationName.equals(shortcutName)) {
1947 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1948 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001949
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001950 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1951 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001952 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001953 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001954 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001955 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
1958
Winson Chung24ab2f12010-09-16 14:10:47 -07001959 void processWallpaper(Intent intent) {
1960 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1961 }
1962
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001964 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001965 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 folderInfo.title = getText(R.string.folder_name);
1967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001969 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001970 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001971 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972
1973 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001974 FolderIcon newFolder =
1975 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001977 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001978 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 }
Romain Guycbb89e42009-06-08 15:52:54 -07001980
Joe Onorato9c1289c2009-08-17 11:03:03 -04001981 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001982 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001983 }
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001986 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001988 Intent chooser = Intent.createChooser(pickWallpaper,
1989 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001990 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1991 // Removed in Eclair MR1
1992// WallpaperManager wm = (WallpaperManager)
1993// getSystemService(Context.WALLPAPER_SERVICE);
1994// WallpaperInfo wi = wm.getWallpaperInfo();
1995// if (wi != null && wi.getSettingsActivity() != null) {
1996// LabeledIntent li = new LabeledIntent(getPackageName(),
1997// R.string.configure_wallpaper, 0);
1998// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1999// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
2000// }
Mike Clerona0618e42009-10-22 13:55:21 -07002001 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 }
2003
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002004 /**
2005 * Registers various content observers. The current implementation registers
2006 * only a favorites observer to keep track of the favorites applications.
2007 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002008 private void registerContentObservers() {
2009 ContentResolver resolver = getContentResolver();
2010 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2011 true, mWidgetObserver);
2012 }
2013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 @Override
2015 public boolean dispatchKeyEvent(KeyEvent event) {
2016 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2017 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002019 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002020 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002021 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002022 dumpState();
2023 return true;
2024 }
2025 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002026 }
2027 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2028 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002029 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 return true;
2031 }
2032 }
2033
2034 return super.dispatchKeyEvent(event);
2035 }
2036
Joe Onorato88ec0992009-11-19 13:16:06 -08002037 @Override
2038 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002039 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002040 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002041 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002042 Folder openFolder = mWorkspace.getOpenFolder();
2043 if (openFolder.isEditingName()) {
2044 openFolder.dismissEditingName();
2045 } else {
2046 closeFolder();
2047 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002048 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002049 mWorkspace.exitWidgetResizeMode();
2050
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002051 // Back button is a no-op here, but give at least some feedback for the button press
2052 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002053 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002054 }
2055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002057 * Re-listen when widgets are reset.
2058 */
2059 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002060 if (mAppWidgetHost != null) {
2061 mAppWidgetHost.startListening();
2062 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002063 }
2064
2065 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 * Launches the intent referred by the clicked shortcut.
2067 *
2068 * @param v The view representing the clicked shortcut.
2069 */
2070 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002071 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2072 // view has detached (it's possible for this to happen if the view is removed mid touch).
2073 if (v.getWindowToken() == null) {
2074 return;
2075 }
2076
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002077 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002078 return;
2079 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002082 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002084 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002085
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002086 // Check for special shortcuts
2087 if (intent.getComponent() != null) {
2088 final String shortcutClass = intent.getComponent().getClassName();
2089
2090 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2091 showAllApps(true);
2092 return;
2093 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2094 MemoryDumpActivity.startDump(this);
2095 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002096 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2097 toggleShowWeightWatcher();
2098 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002099 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002100 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002101
2102 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002103 int[] pos = new int[2];
2104 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002105 intent.setSourceBounds(new Rect(pos[0], pos[1],
2106 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002107
2108 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002109
2110 if (success && v instanceof BubbleTextView) {
2111 mWaitingForResume = (BubbleTextView) v;
2112 mWaitingForResume.setStayPressed(true);
2113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002115 if (v instanceof FolderIcon) {
2116 FolderIcon fi = (FolderIcon) v;
2117 handleFolderClick(fi);
2118 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002119 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002120 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002121 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002122 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002123 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 }
2126 }
2127
Michael Jurka0e260592010-06-30 17:07:39 -07002128 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002129 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002130 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002131 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2132 showWorkspace(true);
2133 }
Michael Jurka0e260592010-06-30 17:07:39 -07002134 return false;
2135 }
2136
Michael Jurkaaf442092010-06-10 17:01:57 -07002137 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002138 * Event handler for the search button
2139 *
2140 * @param v The view that was clicked.
2141 */
2142 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002143 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2144
Amith Yamasania135ba82011-08-09 17:42:01 -07002145 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002146 }
2147
2148 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002149 * Event handler for the voice button
2150 *
2151 * @param v The view that was clicked.
2152 */
2153 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002154 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002155
Bjorn Bringertc459e522013-06-07 19:36:01 +01002156 startVoice();
2157 }
2158
2159 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002160 try {
2161 final SearchManager searchManager =
2162 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2163 ComponentName activityName = searchManager.getGlobalSearchActivity();
2164 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002165 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002166 if (activityName != null) {
2167 intent.setPackage(activityName.getPackageName());
2168 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002169 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002170 } catch (ActivityNotFoundException e) {
2171 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002172 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002173 startActivitySafely(null, intent, "onClickVoiceButton");
2174 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002175 }
2176
2177 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002178 * Event handler for the "grid" button that appears on the home screen, which
2179 * enters all apps mode.
2180 *
2181 * @param v The view that was clicked.
2182 */
2183 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002184 showAllApps(true);
2185 }
2186
2187 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002188 // Provide the same haptic feedback that the system offers for virtual keys.
2189 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002190 }
2191
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002192 public void onClickAppMarketButton(View v) {
2193 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002194 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002195 } else {
2196 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002197 }
2198 }
2199
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002200 void startApplicationDetailsActivity(ComponentName componentName) {
2201 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002202 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2203 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002204 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002205 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002206 }
2207
Michael Jurka1e2f4652013-07-08 18:03:46 -07002208 // returns true if the activity was started
2209 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2210 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002211 // System applications cannot be installed. For now, show a toast explaining that.
2212 // We may give them the option of disabling apps this way.
2213 int messageId = R.string.uninstall_system_app_text;
2214 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002215 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002216 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002217 String packageName = componentName.getPackageName();
2218 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002219 Intent intent = new Intent(
2220 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002221 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2222 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002223 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002224 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002225 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002226 }
2227
Michael Jurka86a720e2012-05-09 11:23:23 -07002228 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002232 // Only launch using the new animation if the shortcut has not opted out (this is a
2233 // private contract between launcher and may be ignored in the future).
2234 boolean useLaunchAnimation = (v != null) &&
2235 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2236 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002237 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2238 v.getMeasuredWidth(), v.getMeasuredHeight());
2239
2240 startActivity(intent, opts.toBundle());
2241 } else {
2242 startActivity(intent);
2243 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002244 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 } catch (SecurityException e) {
2246 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002247 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002249 "or use the exported attribute for this activity. "
2250 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002252 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002254
Michael Jurka86a720e2012-05-09 11:23:23 -07002255 boolean startActivitySafely(View v, Intent intent, Object tag) {
2256 boolean success = false;
2257 try {
2258 success = startActivity(v, intent, tag);
2259 } catch (ActivityNotFoundException e) {
2260 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2261 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2262 }
2263 return success;
2264 }
2265
Romain Guy8e633c52010-03-04 12:51:36 -08002266 void startActivityForResultSafely(Intent intent, int requestCode) {
2267 try {
2268 startActivityForResult(intent, requestCode);
2269 } catch (ActivityNotFoundException e) {
2270 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2271 } catch (SecurityException e) {
2272 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2273 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2274 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2275 "or use the exported attribute for this activity.", e);
2276 }
2277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278
Adam Cohena9cf38f2011-05-02 15:36:58 -07002279 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002280 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002281 Folder openFolder = mWorkspace.getFolderForTag(info);
2282
2283 // If the folder info reports that the associated folder is open, then verify that
2284 // it is actually opened. There have been a few instances where this gets out of sync.
2285 if (info.opened && openFolder == null) {
2286 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002287 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002288 info.opened = false;
2289 }
2290
Adam Cohenfb91f302012-06-11 15:45:18 -07002291 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 // Close any open folder
2293 closeFolder();
2294 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002295 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002296 } else {
2297 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298 int folderScreen;
2299 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002300 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 // .. and close it
2302 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002303 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 // Close any folder open on the current screen
2305 closeFolder();
2306 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002307 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 }
2309 }
2310 }
2311 }
2312
Adam Cohen268c4752012-06-06 17:47:33 -07002313 /**
2314 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2315 * in the DragLayer in the exact absolute location of the original FolderIcon.
2316 */
2317 private void copyFolderIconToImage(FolderIcon fi) {
2318 final int width = fi.getMeasuredWidth();
2319 final int height = fi.getMeasuredHeight();
2320
2321 // Lazy load ImageView, Bitmap and Canvas
2322 if (mFolderIconImageView == null) {
2323 mFolderIconImageView = new ImageView(this);
2324 }
2325 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2326 mFolderIconBitmap.getHeight() != height) {
2327 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2328 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2329 }
2330
2331 DragLayer.LayoutParams lp;
2332 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2333 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2334 } else {
2335 lp = new DragLayer.LayoutParams(width, height);
2336 }
2337
Adam Cohen307fe232012-08-16 17:55:58 -07002338 // The layout from which the folder is being opened may be scaled, adjust the starting
2339 // view size by this scale factor.
2340 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002341 lp.customPosition = true;
2342 lp.x = mRectForFolderAnimation.left;
2343 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002344 lp.width = (int) (scale * width);
2345 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002346
2347 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2348 fi.draw(mFolderIconCanvas);
2349 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002350 if (fi.getFolder() != null) {
2351 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2352 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002353 }
Adam Cohen268c4752012-06-06 17:47:33 -07002354 // Just in case this image view is still in the drag layer from a previous animation,
2355 // we remove it and re-add it.
2356 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2357 mDragLayer.removeView(mFolderIconImageView);
2358 }
2359 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002360 if (fi.getFolder() != null) {
2361 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002362 }
Adam Cohen268c4752012-06-06 17:47:33 -07002363 }
2364
Adam Cohen2801caf2011-05-13 20:57:39 -07002365 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002366 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002367 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2368 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2369 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2370
Adam Cohenc51934b2011-07-26 21:07:43 -07002371 FolderInfo info = (FolderInfo) fi.getTag();
2372 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2373 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002374 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2375 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002376 }
2377
Adam Cohen268c4752012-06-06 17:47:33 -07002378 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2379 copyFolderIconToImage(fi);
2380 fi.setVisibility(View.INVISIBLE);
2381
Michael Jurka2ecf9952012-06-18 12:52:28 -07002382 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002383 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002384 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2385 oa.start();
2386 }
2387
Adam Cohen268c4752012-06-06 17:47:33 -07002388 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002389 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002390 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2391 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2392 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2393
Adam Cohen268c4752012-06-06 17:47:33 -07002394 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002395
Adam Cohen268c4752012-06-06 17:47:33 -07002396 // We remove and re-draw the FolderIcon in-case it has changed
2397 mDragLayer.removeView(mFolderIconImageView);
2398 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002399 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002400 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002401 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002402 oa.addListener(new AnimatorListenerAdapter() {
2403 @Override
2404 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002405 if (cl != null) {
2406 cl.clearFolderLeaveBehind();
2407 // Remove the ImageView copy of the FolderIcon and make the original visible.
2408 mDragLayer.removeView(mFolderIconImageView);
2409 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002410 }
2411 }
2412 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002413 oa.start();
2414 }
2415
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002417 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 * is animated relative to the specified View. If the View is null, no animation
2419 * is played.
2420 *
2421 * @param folderInfo The FolderInfo describing the folder to open.
2422 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002423 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002424 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002425 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426
Adam Cohena9cf38f2011-05-02 15:36:58 -07002427 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428
Adam Cohen4554ee12011-08-03 16:13:21 -07002429 // Just verify that the folder hasn't already been added to the DragLayer.
2430 // There was a one-off crash where the folder had a parent already.
2431 if (folder.getParent() == null) {
2432 mDragLayer.addView(folder);
2433 mDragController.addDropTarget((DropTarget) folder);
2434 } else {
2435 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2436 folder.getParent() + ").");
2437 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002438 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002439 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002440
2441 // Notify the accessibility manager that this folder "window" has appeared and occluded
2442 // the workspace items
2443 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2444 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002445 }
2446
2447 public void closeFolder() {
2448 Folder folder = mWorkspace.getOpenFolder();
2449 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002450 if (folder.isEditingName()) {
2451 folder.dismissEditingName();
2452 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002453 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002454
2455 // Dismiss the folder cling
2456 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002457 }
2458 }
2459
2460 void closeFolder(Folder folder) {
2461 folder.getInfo().opened = false;
2462
2463 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2464 if (parent != null) {
2465 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2466 shrinkAndFadeInFolderIcon(fi);
2467 }
2468 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002469
2470 // Notify the accessibility manager that this folder "window" has disappeard and no
2471 // longer occludeds the workspace items
2472 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 }
2474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002476 if (!isDraggingEnabled()) return false;
2477 if (isWorkspaceLocked()) return false;
2478 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479
2480 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002481 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 }
2483
Michael Jurka0280c3b2010-09-17 15:00:07 -07002484 resetAddInfo();
2485 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002487 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 return true;
2489 }
2490
Winson Chung3d503fb2011-07-13 17:25:49 -07002491 // The hotseat touch handling does not go through Workspace, and we always allow long press
2492 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002493 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002494 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2495 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002496 if (itemUnderLongClick == null) {
2497 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002498 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2499 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002500 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002501 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2502 mWorkspace.startReordering();
2503 } else {
2504 startWallpaper();
2505 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002507 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002509 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 }
2511 }
2512 }
2513 return true;
2514 }
2515
Winson Chung3d503fb2011-07-13 17:25:49 -07002516 boolean isHotseatLayout(View layout) {
2517 return mHotseat != null && layout != null &&
2518 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2519 }
2520 Hotseat getHotseat() {
2521 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002522 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002523 SearchDropTargetBar getSearchBar() {
2524 return mSearchDropTargetBar;
2525 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002526
Winson Chung3d503fb2011-07-13 17:25:49 -07002527 /**
2528 * Returns the CellLayout of the specified container at the specified screen.
2529 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002530 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002531 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2532 if (mHotseat != null) {
2533 return mHotseat.getLayout();
2534 } else {
2535 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002536 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002537 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002538 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002539 }
2540 }
2541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 Workspace getWorkspace() {
2543 return mWorkspace;
2544 }
2545
Daniel Sandler843e8602010-06-07 14:59:01 -04002546 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002547 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002548 }
2549
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002550 /**
2551 * Helper method for the cameraZoomIn/cameraZoomOut animations
2552 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002553 * @param scaleFactor The scale factor used for the zoom
2554 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002555 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002556 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002557 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002558 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002559
Winson Chung18f41f82012-05-09 13:28:10 -07002560 void disableWallpaperIfInAllApps() {
2561 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002562 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002563 if (mAppsCustomizeTabHost != null &&
2564 !mAppsCustomizeTabHost.isTransitioning()) {
2565 updateWallpaperVisibility(false);
2566 }
2567 }
2568 }
2569
Craig Mautner360310b2012-10-26 15:13:08 -07002570 private void setWorkspaceBackground(boolean workspace) {
2571 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002572 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002573 }
2574
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002575 void updateWallpaperVisibility(boolean visible) {
2576 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2577 int curflags = getWindow().getAttributes().flags
2578 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2579 if (wpflags != curflags) {
2580 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2581 }
Craig Mautner360310b2012-10-26 15:13:08 -07002582 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002583 }
2584
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002585 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2586 if (v instanceof LauncherTransitionable) {
2587 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2588 }
2589 }
2590
Michael Jurkabed61d22012-02-14 22:51:29 -08002591 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2592 if (v instanceof LauncherTransitionable) {
2593 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2594 }
Winson Chung70442722012-02-10 15:43:22 -08002595
2596 // Update the workspace transition step as well
2597 dispatchOnLauncherTransitionStep(v, 0f);
2598 }
2599
2600 private void dispatchOnLauncherTransitionStep(View v, float t) {
2601 if (v instanceof LauncherTransitionable) {
2602 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2603 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002604 }
2605
2606 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2607 if (v instanceof LauncherTransitionable) {
2608 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2609 }
Winson Chung70442722012-02-10 15:43:22 -08002610
2611 // Update the workspace transition step as well
2612 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002613 }
2614
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002615 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002616 * Things to test when changing the following seven functions.
2617 * - Home from workspace
2618 * - from center screen
2619 * - from other screens
2620 * - Home from all apps
2621 * - from center screen
2622 * - from other screens
2623 * - Back from all apps
2624 * - from center screen
2625 * - from other screens
2626 * - Launch app from workspace and quit
2627 * - with back
2628 * - with home
2629 * - Launch app from all apps and quit
2630 * - with back
2631 * - with home
2632 * - Go to a screen that's not the default, then all
2633 * apps, and launch and app, and go back
2634 * - with back
2635 * -with home
2636 * - On workspace, long press power and go back
2637 * - with back
2638 * - with home
2639 * - On all apps, long press power and go back
2640 * - with back
2641 * - with home
2642 * - On workspace, power off
2643 * - On all apps, power off
2644 * - Launch an app and turn off the screen while in that app
2645 * - Go back with home key
2646 * - Go back with back key TODO: make this not go to workspace
2647 * - From all apps
2648 * - From workspace
2649 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2650 * - From all apps
2651 * - From the center workspace
2652 * - From another workspace
2653 */
2654
2655 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002656 * Zoom the camera out from the workspace to reveal 'toView'.
2657 * Assumes that the view to show is anchored at either the very top or very bottom
2658 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002659 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002660 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002661 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002662 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002663 mStateAnimation.cancel();
2664 mStateAnimation = null;
2665 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002666 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002667
Winson Chungf0ea4d32011-06-06 14:27:16 -07002668 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2669 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2670 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002671 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002672 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002673 final int startDelay =
2674 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002675
Michael Jurkab3e22d92011-10-31 15:58:33 -07002676 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002677
Michael Jurkad74c9842011-07-10 12:44:21 -07002678 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002679 Animator workspaceAnim =
2680 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681
2682 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002683 toView.setScaleX(scale);
2684 toView.setScaleY(scale);
2685 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2686 scaleAnim.
2687 scaleX(1f).scaleY(1f).
2688 setDuration(duration).
2689 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002690
Winson Chungf0ea4d32011-06-06 14:27:16 -07002691 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002692 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002693 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002694 .ofFloat(toView, "alpha", 0f, 1f)
2695 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002696 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002697 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2698 @Override
2699 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002700 if (animation == null) {
2701 throw new RuntimeException("animation is null");
2702 }
Winson Chung70442722012-02-10 15:43:22 -08002703 float t = (Float) animation.getAnimatedValue();
2704 dispatchOnLauncherTransitionStep(fromView, t);
2705 dispatchOnLauncherTransitionStep(toView, t);
2706 }
2707 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002708
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002709 // toView should appear right at the end of the workspace shrink
2710 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002711 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002712 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002713 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002714
2715 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002716 boolean animationCancelled = false;
2717
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002718 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002719 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002720 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721 // Prepare the position
2722 toView.setTranslationX(0.0f);
2723 toView.setTranslationY(0.0f);
2724 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002725 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002726 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002727 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002728 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002729 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2730 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002731
Daniel Sandlere4f98912013-06-25 15:13:26 -04002732 if (mWorkspace != null
2733 && !springLoaded
2734 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002735 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002736 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002737 if (!animationCancelled) {
2738 updateWallpaperVisibility(false);
2739 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002740
2741 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002742 if (mSearchDropTargetBar != null) {
2743 mSearchDropTargetBar.hideSearchBar(false);
2744 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002745 }
2746
Adam Cohencff6af82011-09-13 14:51:53 -07002747 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002748 public void onAnimationCancel(Animator animation) {
2749 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002750 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002751 });
2752
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002753 if (workspaceAnim != null) {
2754 mStateAnimation.play(workspaceAnim);
2755 }
Michael Jurka1899a362011-11-03 13:50:45 -07002756
2757 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002758
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002759 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2760 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002761
2762 // If any of the objects being animated haven't been measured/laid out
2763 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002764 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002765 (mWorkspace.getMeasuredWidth() == 0) ||
2766 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002767 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002768 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002769
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002770 final AnimatorSet stateAnimation = mStateAnimation;
2771 final Runnable startAnimRunnable = new Runnable() {
2772 public void run() {
2773 // Check that mStateAnimation hasn't changed while
2774 // we waited for a layout/draw pass
2775 if (mStateAnimation != stateAnimation)
2776 return;
2777 setPivotsForZoom(toView, scale);
2778 dispatchOnLauncherTransitionStart(fromView, animated, false);
2779 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002780 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002781 }
2782 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002783 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002784 final ViewTreeObserver observer = toView.getViewTreeObserver();
2785 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2786 public void onGlobalLayout() {
2787 startAnimRunnable.run();
2788 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2789 }
2790 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002791 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002792 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002793 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002794 } else {
2795 toView.setTranslationX(0.0f);
2796 toView.setTranslationY(0.0f);
2797 toView.setScaleX(1.0f);
2798 toView.setScaleY(1.0f);
2799 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002800 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002801
Daniel Sandlere4f98912013-06-25 15:13:26 -04002802 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002803 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002804
2805 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002806 if (mSearchDropTargetBar != null) {
2807 mSearchDropTargetBar.hideSearchBar(false);
2808 }
Michael Jurkaabded662011-03-04 12:06:57 -08002809 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002810 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002811 dispatchOnLauncherTransitionStart(fromView, animated, false);
2812 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002813 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002814 dispatchOnLauncherTransitionStart(toView, animated, false);
2815 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002816 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002817 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002818 }
2819
2820 /**
2821 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002822 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002823 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002824 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002825 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2826 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002827
Michael Jurkab3e22d92011-10-31 15:58:33 -07002828 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002829 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002830 mStateAnimation.cancel();
2831 mStateAnimation = null;
2832 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002833 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002834
Winson Chungf0ea4d32011-06-06 14:27:16 -07002835 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002836 final int fadeOutDuration =
2837 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002838 final float scaleFactor = (float)
2839 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2840 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002841 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002842 Animator workspaceAnim = null;
2843
2844 if (toState == State.WORKSPACE) {
2845 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2846 workspaceAnim = mWorkspace.getChangeStateAnimation(
2847 Workspace.State.NORMAL, animated, stagger);
2848 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2849 workspaceAnim = mWorkspace.getChangeStateAnimation(
2850 Workspace.State.SPRING_LOADED, animated);
2851 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002852
Michael Jurkab3e22d92011-10-31 15:58:33 -07002853 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002854 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002855 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002856 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002857 final LauncherViewPropertyAnimator scaleAnim =
2858 new LauncherViewPropertyAnimator(fromView);
2859 scaleAnim.
2860 scaleX(scaleFactor).scaleY(scaleFactor).
2861 setDuration(duration).
2862 setInterpolator(new Workspace.ZoomInInterpolator());
2863
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002864 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002865 .ofFloat(fromView, "alpha", 1f, 0f)
2866 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002867 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002868 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2869 @Override
2870 public void onAnimationUpdate(ValueAnimator animation) {
2871 float t = 1f - (Float) animation.getAnimatedValue();
2872 dispatchOnLauncherTransitionStep(fromView, t);
2873 dispatchOnLauncherTransitionStep(toView, t);
2874 }
2875 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002876
Michael Jurka2ecf9952012-06-18 12:52:28 -07002877 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002878
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002879 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2880 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002881 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002882
2883 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002884 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002885 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002886 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002887 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002888 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2889 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002890 if (onCompleteRunnable != null) {
2891 onCompleteRunnable.run();
2892 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002893 mAppsCustomizeContent.updateCurrentPageScroll();
2894 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002895 }
2896 });
2897
Winson Chungf0ea4d32011-06-06 14:27:16 -07002898 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002899 if (workspaceAnim != null) {
2900 mStateAnimation.play(workspaceAnim);
2901 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002902 dispatchOnLauncherTransitionStart(fromView, animated, true);
2903 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002904 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002905 } else {
2906 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002907 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002908 dispatchOnLauncherTransitionStart(fromView, animated, true);
2909 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002910 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002911 dispatchOnLauncherTransitionStart(toView, animated, true);
2912 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002913 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002914 }
2915
Michael Jurkae326f182011-11-21 14:05:46 -08002916 @Override
2917 public void onTrimMemory(int level) {
2918 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002919 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002920 mAppsCustomizeTabHost.onTrimMemory();
2921 }
2922 }
2923
Winson Chung18f41f82012-05-09 13:28:10 -07002924 @Override
2925 public void onWindowFocusChanged(boolean hasFocus) {
2926 if (!hasFocus) {
2927 // When another window occludes launcher (like the notification shade, or recents),
2928 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2929 updateWallpaperVisibility(true);
2930 } else {
2931 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002932 mWorkspace.postDelayed(new Runnable() {
2933 @Override
2934 public void run() {
2935 disableWallpaperIfInAllApps();
2936 }
2937 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002938 }
2939 }
2940
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002941 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002942 showWorkspace(animated, null);
2943 }
2944
2945 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002946 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002947 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002948 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002949 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002950
Winson Chungc7d2b602012-05-16 17:02:20 -07002951 // Show the search bar (only animate if we were showing the drop target bar in spring
2952 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002953 if (mSearchDropTargetBar != null) {
2954 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2955 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002956
Michael Jurkab737ee62011-11-15 15:57:22 -08002957 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002958 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002959
2960 // Set focus to the AppsCustomize button
2961 if (mAllAppsButton != null) {
2962 mAllAppsButton.requestFocus();
2963 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002964 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002965
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002966 // Change the state *after* we've called all the transition code
2967 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002968
Winson Chung5fb63472011-02-02 17:03:37 -08002969 // Resume the auto-advance of widgets
2970 mUserPresent = true;
2971 updateRunning();
2972
alanv1d4fde62012-10-17 13:15:47 -07002973 // Send an accessibility event to announce the context change
2974 getWindow().getDecorView()
2975 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002976
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002977 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002978 }
2979
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002980 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002981 }
2982
Michael Jurkab3e22d92011-10-31 15:58:33 -07002983 void showAllApps(boolean animated) {
2984 if (mState != State.WORKSPACE) return;
2985
2986 showAppsCustomizeHelper(animated, false);
2987 mAppsCustomizeTabHost.requestFocus();
2988
Michael Jurkab3e22d92011-10-31 15:58:33 -07002989 // Change the state *after* we've called all the transition code
2990 mState = State.APPS_CUSTOMIZE;
2991
2992 // Pause the auto-advance of widgets until we are out of AllApps
2993 mUserPresent = false;
2994 updateRunning();
2995 closeFolder();
2996
2997 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002998 getWindow().getDecorView()
2999 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003000 }
3001
Adam Cohen7777d962011-08-18 18:58:38 -07003002 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003003 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003004 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08003005 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07003006 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003007 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003008 }
Adam Cohen7777d962011-08-18 18:58:38 -07003009
Adam Cohened66b2b2012-01-23 17:28:51 -08003010 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3011 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003012 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3013
Winson Chunge7a03942011-08-05 15:05:12 -07003014 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003015 @Override
3016 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003017 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003018 // Before we show workspace, hide all apps again because
3019 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3020 // clean up our state transition functions
3021 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003022 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003023 } else {
3024 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003025 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003026 }
3027 }, (extendedDelay ?
3028 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3029 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3030 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003031
Michael Jurkad3ef3062010-11-23 16:23:58 -08003032 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003033 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003034 final boolean animated = true;
3035 final boolean springLoaded = true;
3036 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003037 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003038 }
3039 // Otherwise, we are not in spring loaded mode, so don't do anything.
3040 }
3041
Michael Jurkab737ee62011-11-15 15:57:22 -08003042 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003043 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003044 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003045 }
3046 }
3047
3048 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003049 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003050 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003051 if (mDividerAnimator != null) {
3052 mDividerAnimator.cancel();
3053 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003054 mDividerAnimator = null;
3055 }
3056 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003057 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003058 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003059 int duration = 0;
3060 if (mSearchDropTargetBar != null) {
3061 duration = mSearchDropTargetBar.getTransitionInDuration();
3062 }
3063 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003064 mDividerAnimator.start();
3065 }
3066 }
3067 }
3068
Michael Jurkab3e22d92011-10-31 15:58:33 -07003069 void lockAllApps() {
3070 // TODO
3071 }
3072
3073 void unlockAllApps() {
3074 // TODO
3075 }
3076
Winson Chungf0ea4d32011-06-06 14:27:16 -07003077 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003078 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003079 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003080 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003081 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003082 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003083 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003084 int duration = 0;
3085 if (mSearchDropTargetBar != null) {
3086 duration = mSearchDropTargetBar.getTransitionInDuration();
3087 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003088 mHotseat.animate().alpha(1f).setDuration(duration);
3089 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003090 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003091 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003092 }
3093 }
3094 }
3095
3096 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003097 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003098 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003099 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003100 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003101 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003102 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003103 int duration = 0;
3104 if (mSearchDropTargetBar != null) {
3105 duration = mSearchDropTargetBar.getTransitionOutDuration();
3106 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003107 mHotseat.animate().alpha(0f).setDuration(duration);
3108 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003109 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003110 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003111 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003112 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003113 }
3114
Patrick Dubroy5f445422011-02-18 14:35:21 -08003115 /**
3116 * Add an item from all apps or customize onto the given workspace screen.
3117 * If layout is null, add to the current screen.
3118 */
3119 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003120 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003121 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003122 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003123 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003124
Winson Chungdff8ebb2011-09-08 17:25:31 -07003125 /** Maps the current orientation to an index for referencing orientation correct global icons */
3126 private int getCurrentOrientationIndexForGlobalIcons() {
3127 // default - 0, landscape - 1
3128 switch (getResources().getConfiguration().orientation) {
3129 case Configuration.ORIENTATION_LANDSCAPE:
3130 return 1;
3131 default:
3132 return 0;
3133 }
3134 }
3135
Michael Jurkaae65ee32012-04-30 11:45:54 -07003136 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003137 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003138 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003139 // Look for the toolbar icon specified in the activity meta-data
3140 Bundle metaData = packageManager.getActivityInfo(
3141 activityName, PackageManager.GET_META_DATA).metaData;
3142 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003143 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003144 if (iconResId != 0) {
3145 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003146 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003147 }
3148 }
3149 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003150 // This can happen if the activity defines an invalid drawable
3151 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3152 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003153 } catch (Resources.NotFoundException nfe) {
3154 // This can happen if the activity defines an invalid drawable
3155 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3156 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003157 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003158 return null;
3159 }
3160
3161 // if successful in getting icon, return it; otherwise, set button to use default drawable
3162 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003163 int buttonId, ComponentName activityName, int fallbackDrawableId,
3164 String toolbarResourceName) {
3165 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003166 Resources r = getResources();
3167 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3168 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003169
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003170 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003171 // If we were unable to find the icon via the meta-data, use a generic one
3172 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003173 toolbarIcon = r.getDrawable(fallbackDrawableId);
3174 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003175 if (button != null) {
3176 button.setCompoundDrawables(toolbarIcon, null, null, null);
3177 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003178 return null;
3179 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003180 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003181 if (button != null) {
3182 button.setCompoundDrawables(toolbarIcon, null, null, null);
3183 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003184 return toolbarIcon.getConstantState();
3185 }
3186 }
3187
3188 // if successful in getting icon, return it; otherwise, set button to use default drawable
3189 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003190 int buttonId, ComponentName activityName, int fallbackDrawableId,
3191 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003192 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003193 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003194
Michael Jurka19e0fc52011-07-22 18:00:21 -07003195 if (button != null) {
3196 // If we were unable to find the icon via the meta-data, use a
3197 // generic one
3198 if (toolbarIcon == null) {
3199 button.setImageResource(fallbackDrawableId);
3200 } else {
3201 button.setImageDrawable(toolbarIcon);
3202 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003203 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003204
3205 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3206
Michael Jurka0423dcf2010-10-05 14:56:18 -07003207 }
3208
Winson Chung1b884092012-06-08 17:13:02 -07003209 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003210 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003211 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003212 }
3213
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003214 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003215 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003216 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003217 }
3218
Winson Chungbb185bd2011-11-21 12:31:42 -08003219 private void invalidatePressedFocusedStates(View container, View button) {
3220 if (container instanceof HolographicLinearLayout) {
3221 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3222 layout.invalidatePressedFocusedStates();
3223 } else if (button instanceof HolographicImageView) {
3224 HolographicImageView view = (HolographicImageView) button;
3225 view.invalidatePressedFocusedStates();
3226 }
3227 }
3228
Winson Chungc51db6a2011-10-05 11:44:49 -07003229 private boolean updateGlobalSearchIcon() {
3230 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003231 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003232 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003233 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003234
Winson Chung1cad91e2011-05-25 17:41:01 -07003235 final SearchManager searchManager =
3236 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3237 ComponentName activityName = searchManager.getGlobalSearchActivity();
3238 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003239 int coi = getCurrentOrientationIndexForGlobalIcons();
3240 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003241 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3242 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3243 if (sGlobalSearchIcon[coi] == null) {
3244 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3245 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3246 TOOLBAR_ICON_METADATA_NAME);
3247 }
3248
Winson Chungc51db6a2011-10-05 11:44:49 -07003249 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3250 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003251 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003252 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003253 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003254 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003255 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3256 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3257 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003258 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003259 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003260 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003261 }
3262 }
3263
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003264 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003265 final View searchButtonContainer = findViewById(R.id.search_button_container);
3266 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003267 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003268 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003269 }
3270
Winson Chungc51db6a2011-10-05 11:44:49 -07003271 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003272 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003273 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003274
Winson Chungc51db6a2011-10-05 11:44:49 -07003275 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003276 final SearchManager searchManager =
3277 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3278 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3279
3280 ComponentName activityName = null;
3281 if (globalSearchActivity != null) {
3282 // Check if the global search activity handles voice search
3283 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3284 intent.setPackage(globalSearchActivity.getPackageName());
3285 activityName = intent.resolveActivity(getPackageManager());
3286 }
3287
3288 if (activityName == null) {
3289 // Fallback: check if an activity other than the global search activity
3290 // resolves this
3291 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3292 activityName = intent.resolveActivity(getPackageManager());
3293 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003294 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003295 int coi = getCurrentOrientationIndexForGlobalIcons();
3296 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003297 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3298 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3299 if (sVoiceSearchIcon[coi] == null) {
3300 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3301 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3302 TOOLBAR_ICON_METADATA_NAME);
3303 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003304 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003305 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003306 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003307 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003308 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003309 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003310 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003311 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003312 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003313 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003314 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003315 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003316
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003317 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003318 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3319 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003320 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003321 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003322 }
3323
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003324 public void setVoiceButtonProxyVisible(boolean visible) {
3325 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3326 if (voiceButtonProxy != null) {
3327 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3328 }
3329 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003330 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003331 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003332 */
3333 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003334 final View marketButton = findViewById(R.id.market_button);
3335 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3336 // Find the app market activity by resolving an intent.
3337 // (If multiple app markets are installed, it will return the ResolverActivity.)
3338 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003339 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003340 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003341 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003342 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003343 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3344 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003345 marketButton.setVisibility(View.VISIBLE);
3346 } else {
3347 // We should hide and disable the view so that we don't try and restore the visibility
3348 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3349 marketButton.setVisibility(View.GONE);
3350 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003351 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003352 }
3353
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003354 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003355 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3356 Resources r = getResources();
3357 Drawable marketIconDrawable = d.newDrawable(r);
3358 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3359 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3360 marketIconDrawable.setBounds(0, 0, w, h);
3361
3362 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003363 }
3364
Michael Jurkad7c28052012-04-27 15:43:36 -07003365 @Override
3366 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003367 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003368 final List<CharSequence> text = event.getText();
3369 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003370 // Populate event with a fake title based on the current state.
3371 if (mState == State.APPS_CUSTOMIZE) {
3372 text.add(getString(R.string.all_apps_button_label));
3373 } else {
3374 text.add(getString(R.string.all_apps_home_button_label));
3375 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003376 return result;
3377 }
3378
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003379 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003380 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003381 */
3382 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3383 @Override
3384 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003385 closeSystemDialogs();
3386 }
3387 }
3388
3389 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003390 * Receives notifications whenever the appwidgets are reset.
3391 */
3392 private class AppWidgetResetObserver extends ContentObserver {
3393 public AppWidgetResetObserver() {
3394 super(new Handler());
3395 }
3396
3397 @Override
3398 public void onChange(boolean selfChange) {
3399 onAppWidgetReset();
3400 }
3401 }
3402
3403 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003404 * If the activity is currently paused, signal that we need to run the passed Runnable
3405 * in onResume.
3406 *
3407 * This needs to be called from incoming places where resources might have been loaded
3408 * while we are paused. That is becaues the Configuration might be wrong
3409 * when we're not running, and if it comes back to what it was when we
3410 * were paused, we are not restarted.
3411 *
3412 * Implementation of the method from LauncherModel.Callbacks.
3413 *
3414 * @return true if we are currently paused. The caller might be able to
3415 * skip some work in that case since we will come back again.
3416 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003417 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003418 if (mPaused) {
3419 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003420 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003421 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003422 }
3423 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003424 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003425 return true;
3426 } else {
3427 return false;
3428 }
3429 }
3430
Michael Jurkac402cd92013-05-20 15:49:32 +02003431 private boolean waitUntilResume(Runnable run) {
3432 return waitUntilResume(run, false);
3433 }
3434
Michael Jurka1e2f4652013-07-08 18:03:46 -07003435 public void addOnResumeCallback(Runnable run) {
3436 mBindOnResumeCallbacks.add(run);
3437 }
3438
3439 public void removeOnResumeCallback(Runnable run) {
3440 mBindOnResumeCallbacks.remove(run);
3441 }
3442
Michael Jurka7607c2f2013-04-03 14:33:19 -07003443 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003444 * If the activity is currently paused, signal that we need to re-run the loader
3445 * in onResume.
3446 *
3447 * This needs to be called from incoming places where resources might have been loaded
3448 * while we are paused. That is becaues the Configuration might be wrong
3449 * when we're not running, and if it comes back to what it was when we
3450 * were paused, we are not restarted.
3451 *
3452 * Implementation of the method from LauncherModel.Callbacks.
3453 *
3454 * @return true if we are currently paused. The caller might be able to
3455 * skip some work in that case since we will come back again.
3456 */
3457 public boolean setLoadOnResume() {
3458 if (mPaused) {
3459 Log.i(TAG, "setLoadOnResume");
3460 mOnResumeNeedsLoad = true;
3461 return true;
3462 } else {
3463 return false;
3464 }
3465 }
3466
3467 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003468 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003469 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003470 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003471 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003472 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003473 } else {
3474 return SCREEN_COUNT / 2;
3475 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003477
Joe Onorato9c1289c2009-08-17 11:03:03 -04003478 /**
3479 * Refreshes the shortcuts shown on the workspace.
3480 *
3481 * Implementation of the method from LauncherModel.Callbacks.
3482 */
3483 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003484 // If we're starting binding all over again, clear any bind calls we'd postponed in
3485 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3486 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003487 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003488
Michael Jurka7607c2f2013-04-03 14:33:19 -07003489 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003490 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003491 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003492 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 int count = workspace.getChildCount();
3494 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003495 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003496 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3497 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003499 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003500 if (mHotseat != null) {
3501 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003502 }
3503 }
3504
Adam Cohendcd297f2013-06-18 13:13:40 -07003505 @Override
3506 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003507 bindAddScreens(orderedScreenIds);
3508 mWorkspace.addExtraEmptyScreen();
3509 }
3510
3511 @Override
3512 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003513 int count = orderedScreenIds.size();
3514 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003515 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003516 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003517 }
3518
Adam Cohen39a06042013-07-19 14:30:12 -07003519 private boolean shouldShowWeightWatcher() {
3520 String spKey = LauncherAppState.getSharedPreferencesKey();
3521 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3522 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3523
3524 return show;
3525 }
3526
3527 private void toggleShowWeightWatcher() {
3528 String spKey = LauncherAppState.getSharedPreferencesKey();
3529 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3530 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3531
3532 show = !show;
3533
3534 SharedPreferences.Editor editor = sp.edit();
3535 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3536 editor.commit();
3537
3538 if (mWeightWatcher != null) {
3539 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3540 }
3541 }
3542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003543 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003544 * Bind the items start-end from the list.
3545 *
3546 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003547 */
Winson Chung64359a52013-07-08 17:17:08 -07003548 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3549 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003550 if (waitUntilResume(new Runnable() {
3551 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003552 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003553 }
3554 })) {
3555 return;
3556 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003557
Winson Chungf0c6ae02012-03-21 16:10:31 -07003558 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3559 Set<String> newApps = new HashSet<String>();
3560 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3561
Winson Chung64359a52013-07-08 17:17:08 -07003562 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3563 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003564 Workspace workspace = mWorkspace;
3565 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003566 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003567
3568 // Short circuit if we are loading dock items for a configuration which has no dock
3569 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3570 mHotseat == null) {
3571 continue;
3572 }
3573
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 switch (item.itemType) {
3575 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3576 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003577 ShortcutInfo info = (ShortcutInfo) item;
3578 String uri = info.intent.toUri(0).toString();
3579 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003580
Winson Chung64359a52013-07-08 17:17:08 -07003581 /*
3582 * TODO: FIX collision case
3583 */
Winson Chungce376632013-07-11 16:12:41 -07003584 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3585 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3586 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3587 throw new RuntimeException("OCCUPIED");
3588 }
Winson Chung64359a52013-07-08 17:17:08 -07003589 }
3590
Adam Cohendcd297f2013-06-18 13:13:40 -07003591 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3592 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003593 boolean animateIconUp = false;
3594 synchronized (newApps) {
3595 if (newApps.contains(uri)) {
3596 animateIconUp = newApps.remove(uri);
3597 }
3598 }
Winson Chung64359a52013-07-08 17:17:08 -07003599 if (forceAnimateIcons) {
3600 // Animate all the applications up now
3601 shortcut.setAlpha(0f);
3602 shortcut.setScaleX(0f);
3603 shortcut.setScaleY(0f);
3604 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3605 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003606 // Prepare the view to be animated up
3607 shortcut.setAlpha(0f);
3608 shortcut.setScaleX(0f);
3609 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003610 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003611 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3612 mNewShortcutAnimateViews.add(shortcut);
3613 }
3614 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003616 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003617 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003618 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003619 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003620 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3621 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003622 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003624 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003625
Winson Chung64359a52013-07-08 17:17:08 -07003626 if (forceAnimateIcons) {
3627 // We post the animation slightly delayed to prevent slowdowns when we are loading
3628 // right after we return to launcher.
3629 mWorkspace.postDelayed(new Runnable() {
3630 public void run() {
3631 anim.playTogether(bounceAnims);
3632 anim.start();
3633 }
3634 }, NEW_APPS_ANIMATION_DELAY);
3635 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003636 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003637 }
3638
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639 /**
3640 * Implementation of the method from LauncherModel.Callbacks.
3641 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003642 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3643 if (waitUntilResume(new Runnable() {
3644 public void run() {
3645 bindFolders(folders);
3646 }
3647 })) {
3648 return;
3649 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003650 sFolders.clear();
3651 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003652 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653
3654 /**
3655 * Add the views for a widget to the workspace.
3656 *
3657 * Implementation of the method from LauncherModel.Callbacks.
3658 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003659 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3660 if (waitUntilResume(new Runnable() {
3661 public void run() {
3662 bindAppWidget(item);
3663 }
3664 })) {
3665 return;
3666 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003667
Daniel Sandler843e8602010-06-07 14:59:01 -04003668 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3669 if (DEBUG_WIDGETS) {
3670 Log.d(TAG, "bindAppWidget: " + item);
3671 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003672 final Workspace workspace = mWorkspace;
3673
3674 final int appWidgetId = item.appWidgetId;
3675 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003676 if (DEBUG_WIDGETS) {
3677 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3678 }
3679
Joe Onorato9c1289c2009-08-17 11:03:03 -04003680 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3681
Joe Onorato9c1289c2009-08-17 11:03:03 -04003682 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003683 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003684
Adam Cohendcd297f2013-06-18 13:13:40 -07003685 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003687 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3688
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 workspace.requestLayout();
3690
Daniel Sandler843e8602010-06-07 14:59:01 -04003691 if (DEBUG_WIDGETS) {
3692 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3693 + (SystemClock.uptimeMillis()-start) + "ms");
3694 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 }
3696
Adam Cohen1462de32012-07-24 22:34:36 -07003697 public void onPageBoundSynchronously(int page) {
3698 mSynchronouslyBoundPages.add(page);
3699 }
3700
Joe Onorato9c1289c2009-08-17 11:03:03 -04003701 /**
3702 * Callback saying that there aren't any more items to bind.
3703 *
3704 * Implementation of the method from LauncherModel.Callbacks.
3705 */
Adam Cohene25af792013-06-06 23:08:25 -07003706 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003707 if (waitUntilResume(new Runnable() {
3708 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003709 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003710 }
3711 })) {
3712 return;
3713 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003714 if (mSavedState != null) {
3715 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003716 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003717 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 mSavedState = null;
3719 }
3720
Adam Cohen1462de32012-07-24 22:34:36 -07003721 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003722
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003723 // If we received the result of any pending adds while the loader was running (e.g. the
3724 // widget configuration forced an orientation change), process them now.
3725 for (int i = 0; i < sPendingAddList.size(); i++) {
3726 completeAdd(sPendingAddList.get(i));
3727 }
3728 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729
Winson Chungc51db6a2011-10-05 11:44:49 -07003730 // Update the market app icon as necessary (the other icons will be managed in response to
3731 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003732 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003733
Winson Chungf0c6ae02012-03-21 16:10:31 -07003734 // Animate up any icons as necessary
3735 if (mVisible || mWorkspaceLoading) {
3736 Runnable newAppsRunnable = new Runnable() {
3737 @Override
3738 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003739 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003740 }
3741 };
Winson Chunga2413752012-04-03 14:22:34 -07003742
Adam Cohendcd297f2013-06-18 13:13:40 -07003743 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3744 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003745 if (canRunNewAppsAnimation()) {
3746 // If the user has not interacted recently, then either snap to the new page to show
3747 // the new-apps animation or just run them if they are to appear on the current page
3748 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003749 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003750 } else {
3751 runNewAppsAnimation(false);
3752 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003753 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003754 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003755 // are on another page (or just normally if they are added to the current page)
3756 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003757 }
3758 }
3759
3760 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003761 if (upgradePath) {
3762 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003763 mWorkspace.stripDuplicateApps();
3764 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003765 }
Adam Cohen99894d92013-06-14 11:22:59 -07003766
Adam Cohen66a01fd2013-06-11 12:48:00 -07003767 mWorkspace.post(new Runnable() {
3768 @Override
3769 public void run() {
3770 onFinishBindingItems();
3771 }
3772 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003773 }
3774
Winson Chunga2413752012-04-03 14:22:34 -07003775 private boolean canRunNewAppsAnimation() {
3776 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3777 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3778 }
3779
Winson Chungc9168342013-06-26 14:54:55 -07003780 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3781 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3782 PropertyValuesHolder.ofFloat("alpha", 1f),
3783 PropertyValuesHolder.ofFloat("scaleX", 1f),
3784 PropertyValuesHolder.ofFloat("scaleY", 1f));
3785 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3786 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3787 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3788 return bounceAnim;
3789 }
3790
Winson Chungf0c6ae02012-03-21 16:10:31 -07003791 /**
3792 * Runs a new animation that scales up icons that were added while Launcher was in the
3793 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003794 *
3795 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003796 */
Winson Chunga2413752012-04-03 14:22:34 -07003797 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003798 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003799 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003800
3801 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003802 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3803 @Override
3804 public int compare(View a, View b) {
3805 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3806 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3807 int cellCountX = LauncherModel.getCellCountX();
3808 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3809 }
3810 });
Winson Chunga2413752012-04-03 14:22:34 -07003811
3812 // Animate each of the views in place (or show them immediately if requested)
3813 if (immediate) {
3814 for (View v : mNewShortcutAnimateViews) {
3815 v.setAlpha(1f);
3816 v.setScaleX(1f);
3817 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003818 }
Winson Chunga2413752012-04-03 14:22:34 -07003819 } else {
3820 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3821 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003822 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003823 }
3824 anim.playTogether(bounceAnims);
3825 anim.addListener(new AnimatorListenerAdapter() {
3826 @Override
3827 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003828 if (mWorkspace != null) {
3829 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3830 }
Winson Chunga2413752012-04-03 14:22:34 -07003831 }
3832 });
3833 anim.start();
3834 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003835
3836 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003837 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003838 mNewShortcutAnimateViews.clear();
3839 new Thread("clearNewAppsThread") {
3840 public void run() {
3841 mSharedPrefs.edit()
3842 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3843 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3844 .commit();
3845 }
3846 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003847 }
3848
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003849 @Override
3850 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003851 boolean searchVisible = updateGlobalSearchIcon();
3852 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003853 if (mSearchDropTargetBar != null) {
3854 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3855 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003856 }
3857
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003858 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003859 * Add the icons for all apps.
3860 *
3861 * Implementation of the method from LauncherModel.Callbacks.
3862 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003863 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003864 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3865 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003866 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003867 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 }
3870
3871 /**
3872 * A package was updated.
3873 *
3874 * Implementation of the method from LauncherModel.Callbacks.
3875 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003876 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3877 if (waitUntilResume(new Runnable() {
3878 public void run() {
3879 bindAppsUpdated(apps);
3880 }
3881 })) {
3882 return;
3883 }
3884
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003885 if (mWorkspace != null) {
3886 mWorkspace.updateShortcuts(apps);
3887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003888 }
3889
3890 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003891 * A package was uninstalled. We take both the super set of packageNames
3892 * in addition to specific applications to remove, the reason being that
3893 * this can be called when a package is updated as well. In that scenario,
3894 * we only remove specific components from the workspace, where as
3895 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896 *
3897 * Implementation of the method from LauncherModel.Callbacks.
3898 */
Winson Chung83892cc2013-05-01 16:53:33 -07003899 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3900 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003901 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003902 if (waitUntilResume(new Runnable() {
3903 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003904 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003905 }
3906 })) {
3907 return;
3908 }
3909
Winson Chung64359a52013-07-08 17:17:08 -07003910 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003911 mWorkspace.removeItemsByPackageName(packageNames);
3912 } else {
3913 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003914 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003915
Winson Chunga1820962011-10-03 16:31:06 -07003916 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003917 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003918 }
Joe Onoratobe386092009-11-17 17:32:16 -08003919
3920 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003921 * A number of packages were updated.
3922 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003923 private ArrayList<Object> mWidgetsAndShortcuts;
3924 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003925 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003926 bindPackagesUpdated(mWidgetsAndShortcuts);
3927 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003928 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003929 };
3930
3931 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3932 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3933 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003934 return;
3935 }
3936
Winson Chung64359a52013-07-08 17:17:08 -07003937 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003938 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003939 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003940 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003941 }
3942
Winson Chung400438b2011-01-16 17:53:48 -08003943 private int mapConfigurationOriActivityInfoOri(int configOri) {
3944 final Display d = getWindowManager().getDefaultDisplay();
3945 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3946 switch (d.getRotation()) {
3947 case Surface.ROTATION_0:
3948 case Surface.ROTATION_180:
3949 // We are currently in the same basic orientation as the natural orientation
3950 naturalOri = configOri;
3951 break;
3952 case Surface.ROTATION_90:
3953 case Surface.ROTATION_270:
3954 // We are currently in the other basic orientation to the natural orientation
3955 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3956 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3957 break;
3958 }
3959
3960 int[] oriMap = {
3961 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3962 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3963 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3964 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3965 };
3966 // Since the map starts at portrait, we need to offset if this device's natural orientation
3967 // is landscape.
3968 int indexOffset = 0;
3969 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3970 indexOffset = 1;
3971 }
3972 return oriMap[(d.getRotation() + indexOffset) % 4];
3973 }
Adam Cohen446e9402011-09-15 18:21:21 -07003974
Winson Chung4b919f82012-05-01 10:44:08 -07003975 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003976 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003977 getResources().getBoolean(R.bool.allow_rotation);
3978 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003979 }
Winson Chung4b919f82012-05-01 10:44:08 -07003980 public void lockScreenOrientation() {
3981 if (isRotationEnabled()) {
3982 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3983 .getConfiguration().orientation));
3984 }
3985 }
3986 public void unlockScreenOrientation(boolean immediate) {
3987 if (isRotationEnabled()) {
3988 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003989 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003990 } else {
3991 mHandler.postDelayed(new Runnable() {
3992 public void run() {
3993 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3994 }
3995 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003996 }
Winson Chung4b919f82012-05-01 10:44:08 -07003997 }
Winson Chung400438b2011-01-16 17:53:48 -08003998 }
3999
Winson Chung82f55532011-08-09 14:14:23 -07004000 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004001 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004002 if (DISABLE_CLINGS) {
4003 return false;
4004 }
4005
Brett Chabot2a9e2282011-08-23 15:03:13 -07004006 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004007 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004008
Michael Jurkae233a8b2013-03-19 13:49:20 +01004009 // Restricted secondary users (child mode) will potentially have very few apps
4010 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004011// boolean supportsLimitedUsers =
4012// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4013// Account[] accounts = AccountManager.get(this).getAccounts();
4014// if (supportsLimitedUsers && accounts.length == 0) {
4015// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4016// Bundle restrictions = um.getUserRestrictions();
4017// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4018// return false;
4019// }
4020// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004021 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004022 }
Michael Jurka22143132012-10-04 17:42:38 +02004023
Winson Chung7d7541e2011-09-16 20:14:36 -07004024 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004025 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004026 if (cling != null) {
4027 cling.init(this, positionData);
4028 cling.setVisibility(View.VISIBLE);
4029 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4030 if (animate) {
4031 cling.buildLayer();
4032 cling.setAlpha(0f);
4033 cling.animate()
4034 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004035 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004036 .setDuration(SHOW_CLING_DURATION)
4037 .setStartDelay(delay)
4038 .start();
4039 } else {
4040 cling.setAlpha(1f);
4041 }
Michael Jurka22143132012-10-04 17:42:38 +02004042 cling.setFocusableInTouchMode(true);
4043 cling.post(new Runnable() {
4044 public void run() {
4045 cling.setFocusable(true);
4046 cling.requestFocus();
4047 }
4048 });
4049 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4050 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004051 }
4052 return cling;
4053 }
Michael Jurka22143132012-10-04 17:42:38 +02004054
Winson Chung7d7541e2011-09-16 20:14:36 -07004055 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004056 // To catch cases where siblings of top-level views are made invisible, just check whether
4057 // the cling is directly set to GONE before dismissing it.
4058 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004059 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004060 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004061 anim.addListener(new AnimatorListenerAdapter() {
4062 public void onAnimationEnd(Animator animation) {
4063 cling.setVisibility(View.GONE);
4064 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004065 // We should update the shared preferences on a background thread
4066 new Thread("dismissClingThread") {
4067 public void run() {
4068 SharedPreferences.Editor editor = mSharedPrefs.edit();
4069 editor.putBoolean(flag, true);
4070 editor.commit();
4071 }
4072 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004073 };
4074 });
4075 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004076 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004077 }
4078 }
Michael Jurka22143132012-10-04 17:42:38 +02004079
Winson Chung9d9d74f2011-09-19 11:49:12 -07004080 private void removeCling(int id) {
4081 final View cling = findViewById(id);
4082 if (cling != null) {
4083 final ViewGroup parent = (ViewGroup) cling.getParent();
4084 parent.post(new Runnable() {
4085 @Override
4086 public void run() {
4087 parent.removeView(cling);
4088 }
4089 });
Michael Jurka22143132012-10-04 17:42:38 +02004090 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004091 }
4092 }
Michael Jurka974c3862012-05-22 22:00:31 -07004093
4094 private boolean skipCustomClingIfNoAccounts() {
4095 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4096 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4097 if (customCling) {
4098 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004099 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004100 Account[] accounts = am.getAccountsByType("com.google");
4101 return accounts.length == 0;
4102 }
4103 return false;
4104 }
4105
Winson Chung7d7541e2011-09-16 20:14:36 -07004106 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004107 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004108 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004109 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4110 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004111 // If we're not using the default workspace layout, replace workspace cling
4112 // with a custom workspace cling (usually specified in an overlay)
4113 // For now, only do this on tablets
4114 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004115 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004116 // Use a custom cling
4117 View cling = findViewById(R.id.workspace_cling);
4118 ViewGroup clingParent = (ViewGroup) cling.getParent();
4119 int clingIndex = clingParent.indexOfChild(cling);
4120 clingParent.removeViewAt(clingIndex);
4121 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4122 clingParent.addView(customCling, clingIndex);
4123 customCling.setId(R.id.workspace_cling);
4124 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004125 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004126 } else {
4127 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004128 }
4129 }
4130 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004131 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004132 if (isClingsEnabled() &&
4133 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004134 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004135 } else {
4136 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004137 }
4138 }
4139 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004140 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004141 if (isClingsEnabled() &&
4142 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4143 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004144 } else {
4145 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004146 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004147 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004148 }
4149 public boolean isFolderClingVisible() {
4150 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004151 if (cling != null) {
4152 return cling.getVisibility() == View.VISIBLE;
4153 }
4154 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004155 }
Winson Chung82f55532011-08-09 14:14:23 -07004156 public void dismissWorkspaceCling(View v) {
4157 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004158 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004159 }
4160 public void dismissAllAppsCling(View v) {
4161 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004162 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4163 }
4164 public void dismissFolderCling(View v) {
4165 Cling cling = (Cling) findViewById(R.id.folder_cling);
4166 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004167 }
4168
Winson Chung80baf5a2010-08-09 16:03:15 -07004169 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004170 * Prints out out state for debugging.
4171 */
4172 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004173 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004174 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004175 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4176 Log.d(TAG, "mRestoring=" + mRestoring);
4177 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4178 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004179 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004180 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004181
Winson Chung785d2eb2011-04-14 16:08:02 -07004182 if (mAppsCustomizeContent != null) {
4183 mAppsCustomizeContent.dumpState();
4184 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004185 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004186 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004187
4188 @Override
4189 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4190 super.dump(prefix, fd, writer, args);
4191 writer.println(" ");
4192 writer.println("Debug logs: ");
4193 for (int i = 0; i < sDumpLogs.size(); i++) {
4194 writer.println(" " + sDumpLogs.get(i));
4195 }
4196 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004197
4198 public static void dumpDebugLogsToConsole() {
4199 Log.d(TAG, "");
4200 Log.d(TAG, "*********************");
4201 Log.d(TAG, "Launcher debug logs: ");
4202 for (int i = 0; i < sDumpLogs.size(); i++) {
4203 Log.d(TAG, " " + sDumpLogs.get(i));
4204 }
4205 Log.d(TAG, "*********************");
4206 Log.d(TAG, "");
4207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004208}
Michael Jurka2763be32011-02-24 11:19:57 -08004209
Michael Jurkaabded662011-03-04 12:06:57 -08004210interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004211 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004212 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004213 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004214 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004215 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004216}