blob: c32de33f69d1adf6c3763a763653f36d2fb8297a [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 Cohen268c4752012-06-06 17:47:33 -0700314 private ImageView mFolderIconImageView;
315 private Bitmap mFolderIconBitmap;
316 private Canvas mFolderIconCanvas;
317 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700318
Michael Jurkaddd62e92011-02-16 17:49:14 -0800319 private BubbleTextView mWaitingForResume;
320
Michael Jurka22143132012-10-04 17:42:38 +0200321 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
322 = new HideFromAccessibilityHelper();
323
Michael Jurkac1f5d262011-09-30 19:32:27 -0700324 private Runnable mBuildLayersRunnable = new Runnable() {
325 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700326 if (mWorkspace != null) {
327 mWorkspace.buildPageHardwareLayers();
328 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700329 }
330 };
331
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800332 private static ArrayList<PendingAddArguments> sPendingAddList
333 = new ArrayList<PendingAddArguments>();
334
Michael Jurka0a457bf2012-11-19 14:05:05 -0800335 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
336
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800337 private static class PendingAddArguments {
338 int requestCode;
339 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700340 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700341 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800342 int cellX;
343 int cellY;
344 }
345
Michael Jurka0a457bf2012-11-19 14:05:05 -0800346 private static boolean isPropertyEnabled(String propertyName) {
347 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700348 }
349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 @Override
351 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700352 if (DEBUG_STRICT_MODE) {
353 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
354 .detectDiskReads()
355 .detectDiskWrites()
356 .detectNetwork() // or .detectAll() for all detectable problems
357 .penaltyLog()
358 .build());
359 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
360 .detectLeakedSqlLiteObjects()
361 .detectLeakedClosableObjects()
362 .penaltyLog()
363 .penaltyDeath()
364 .build());
365 }
366
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400368
369 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
370 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400371 LauncherAppState app = LauncherAppState.getInstance();
372 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700373 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800374 mModel = app.setLauncher(this);
375 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400376 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700378
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700379 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700380 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
381 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700382
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700383 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
384 // this also ensures that any synchronous binding below doesn't re-trigger another
385 // LauncherModel load.
386 mPaused = false;
387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200389 android.os.Debug.startMethodTracing(
390 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 }
392
393 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 setContentView(R.layout.launcher);
395 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700396 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800398 registerContentObservers();
399
Joe Onorato7c312c12009-08-13 21:36:53 -0700400 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 mSavedState = savedInstanceState;
403 restoreState(mSavedState);
404
Winson Chunga12a2502010-12-20 14:41:35 -0800405 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700406 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200407 mAppsCustomizeContent.onPackagesUpdated(
408 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700409 }
Winson Chunga12a2502010-12-20 14:41:35 -0800410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 if (PROFILE_STARTUP) {
412 android.os.Debug.stopMethodTracing();
413 }
414
415 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700416 if (sPausedFromUserAction) {
417 // If the user leaves launcher, then we should just load items asynchronously when
418 // they return.
419 mModel.startLoader(true, -1);
420 } else {
421 // We only load the page synchronously if the user rotates (or triggers a
422 // configuration change) while launcher is in the foreground
423 mModel.startLoader(true, mWorkspace.getCurrentPage());
424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 }
426
427 // For handling default keys
428 mDefaultKeySsb = new SpannableStringBuilder();
429 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800430
431 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
432 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800433
Adam Cohenf9426d52012-06-04 17:26:21 -0700434 updateGlobalIcons();
435
436 // On large interfaces, we want the screen to auto-rotate based on the current orientation
437 unlockScreenOrientation(true);
438 }
439
Winson Chung4a2afa32012-07-19 14:53:05 -0700440 protected void onUserLeaveHint() {
441 super.onUserLeaveHint();
442 sPausedFromUserAction = true;
443 }
444
Adam Cohenf9426d52012-06-04 17:26:21 -0700445 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700446 boolean searchVisible = false;
447 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800448 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700449 int coi = getCurrentOrientationIndexForGlobalIcons();
450 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
451 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700452 updateAppMarketIcon();
453 searchVisible = updateGlobalSearchIcon();
454 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700455 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700456 if (sGlobalSearchIcon[coi] != null) {
457 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700458 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700459 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700460 if (sVoiceSearchIcon[coi] != null) {
461 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700462 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700463 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700464 if (sAppMarketIcon[coi] != null) {
465 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800466 }
Winson Chungadf0c182012-08-23 14:59:07 -0700467 if (mSearchDropTargetBar != null) {
468 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
Romain Guycbb89e42009-06-08 15:52:54 -0700471
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700473 if (sLocaleConfiguration == null) {
474 new AsyncTask<Void, Void, LocaleConfiguration>() {
475 @Override
476 protected LocaleConfiguration doInBackground(Void... unused) {
477 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
478 readConfiguration(Launcher.this, localeConfiguration);
479 return localeConfiguration;
480 }
481
482 @Override
483 protected void onPostExecute(LocaleConfiguration result) {
484 sLocaleConfiguration = result;
485 checkForLocaleChange(); // recursive, but now with a locale configuration
486 }
487 }.execute();
488 return;
489 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700490
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 final Configuration configuration = getResources().getConfiguration();
492
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700493 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 final String locale = configuration.locale.toString();
495
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700496 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 final int mcc = configuration.mcc;
498
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700499 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 final int mnc = configuration.mnc;
501
Romain Guy84f296c2009-11-04 15:00:44 -0800502 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503
Romain Guy84f296c2009-11-04 15:00:44 -0800504 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700505 sLocaleConfiguration.locale = locale;
506 sLocaleConfiguration.mcc = mcc;
507 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700508
Joe Onorato0589f0f2010-02-08 13:44:00 -0800509 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700510
511 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
512 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700513 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700514 public void run() {
515 writeConfiguration(Launcher.this, localeConfiguration);
516 }
517 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700518 }
519 }
520
521 private static class LocaleConfiguration {
522 public String locale;
523 public int mcc = -1;
524 public int mnc = -1;
525 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700526
Romain Guy98d01652009-06-30 16:21:04 -0700527 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
528 DataInputStream in = null;
529 try {
530 in = new DataInputStream(context.openFileInput(PREFERENCES));
531 configuration.locale = in.readUTF();
532 configuration.mcc = in.readInt();
533 configuration.mnc = in.readInt();
534 } catch (FileNotFoundException e) {
535 // Ignore
536 } catch (IOException e) {
537 // Ignore
538 } finally {
539 if (in != null) {
540 try {
541 in.close();
542 } catch (IOException e) {
543 // Ignore
544 }
545 }
546 }
547 }
548
549 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
550 DataOutputStream out = null;
551 try {
552 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
553 out.writeUTF(configuration.locale);
554 out.writeInt(configuration.mcc);
555 out.writeInt(configuration.mnc);
556 out.flush();
557 } catch (FileNotFoundException e) {
558 // Ignore
559 } catch (IOException e) {
560 //noinspection ResultOfMethodCallIgnored
561 context.getFileStreamPath(PREFERENCES).delete();
562 } finally {
563 if (out != null) {
564 try {
565 out.close();
566 } catch (IOException e) {
567 // Ignore
568 }
569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 }
571 }
572
Bjorn Bringertc459e522013-06-07 19:36:01 +0100573 public LayoutInflater getInflater() {
574 return mInflater;
575 }
576
Adam Cohen716b51e2011-06-30 12:09:54 -0700577 public DragLayer getDragLayer() {
578 return mDragLayer;
579 }
580
Winson Chung36a62fe2012-05-06 18:04:42 -0700581 boolean isDraggingEnabled() {
582 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
583 // that is subsequently removed from the workspace in startBinding().
584 return !mModel.isLoadingWorkspace();
585 }
586
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 static int getScreen() {
588 synchronized (sLock) {
589 return sScreen;
590 }
591 }
592
593 static void setScreen(int screen) {
594 synchronized (sLock) {
595 sScreen = screen;
596 }
597 }
598
Winson Chung557d6ed2011-07-08 15:34:52 -0700599 /**
600 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
601 * a configuration step, this allows the proper animations to run after other transitions.
602 */
603 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700604 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800605 switch (args.requestCode) {
606 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700607 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700608 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800609 break;
610 case REQUEST_PICK_SHORTCUT:
611 processShortcut(args.intent);
612 break;
613 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700614 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700615 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700616 result = true;
617 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 case REQUEST_CREATE_APPWIDGET:
619 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700620 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700621 result = true;
622 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800623 case REQUEST_PICK_WALLPAPER:
624 // We just wanted the activity result here so we can clear mWaitingForResult
625 break;
626 }
Michael Jurka27614d22012-04-02 06:40:22 -0700627 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
628 // if you turned the screen off and then back while in All Apps, Launcher would not
629 // return to the workspace. Clearing mAddInfo.container here fixes this issue
630 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700631 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800632 }
633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700635 protected void onActivityResult(
636 final int requestCode, final int resultCode, final Intent data) {
637 if (requestCode == REQUEST_BIND_APPWIDGET) {
638 int appWidgetId = data != null ?
639 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
640 if (resultCode == RESULT_CANCELED) {
641 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
642 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700643 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700644 }
645 return;
646 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700647 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800648 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
649 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700650 mWaitingForResult = false;
651
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 // We have special handling for widgets
653 if (isWidgetDrop) {
654 int appWidgetId = data != null ?
655 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700656 if (appWidgetId < 0) {
657 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
658 "widget configuration activity.");
659 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
660 } else {
661 completeTwoStageWidgetDrop(resultCode, appWidgetId);
662 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800663 return;
664 }
665
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 // The pattern used here is that a user PICKs a specific application,
667 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700668
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
670 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700671 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800672 final PendingAddArguments args = new PendingAddArguments();
673 args.requestCode = requestCode;
674 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700675 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700676 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700677 args.cellX = mPendingAddInfo.cellX;
678 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 if (isWorkspaceLocked()) {
680 sPendingAddList.add(args);
681 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700682 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700686 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
688 null);
689 }
690
691 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700692 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700693 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800694 Runnable onCompleteRunnable = null;
695 int animationType = 0;
696
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700697 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800698 if (resultCode == RESULT_OK) {
699 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
700 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700701 mPendingAddWidgetInfo);
702 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800703 onCompleteRunnable = new Runnable() {
704 @Override
705 public void run() {
706 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700707 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800708 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
709 null);
710 }
711 };
712 } else if (resultCode == RESULT_CANCELED) {
713 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
714 onCompleteRunnable = new Runnable() {
715 @Override
716 public void run() {
717 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
718 null);
719 }
720 };
721 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700722 if (mDragLayer.getAnimatedView() != null) {
723 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
724 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
725 animationType, boundWidget, true);
726 } else {
727 // The animated view may be null in the case of a rotation during widget configuration
728 onCompleteRunnable.run();
729 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 }
731
732 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700733 protected void onStop() {
734 super.onStop();
735 FirstFrameAnimatorHelper.setIsVisible(false);
736 }
737
738 @Override
739 protected void onStart() {
740 super.onStart();
741 FirstFrameAnimatorHelper.setIsVisible(true);
742 }
743
744 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200746 long startTime = 0;
747 if (DEBUG_RESUME_TIME) {
748 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400749 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700752
Winson Chung4a2afa32012-07-19 14:53:05 -0700753 // Restore the previous launcher state
754 if (mOnResumeState == State.WORKSPACE) {
755 showWorkspace(false);
756 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
757 showAllApps(false);
758 }
759 mOnResumeState = State.NONE;
760
Craig Mautner360310b2012-10-26 15:13:08 -0700761 // Background was set to gradient in onPause(), restore to black if in all apps.
762 setWorkspaceBackground(mState == State.WORKSPACE);
763
Winson Chungde0fb8f2012-05-08 14:37:08 -0700764 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700765 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700766
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800767 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700768 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700769 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400770 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700771 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700773 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700775 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200776 // We might have postponed some bind calls until onResume (see waitUntilResume) --
777 // execute them here
778 long startTimeCallbacks = 0;
779 if (DEBUG_RESUME_TIME) {
780 startTimeCallbacks = System.currentTimeMillis();
781 }
782
783 if (mAppsCustomizeContent != null) {
784 mAppsCustomizeContent.setBulkBind(true);
785 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700786 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
787 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200788 }
789 if (mAppsCustomizeContent != null) {
790 mAppsCustomizeContent.setBulkBind(false);
791 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700792 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200793 if (DEBUG_RESUME_TIME) {
794 Log.d(TAG, "Time spent processing callbacks in onResume: " +
795 (System.currentTimeMillis() - startTimeCallbacks));
796 }
Michael Jurka447bf842013-05-15 14:52:15 +0200797 }
Winson Chunge4e50662012-01-23 14:45:13 -0800798
799 // Reset the pressed state of icons that were locked in the press state while activities
800 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800801 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800802 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800803 mWaitingForResume.setStayPressed(false);
804 }
Winson Chunge4e50662012-01-23 14:45:13 -0800805 if (mAppsCustomizeContent != null) {
806 // Resets the previous all apps icon press state
807 mAppsCustomizeContent.resetDrawableState();
808 }
Adam Cohen06dff352012-06-01 17:17:08 -0700809 // It is possible that widgets can receive updates while launcher is not in the foreground.
810 // Consequently, the widgets will be inflated in the orientation of the foreground activity
811 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
812 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700813 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700814
815 // Again, as with the above scenario, it's possible that one or more of the global icons
816 // were updated in the wrong orientation.
817 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200818 if (DEBUG_RESUME_TIME) {
819 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
820 }
Adam Cohen06dff352012-06-01 17:17:08 -0700821 }
822
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700824 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700825 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
826 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
827 // when Launcher resumes and we are still in AllApps.
828 updateWallpaperVisibility(true);
829
Winson Chung997a9232013-07-24 15:33:46 -0700830 // Ensure that items added to Launcher are queued until Launcher returns
831 InstallShortcutReceiver.enableInstallQueue();
832
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700833 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700834 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800835 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700836 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700837 }
Romain Guycbb89e42009-06-08 15:52:54 -0700838
Adam Cohen66a01fd2013-06-11 12:48:00 -0700839 protected void onFinishBindingItems() {
840 }
841
Adam Cohenbffe7452013-07-22 18:21:45 -0700842 QSBScroller mQsbScroller = new QSBScroller() {
843 int scrollY = 0;
844
845 @Override
846 public void setScrollY(int scroll) {
847 scrollY = scroll;
848
849 if (mWorkspace.isOnOrMovingToCustomContent()) {
850 mSearchDropTargetBar.setTranslationY(- scrollY);
851 }
852 }
853 };
854
855 public void resetQSBScroll() {
856 mSearchDropTargetBar.animate().translationY(0).start();
857 }
858
859 public interface CustomContentCallbacks {
860 // Custom content is completely shown
861 public void onShow();
862
863 // Custom content is completely hidden
864 public void onHide();
865 }
866
867 public interface QSBScroller {
868 public void setScrollY(int scrollY);
869 }
870
Adam Cohen66a01fd2013-06-11 12:48:00 -0700871 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Adam Cohenbffe7452013-07-22 18:21:45 -0700872 public QSBScroller addCustomContentToLeft(View customContent) {
873 return addCustomContentToLeft(customContent, null);
874 }
875
876 public QSBScroller addCustomContentToLeft(View customContent,
877 CustomContentCallbacks callbacks) {
878 mWorkspace.addCustomContentToLeft(customContent, callbacks);
879 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700880 }
881
Adam Cohenedb40762013-07-18 16:45:45 -0700882 // The custom content needs to offset its content to account for the QSB
883 public int getTopOffsetForCustomContent() {
884 return mWorkspace.getPaddingTop();
885 }
886
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700887 @Override
888 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889 // Flag the loader to stop early before switching
890 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700891 if (mAppsCustomizeContent != null) {
892 mAppsCustomizeContent.surrender();
893 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800894 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700895 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700896
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800897 // We can't hide the IME if it was forced open. So don't bother
898 /*
899 @Override
900 public void onWindowFocusChanged(boolean hasFocus) {
901 super.onWindowFocusChanged(hasFocus);
902
903 if (hasFocus) {
904 final InputMethodManager inputManager = (InputMethodManager)
905 getSystemService(Context.INPUT_METHOD_SERVICE);
906 WindowManager.LayoutParams lp = getWindow().getAttributes();
907 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
908 android.os.Handler()) {
909 protected void onReceiveResult(int resultCode, Bundle resultData) {
910 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
911 }
912 });
913 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
914 }
915 }
916 */
917
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 private boolean acceptFilter() {
919 final InputMethodManager inputManager = (InputMethodManager)
920 getSystemService(Context.INPUT_METHOD_SERVICE);
921 return !inputManager.isFullscreenMode();
922 }
923
924 @Override
925 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700926 final int uniChar = event.getUnicodeChar();
927 final boolean handled = super.onKeyDown(keyCode, event);
928 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
929 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
931 keyCode, event);
932 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700933 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700934 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700935 // showSearchDialog()
936 // If there are multiple keystrokes before the search dialog takes focus,
937 // onSearchRequested() will be called for every keystroke,
938 // but it is idempotent, so it's fine.
939 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941 }
942
Joe Onorato8a9625e2010-01-28 15:55:35 -0800943 // Eat the long press event so the keyboard doesn't come up.
944 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
945 return true;
946 }
947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 return handled;
949 }
950
Karl Rosaen138a0412009-04-23 19:00:21 -0700951 private String getTypedText() {
952 return mDefaultKeySsb.toString();
953 }
954
955 private void clearTypedText() {
956 mDefaultKeySsb.clear();
957 mDefaultKeySsb.clearSpans();
958 Selection.setSelection(mDefaultKeySsb, 0);
959 }
960
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700962 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
963 * State
964 */
965 private static State intToState(int stateOrdinal) {
966 State state = State.WORKSPACE;
967 final State[] stateValues = State.values();
968 for (int i = 0; i < stateValues.length; i++) {
969 if (stateValues[i].ordinal() == stateOrdinal) {
970 state = stateValues[i];
971 break;
972 }
973 }
974 return state;
975 }
976
977 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 * Restores the previous state, if it exists.
979 *
980 * @param savedState The previous state.
981 */
982 private void restoreState(Bundle savedState) {
983 if (savedState == null) {
984 return;
985 }
986
Michael Jurka883f55b2010-10-21 15:47:14 -0700987 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700988 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700989 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800990 }
991
Winson Chungf0c6ae02012-03-21 16:10:31 -0700992 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700994 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
996
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700998 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700999
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1001 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001003 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1004 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001005 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1006 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1007 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001008 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 mRestoring = true;
1010 }
1011
1012 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1013 if (renameFolder) {
1014 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001015 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 mRestoring = true;
1017 }
Winson Chunga12a2502010-12-20 14:41:35 -08001018
Winson Chung785d2eb2011-04-14 16:08:02 -07001019 // Restore the AppsCustomize tab
1020 if (mAppsCustomizeTabHost != null) {
1021 String curTab = savedState.getString("apps_customize_currentTab");
1022 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001023 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001024 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001025 mAppsCustomizeContent.loadAssociatedPages(
1026 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001027 }
1028
Winson Chung5afbf7b2011-07-25 11:53:08 -07001029 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1030 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 }
1033
1034 /**
1035 * Finds all the views we need and configure them properly.
1036 */
1037 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001038 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001039
Craig Mautner360310b2012-10-26 15:13:08 -07001040 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001041 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1042 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001043 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001044
1045 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1046 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047
Winson Chung4c98d922011-05-31 16:50:48 -07001048 // Setup the drag layer
1049 mDragLayer.setup(this, dragController);
1050
Winson Chung3d503fb2011-07-13 17:25:49 -07001051 // Setup the hotseat
1052 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1053 if (mHotseat != null) {
1054 mHotseat.setup(this);
1055 }
1056
Winson Chung4c98d922011-05-31 16:50:48 -07001057 // Setup the workspace
1058 mWorkspace.setHapticFeedbackEnabled(false);
1059 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001060 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001061 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001062
Winson Chungf0ea4d32011-06-06 14:27:16 -07001063 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001064 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001065
Winson Chungf0ea4d32011-06-06 14:27:16 -07001066 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001067 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001068 mAppsCustomizeContent = (AppsCustomizePagedView)
1069 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1070 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001071
Winson Chung3d503fb2011-07-13 17:25:49 -07001072 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001073 dragController.setDragScoller(mWorkspace);
1074 dragController.setScrollView(mDragLayer);
1075 dragController.setMoveTarget(mWorkspace);
1076 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001077 if (mSearchDropTargetBar != null) {
1078 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001079 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001080
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001081 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001082 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001083 mWeightWatcher = new WeightWatcher(this);
1084 mWeightWatcher.setAlpha(0.5f);
1085 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001086 new FrameLayout.LayoutParams(
1087 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001088 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001089 Gravity.BOTTOM)
1090 );
Adam Cohen39a06042013-07-19 14:30:12 -07001091
1092 boolean show = shouldShowWeightWatcher();
1093 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001094 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
1096
1097 /**
1098 * Creates a view representing a shortcut.
1099 *
1100 * @param info The data structure describing the shortcut.
1101 *
1102 * @return A View inflated from R.layout.application.
1103 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001104 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001106 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 }
1108
1109 /**
1110 * Creates a view representing a shortcut inflated from the specified resource.
1111 *
1112 * @param layoutResId The id of the XML layout used to create the shortcut.
1113 * @param parent The group the shortcut belongs to.
1114 * @param info The data structure describing the shortcut.
1115 *
1116 * @return A View inflated from layoutResId.
1117 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001118 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001119 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1120 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 return favorite;
1123 }
1124
1125 /**
1126 * Add an application shortcut to the workspace.
1127 *
1128 * @param data The intent describing the application.
1129 * @param cellInfo The position on screen where to create the shortcut.
1130 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001131 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001132 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001133 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001134
Adam Cohenfbba09b2011-07-18 21:43:05 -07001135 // First we check if we already know the exact location where we want to add this item.
1136 if (cellX >= 0 && cellY >= 0) {
1137 cellXY[0] = cellX;
1138 cellXY[1] = cellY;
1139 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001140 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001141 return;
1142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001144 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001145
Romain Guy73b979d2009-06-09 12:57:21 -07001146 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001147 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001149 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001150 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001151 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001152 } else {
1153 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
1155 }
Romain Guycbb89e42009-06-08 15:52:54 -07001156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 /**
1158 * Add a shortcut to the workspace.
1159 *
1160 * @param data The intent describing the shortcut.
1161 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001163 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001164 int cellY) {
1165 int[] cellXY = mTmpAddItemCellCoordinates;
1166 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001167 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001168
Michael Jurkad3ef3062010-11-23 16:23:58 -08001169 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001170
Adam Cohen558baaf2011-08-15 15:22:57 -07001171 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001172 if (info == null) {
1173 return;
1174 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001175 final View view = createShortcut(info);
1176
Adam Cohenfbba09b2011-07-18 21:43:05 -07001177 // First we check if we already know the exact location where we want to add this item.
1178 if (cellX >= 0 && cellY >= 0) {
1179 cellXY[0] = cellX;
1180 cellXY[1] = cellY;
1181 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001182
1183 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001184 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001185 true, null,null)) {
1186 return;
1187 }
1188 DragObject dragObject = new DragObject();
1189 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001190 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1191 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001192 return;
1193 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001194 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001195 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001196 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001197 foundCellSpan = (result != null);
1198 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001199 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001200 }
1201
1202 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001203 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001204 return;
1205 }
1206
Adam Cohendcd297f2013-06-18 13:13:40 -07001207 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208
1209 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001210 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001211 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213 }
1214
Adam Cohen2f093b62012-04-30 18:59:53 -07001215 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1216 int minHeight) {
1217 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001218 // We want to account for the extra amount of padding that we are adding to the widget
1219 // to ensure that it gets the full amount of space that it has requested
1220 int requiredWidth = minWidth + padding.left + padding.right;
1221 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001222 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001223 }
1224
Adam Cohen2f093b62012-04-30 18:59:53 -07001225 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1226 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001227 }
1228
Adam Cohen2f093b62012-04-30 18:59:53 -07001229 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1230 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001231 }
1232
Adam Cohen2f093b62012-04-30 18:59:53 -07001233 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1234 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001235 }
1236
Adam Cohen2f093b62012-04-30 18:59:53 -07001237 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1238 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001239 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001240 }
1241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001243 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001245 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001246 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001248 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001249 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1250 if (appWidgetInfo == null) {
1251 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1252 }
Romain Guycbb89e42009-06-08 15:52:54 -07001253
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001254 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001255 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001256
Adam Cohen2f093b62012-04-30 18:59:53 -07001257 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1258 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001261 // We have saved the position to which the widget was dragged-- this really only matters
1262 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001263 int[] cellXY = mTmpAddItemCellCoordinates;
1264 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001265 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001266 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001267 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1268 cellXY[0] = mPendingAddInfo.cellX;
1269 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001270 spanXY[0] = mPendingAddInfo.spanX;
1271 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001272 foundCellSpan = true;
1273 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001274 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001275 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001276 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1277 spanXY[1], cellXY, finalSpan);
1278 spanXY[0] = finalSpan[0];
1279 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001280 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001281 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001282 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001283 }
1284
Michael Jurka0280c3b2010-09-17 15:00:07 -07001285 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001286 if (appWidgetId != -1) {
1287 // Deleting an app widget ID is a void call but writes to disk before returning
1288 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001289 new Thread("deleteAppWidgetId") {
1290 public void run() {
1291 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1292 }
1293 }.start();
1294 }
Winson Chung93eef082012-03-23 15:59:27 -07001295 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001296 return;
1297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001299 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001300 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1301 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001302 launcherInfo.spanX = spanXY[0];
1303 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001304 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1305 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001306
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001308 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309
1310 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001311 if (hostView == null) {
1312 // Perform actual inflation because we're live
1313 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1314 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1315 } else {
1316 // The AppWidgetHostView has already been inflated and instantiated
1317 launcherInfo.hostView = hostView;
1318 }
Romain Guycbb89e42009-06-08 15:52:54 -07001319
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001321 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001322 launcherInfo.notifyWidgetSizeChanged(this);
1323
Adam Cohendcd297f2013-06-18 13:13:40 -07001324 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001326
1327 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001329 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
Romain Guycbb89e42009-06-08 15:52:54 -07001331
Adam Cohended9f8d2010-11-03 13:25:16 -07001332 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1333 @Override
1334 public void onReceive(Context context, Intent intent) {
1335 final String action = intent.getAction();
1336 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1337 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001338 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001339 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001340
Winson Chungc100e8e2011-08-09 16:02:43 -07001341 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001342 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001343 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1344 mAppsCustomizeTabHost.reset();
1345 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001346 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001347 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1348 mUserPresent = true;
1349 updateRunning();
1350 }
1351 }
1352 };
1353
1354 @Override
1355 public void onAttachedToWindow() {
1356 super.onAttachedToWindow();
1357
1358 // Listen for broadcasts related to user-presence
1359 final IntentFilter filter = new IntentFilter();
1360 filter.addAction(Intent.ACTION_SCREEN_OFF);
1361 filter.addAction(Intent.ACTION_USER_PRESENT);
1362 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001363 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001364 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001365 mVisible = true;
1366 }
1367
1368 @Override
1369 public void onDetachedFromWindow() {
1370 super.onDetachedFromWindow();
1371 mVisible = false;
1372
Adam Cohend113e0c2010-11-11 10:48:05 -08001373 if (mAttached) {
1374 unregisterReceiver(mReceiver);
1375 mAttached = false;
1376 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001377 updateRunning();
1378 }
1379
1380 public void onWindowVisibilityChanged(int visibility) {
1381 mVisible = visibility == View.VISIBLE;
1382 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001383 // The following code used to be in onResume, but it turns out onResume is called when
1384 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1385 // is a more appropriate event to handle
1386 if (mVisible) {
1387 mAppsCustomizeTabHost.onWindowVisible();
1388 if (!mWorkspaceLoading) {
1389 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001390 // We want to let Launcher draw itself at least once before we force it to build
1391 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001392 // apps is nice and speedy.
1393 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001394 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001395 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001396 if (mStarted) return;
1397 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001398 // We delay the layer building a bit in order to give
1399 // other message processing a time to run. In particular
1400 // this avoids a delay in hiding the IME if it was
1401 // currently shown, because doing that may involve
1402 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001403 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001404 final ViewTreeObserver.OnDrawListener listener = this;
1405 mWorkspace.post(new Runnable() {
1406 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001407 if (mWorkspace != null &&
1408 mWorkspace.getViewTreeObserver() != null) {
1409 mWorkspace.getViewTreeObserver().
1410 removeOnDrawListener(listener);
1411 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001412 }
1413 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001414 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001415 }
1416 });
1417 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001418 // When Launcher comes back to foreground, a different Activity might be responsible for
1419 // the app market intent, so refresh the icon
1420 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001421 clearTypedText();
1422 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001423 }
1424
1425 private void sendAdvanceMessage(long delay) {
1426 mHandler.removeMessages(ADVANCE_MSG);
1427 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1428 mHandler.sendMessageDelayed(msg, delay);
1429 mAutoAdvanceSentTime = System.currentTimeMillis();
1430 }
1431
1432 private void updateRunning() {
1433 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1434 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1435 mAutoAdvanceRunning = autoAdvanceRunning;
1436 if (autoAdvanceRunning) {
1437 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1438 sendAdvanceMessage(delay);
1439 } else {
1440 if (!mWidgetsToAdvance.isEmpty()) {
1441 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1442 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1443 }
1444 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001445 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001446 }
1447 }
1448 }
1449
1450 private final Handler mHandler = new Handler() {
1451 @Override
1452 public void handleMessage(Message msg) {
1453 if (msg.what == ADVANCE_MSG) {
1454 int i = 0;
1455 for (View key: mWidgetsToAdvance.keySet()) {
1456 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1457 final int delay = mAdvanceStagger * i;
1458 if (v instanceof Advanceable) {
1459 postDelayed(new Runnable() {
1460 public void run() {
1461 ((Advanceable) v).advance();
1462 }
1463 }, delay);
1464 }
1465 i++;
1466 }
1467 sendAdvanceMessage(mAdvanceInterval);
1468 }
1469 }
1470 };
1471
1472 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001473 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001474 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1475 if (v instanceof Advanceable) {
1476 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001477 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001478 updateRunning();
1479 }
1480 }
1481
1482 void removeWidgetToAutoAdvance(View hostView) {
1483 if (mWidgetsToAdvance.containsKey(hostView)) {
1484 mWidgetsToAdvance.remove(hostView);
1485 updateRunning();
1486 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001487 }
1488
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001490 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001491 launcherInfo.hostView = null;
1492 }
1493
Winson Chung93eef082012-03-23 15:59:27 -07001494 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1495 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1496 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001497 }
1498
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001499 public LauncherAppWidgetHost getAppWidgetHost() {
1500 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 }
Romain Guycbb89e42009-06-08 15:52:54 -07001502
Winson Chunga9abd0e2010-10-27 17:18:37 -07001503 public LauncherModel getModel() {
1504 return mModel;
1505 }
1506
Bjorn Bringertc459e522013-06-07 19:36:01 +01001507 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001508 getWindow().closeAllPanels();
1509
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001510 // Whatever we were doing is hereby canceled.
1511 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001512 }
1513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 @Override
1515 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001516 long startTime = 0;
1517 if (DEBUG_RESUME_TIME) {
1518 startTime = System.currentTimeMillis();
1519 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 super.onNewIntent(intent);
1521
1522 // Close the menu
1523 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001524 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001525 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001526
Jamie Genniscb222e82012-10-23 15:44:26 -07001527 final boolean alreadyOnHome =
1528 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001529 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001530
Jamie Genniscb222e82012-10-23 15:44:26 -07001531 Runnable processIntent = new Runnable() {
1532 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001533 if (mWorkspace == null) {
1534 // Can be cases where mWorkspace is null, this prevents a NPE
1535 return;
1536 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001537 Folder openFolder = mWorkspace.getOpenFolder();
1538 // In all these cases, only animate if we're already on home
1539 mWorkspace.exitWidgetResizeMode();
1540 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1541 openFolder == null) {
1542 mWorkspace.moveToDefaultScreen(true);
1543 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001544
Jamie Genniscb222e82012-10-23 15:44:26 -07001545 closeFolder();
1546 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001547
Jamie Genniscb222e82012-10-23 15:44:26 -07001548 // If we are already on home, then just animate back to the workspace,
1549 // otherwise, just wait until onResume to set the state back to Workspace
1550 if (alreadyOnHome) {
1551 showWorkspace(true);
1552 } else {
1553 mOnResumeState = State.WORKSPACE;
1554 }
1555
1556 final View v = getWindow().peekDecorView();
1557 if (v != null && v.getWindowToken() != null) {
1558 InputMethodManager imm = (InputMethodManager)getSystemService(
1559 INPUT_METHOD_SERVICE);
1560 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1561 }
1562
1563 // Reset AllApps to its initial state
1564 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1565 mAppsCustomizeTabHost.reset();
1566 }
1567 }
1568 };
1569
1570 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1571 // Delay processing of the intent to allow the status bar animation to finish
1572 // first in order to avoid janky animations.
1573 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001574 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001575 // Process the intent immediately.
1576 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001577 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001578
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
Michael Jurka447bf842013-05-15 14:52:15 +02001580 if (DEBUG_RESUME_TIME) {
1581 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
1584
1585 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001586 public void onRestoreInstanceState(Bundle state) {
1587 super.onRestoreInstanceState(state);
1588 for (int page: mSynchronouslyBoundPages) {
1589 mWorkspace.restoreInstanceStateForChild(page);
1590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
1592
1593 @Override
1594 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001595 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001596 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597
Michael Jurka883f55b2010-10-21 15:47:14 -07001598 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001599 // We close any open folder since it will not be re-opened, and we need to make sure
1600 // this state is reflected.
1601 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602
Adam Cohendcd297f2013-06-18 13:13:40 -07001603 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001604 mWaitingForResult) {
1605 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001606 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001607 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1608 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001609 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1610 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1611 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 }
1613
1614 if (mFolderInfo != null && mWaitingForResult) {
1615 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1616 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1617 }
Michael Jurka946ad472010-07-09 18:05:18 -07001618
Winson Chung785d2eb2011-04-14 16:08:02 -07001619 // Save the current AppsCustomize tab
1620 if (mAppsCustomizeTabHost != null) {
1621 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1622 if (currentTabTag != null) {
1623 outState.putString("apps_customize_currentTab", currentTabTag);
1624 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001625 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1626 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 }
1629
1630 @Override
1631 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001633
Winson Chunge7a03942011-08-05 15:05:12 -07001634 // Remove all pending runnables
1635 mHandler.removeMessages(ADVANCE_MSG);
1636 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001637 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001638
Winson Chungcd2b0142011-06-08 16:02:26 -07001639 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001640 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001641 mModel.stopLoader();
1642 app.setLauncher(null);
1643
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001645 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001647 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001649 mAppWidgetHost = null;
1650
1651 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652
1653 TextKeyListener.getInstance().release();
1654
Winson Chung81b52252012-08-27 15:34:29 -07001655 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1656 // to prevent leaking Launcher activities on orientation change.
1657 if (mModel != null) {
1658 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1659 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001660
1661 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001662 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001663
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001664 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001665 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1666 mWorkspace.removeAllViews();
1667 mWorkspace = null;
1668 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001669
Michael Jurka2ecf9952012-06-18 12:52:28 -07001670 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 }
1672
Adam Cohena9cf38f2011-05-02 15:36:58 -07001673 public DragController getDragController() {
1674 return mDragController;
1675 }
1676
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 @Override
1678 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001679 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 super.startActivityForResult(intent, requestCode);
1681 }
1682
Winson Chung70d72102011-08-12 11:24:35 -07001683 /**
1684 * Indicates that we want global search for this activity by setting the globalSearch
1685 * argument for {@link #startSearch} to true.
1686 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001688 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001690
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001691 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001692
Karl Rosaen138a0412009-04-23 19:00:21 -07001693 if (initialQuery == null) {
1694 // Use any text typed in the launcher as the initial query
1695 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 if (appSearchData == null) {
1698 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001699 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 }
Winson Chungadf0c182012-08-23 14:59:07 -07001701 Rect sourceBounds = new Rect();
1702 if (mSearchDropTargetBar != null) {
1703 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1704 }
Romain Guycbb89e42009-06-08 15:52:54 -07001705
Bjorn Bringertc459e522013-06-07 19:36:01 +01001706 startSearch(initialQuery, selectInitialQuery,
1707 appSearchData, sourceBounds);
1708 }
1709
1710 public void startSearch(String initialQuery,
1711 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001712 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001713 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001714 }
1715
1716 /**
1717 * Starts the global search activity. This code is a copied from SearchManager
1718 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001719 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001720 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001721 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001722 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1723 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1724 if (globalSearchActivity == null) {
1725 Log.w(TAG, "No global search activity found.");
1726 return;
1727 }
1728 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1729 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1730 intent.setComponent(globalSearchActivity);
1731 // Make sure that we have a Bundle to put source in
1732 if (appSearchData == null) {
1733 appSearchData = new Bundle();
1734 } else {
1735 appSearchData = new Bundle(appSearchData);
1736 }
1737 // Set source to package name of app that starts global search, if not set already.
1738 if (!appSearchData.containsKey("source")) {
1739 appSearchData.putString("source", getPackageName());
1740 }
1741 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1742 if (!TextUtils.isEmpty(initialQuery)) {
1743 intent.putExtra(SearchManager.QUERY, initialQuery);
1744 }
1745 if (selectInitialQuery) {
1746 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1747 }
1748 intent.setSourceBounds(sourceBounds);
1749 try {
1750 startActivity(intent);
1751 } catch (ActivityNotFoundException ex) {
1752 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
1755
Winson Chung70d72102011-08-12 11:24:35 -07001756 @Override
1757 public boolean onCreateOptionsMenu(Menu menu) {
1758 if (isWorkspaceLocked()) {
1759 return false;
1760 }
1761
1762 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001763
1764 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1765 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1766 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001767 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1768 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1769 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001770 String helpUrl = getString(R.string.help_url);
1771 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001772 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1773 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1774
Winson Chung70d72102011-08-12 11:24:35 -07001775 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1776 .setIcon(android.R.drawable.ic_menu_gallery)
1777 .setAlphabeticShortcut('W');
1778 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1779 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001780 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001781 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001782 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1783 .setIcon(android.R.drawable.ic_menu_preferences)
1784 .setIntent(settings)
1785 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001786 if (!helpUrl.isEmpty()) {
1787 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1788 .setIcon(android.R.drawable.ic_menu_help)
1789 .setIntent(help)
1790 .setAlphabeticShortcut('H');
1791 }
Winson Chung70d72102011-08-12 11:24:35 -07001792 return true;
1793 }
1794
1795 @Override
1796 public boolean onPrepareOptionsMenu(Menu menu) {
1797 super.onPrepareOptionsMenu(menu);
1798
1799 if (mAppsCustomizeTabHost.isTransitioning()) {
1800 return false;
1801 }
1802 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1803 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1804
1805 return true;
1806 }
1807
1808 @Override
1809 public boolean onOptionsItemSelected(MenuItem item) {
1810 switch (item.getItemId()) {
1811 case MENU_WALLPAPER_SETTINGS:
1812 startWallpaper();
1813 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001814 }
1815
1816 return super.onOptionsItemSelected(item);
1817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001819 @Override
1820 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001821 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001822 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001823 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001824 }
1825
Joe Onorato9c1289c2009-08-17 11:03:03 -04001826 public boolean isWorkspaceLocked() {
1827 return mWorkspaceLoading || mWaitingForResult;
1828 }
1829
Michael Jurka0280c3b2010-09-17 15:00:07 -07001830 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001831 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001832 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001833 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1834 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001835 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001836 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001837 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001838
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001839 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1840 AppWidgetProviderInfo appWidgetInfo) {
1841 if (appWidgetInfo.configure != null) {
1842 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001843
Bjorn Bringert7984c942009-12-09 15:38:25 +00001844 // Launch over to configure widget, if needed
1845 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001846 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001847 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001848 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001850 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001851 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001852 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001853 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001854 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 }
1856 }
Romain Guycbb89e42009-06-08 15:52:54 -07001857
Adam Cohenfbba09b2011-07-18 21:43:05 -07001858 /**
1859 * Process a shortcut drop.
1860 *
1861 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001862 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001863 * @param cell The cell it should be added to, optional
1864 * @param position The location on the screen where it was dropped, optional
1865 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001866 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001867 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001868 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001869 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001870 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001871 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001872
1873 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001874 mPendingAddInfo.cellX = cell[0];
1875 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001876 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001877
1878 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1879 createShortcutIntent.setComponent(componentName);
1880 processShortcut(createShortcutIntent);
1881 }
1882
Adam Cohenfbba09b2011-07-18 21:43:05 -07001883 /**
1884 * Process a widget drop.
1885 *
1886 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001887 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001888 * @param cell The cell it should be added to, optional
1889 * @param position The location on the screen where it was dropped, optional
1890 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001891 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001892 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001893 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001894 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001895 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001896 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001897 mPendingAddInfo.minSpanX = info.minSpanX;
1898 mPendingAddInfo.minSpanY = info.minSpanY;
1899
Adam Cohenfbba09b2011-07-18 21:43:05 -07001900 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001901 mPendingAddInfo.cellX = cell[0];
1902 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001903 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001904 if (span != null) {
1905 mPendingAddInfo.spanX = span[0];
1906 mPendingAddInfo.spanY = span[1];
1907 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001908
Adam Cohened66b2b2012-01-23 17:28:51 -08001909 AppWidgetHostView hostView = info.boundWidget;
1910 int appWidgetId;
1911 if (hostView != null) {
1912 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001913 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001914 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001915 // In this case, we either need to start an activity to get permission to bind
1916 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001917 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001918 Bundle options = info.bindOptions;
1919
1920 boolean success = false;
1921 if (options != null) {
1922 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1923 info.componentName, options);
1924 } else {
1925 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1926 info.componentName);
1927 }
1928 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001929 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001930 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001931 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001932 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1933 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1934 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001935 // TODO: we need to make sure that this accounts for the options bundle.
1936 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001937 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1938 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001939 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001940 }
1941
Joe Onoratodeb98af2010-02-19 14:59:39 -08001942 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001943 // Handle case where user selected "Applications"
1944 String applicationName = getResources().getString(R.string.group_applications);
1945 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001946
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001947 if (applicationName != null && applicationName.equals(shortcutName)) {
1948 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1949 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001950
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001951 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1952 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001953 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001954 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001955 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001956 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959
Winson Chung24ab2f12010-09-16 14:10:47 -07001960 void processWallpaper(Intent intent) {
1961 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1962 }
1963
Adam Cohendcd297f2013-06-18 13:13:40 -07001964 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001965 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001966 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 folderInfo.title = getText(R.string.folder_name);
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001970 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001971 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001972 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973
1974 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001975 FolderIcon newFolder =
1976 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001977 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001978 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001979 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 }
Romain Guycbb89e42009-06-08 15:52:54 -07001981
Joe Onorato9c1289c2009-08-17 11:03:03 -04001982 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001983 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001984 }
1985
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001987 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001989 Intent chooser = Intent.createChooser(pickWallpaper,
1990 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001991 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1992 // Removed in Eclair MR1
1993// WallpaperManager wm = (WallpaperManager)
1994// getSystemService(Context.WALLPAPER_SERVICE);
1995// WallpaperInfo wi = wm.getWallpaperInfo();
1996// if (wi != null && wi.getSettingsActivity() != null) {
1997// LabeledIntent li = new LabeledIntent(getPackageName(),
1998// R.string.configure_wallpaper, 0);
1999// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
2000// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
2001// }
Mike Clerona0618e42009-10-22 13:55:21 -07002002 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
2004
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002005 /**
2006 * Registers various content observers. The current implementation registers
2007 * only a favorites observer to keep track of the favorites applications.
2008 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002009 private void registerContentObservers() {
2010 ContentResolver resolver = getContentResolver();
2011 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2012 true, mWidgetObserver);
2013 }
2014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 @Override
2016 public boolean dispatchKeyEvent(KeyEvent event) {
2017 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2018 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002020 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002021 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002022 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002023 dumpState();
2024 return true;
2025 }
2026 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002027 }
2028 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2029 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002030 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 return true;
2032 }
2033 }
2034
2035 return super.dispatchKeyEvent(event);
2036 }
2037
Joe Onorato88ec0992009-11-19 13:16:06 -08002038 @Override
2039 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002040 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002041 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002042 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002043 Folder openFolder = mWorkspace.getOpenFolder();
2044 if (openFolder.isEditingName()) {
2045 openFolder.dismissEditingName();
2046 } else {
2047 closeFolder();
2048 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002049 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002050 mWorkspace.exitWidgetResizeMode();
2051
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002052 // Back button is a no-op here, but give at least some feedback for the button press
2053 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002054 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002055 }
2056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002058 * Re-listen when widgets are reset.
2059 */
2060 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002061 if (mAppWidgetHost != null) {
2062 mAppWidgetHost.startListening();
2063 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002064 }
2065
2066 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 * Launches the intent referred by the clicked shortcut.
2068 *
2069 * @param v The view representing the clicked shortcut.
2070 */
2071 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002072 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2073 // view has detached (it's possible for this to happen if the view is removed mid touch).
2074 if (v.getWindowToken() == null) {
2075 return;
2076 }
2077
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002078 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002079 return;
2080 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002083 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002085 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002086
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002087 // Check for special shortcuts
2088 if (intent.getComponent() != null) {
2089 final String shortcutClass = intent.getComponent().getClassName();
2090
2091 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2092 showAllApps(true);
2093 return;
2094 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2095 MemoryDumpActivity.startDump(this);
2096 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002097 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2098 toggleShowWeightWatcher();
2099 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002100 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002101 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002102
2103 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002104 int[] pos = new int[2];
2105 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002106 intent.setSourceBounds(new Rect(pos[0], pos[1],
2107 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002108
2109 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002110
2111 if (success && v instanceof BubbleTextView) {
2112 mWaitingForResume = (BubbleTextView) v;
2113 mWaitingForResume.setStayPressed(true);
2114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002116 if (v instanceof FolderIcon) {
2117 FolderIcon fi = (FolderIcon) v;
2118 handleFolderClick(fi);
2119 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002120 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002121 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002122 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002123 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002124 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127 }
2128
Michael Jurka0e260592010-06-30 17:07:39 -07002129 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002130 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002131 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002132 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2133 showWorkspace(true);
2134 }
Michael Jurka0e260592010-06-30 17:07:39 -07002135 return false;
2136 }
2137
Michael Jurkaaf442092010-06-10 17:01:57 -07002138 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002139 * Event handler for the search button
2140 *
2141 * @param v The view that was clicked.
2142 */
2143 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002144 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2145
Amith Yamasania135ba82011-08-09 17:42:01 -07002146 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002147 }
2148
2149 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002150 * Event handler for the voice button
2151 *
2152 * @param v The view that was clicked.
2153 */
2154 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002155 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002156
Bjorn Bringertc459e522013-06-07 19:36:01 +01002157 startVoice();
2158 }
2159
2160 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002161 try {
2162 final SearchManager searchManager =
2163 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2164 ComponentName activityName = searchManager.getGlobalSearchActivity();
2165 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002166 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002167 if (activityName != null) {
2168 intent.setPackage(activityName.getPackageName());
2169 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002170 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002171 } catch (ActivityNotFoundException e) {
2172 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002173 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002174 startActivitySafely(null, intent, "onClickVoiceButton");
2175 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002176 }
2177
2178 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002179 * Event handler for the "grid" button that appears on the home screen, which
2180 * enters all apps mode.
2181 *
2182 * @param v The view that was clicked.
2183 */
2184 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002185 showAllApps(true);
2186 }
2187
2188 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002189 // Provide the same haptic feedback that the system offers for virtual keys.
2190 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002191 }
2192
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002193 public void onClickAppMarketButton(View v) {
2194 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002195 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002196 } else {
2197 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002198 }
2199 }
2200
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002201 void startApplicationDetailsActivity(ComponentName componentName) {
2202 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002203 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2204 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002205 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002206 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002207 }
2208
Michael Jurka1e2f4652013-07-08 18:03:46 -07002209 // returns true if the activity was started
2210 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2211 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002212 // System applications cannot be installed. For now, show a toast explaining that.
2213 // We may give them the option of disabling apps this way.
2214 int messageId = R.string.uninstall_system_app_text;
2215 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002216 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002217 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002218 String packageName = componentName.getPackageName();
2219 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002220 Intent intent = new Intent(
2221 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002222 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2223 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002224 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002225 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002226 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002227 }
2228
Michael Jurka86a720e2012-05-09 11:23:23 -07002229 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002233 // Only launch using the new animation if the shortcut has not opted out (this is a
2234 // private contract between launcher and may be ignored in the future).
2235 boolean useLaunchAnimation = (v != null) &&
2236 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2237 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002238 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2239 v.getMeasuredWidth(), v.getMeasuredHeight());
2240
2241 startActivity(intent, opts.toBundle());
2242 } else {
2243 startActivity(intent);
2244 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002245 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 } catch (SecurityException e) {
2247 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002248 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002250 "or use the exported attribute for this activity. "
2251 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002253 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002255
Michael Jurka86a720e2012-05-09 11:23:23 -07002256 boolean startActivitySafely(View v, Intent intent, Object tag) {
2257 boolean success = false;
2258 try {
2259 success = startActivity(v, intent, tag);
2260 } catch (ActivityNotFoundException e) {
2261 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2262 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2263 }
2264 return success;
2265 }
2266
Romain Guy8e633c52010-03-04 12:51:36 -08002267 void startActivityForResultSafely(Intent intent, int requestCode) {
2268 try {
2269 startActivityForResult(intent, requestCode);
2270 } catch (ActivityNotFoundException e) {
2271 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2272 } catch (SecurityException e) {
2273 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2274 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2275 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2276 "or use the exported attribute for this activity.", e);
2277 }
2278 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279
Adam Cohena9cf38f2011-05-02 15:36:58 -07002280 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002281 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002282 Folder openFolder = mWorkspace.getFolderForTag(info);
2283
2284 // If the folder info reports that the associated folder is open, then verify that
2285 // it is actually opened. There have been a few instances where this gets out of sync.
2286 if (info.opened && openFolder == null) {
2287 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002288 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002289 info.opened = false;
2290 }
2291
Adam Cohenfb91f302012-06-11 15:45:18 -07002292 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 // Close any open folder
2294 closeFolder();
2295 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002296 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 } else {
2298 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 int folderScreen;
2300 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002301 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 // .. and close it
2303 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002304 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 // Close any folder open on the current screen
2306 closeFolder();
2307 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002308 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 }
2310 }
2311 }
2312 }
2313
Adam Cohen268c4752012-06-06 17:47:33 -07002314 /**
2315 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2316 * in the DragLayer in the exact absolute location of the original FolderIcon.
2317 */
2318 private void copyFolderIconToImage(FolderIcon fi) {
2319 final int width = fi.getMeasuredWidth();
2320 final int height = fi.getMeasuredHeight();
2321
2322 // Lazy load ImageView, Bitmap and Canvas
2323 if (mFolderIconImageView == null) {
2324 mFolderIconImageView = new ImageView(this);
2325 }
2326 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2327 mFolderIconBitmap.getHeight() != height) {
2328 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2329 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2330 }
2331
2332 DragLayer.LayoutParams lp;
2333 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2334 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2335 } else {
2336 lp = new DragLayer.LayoutParams(width, height);
2337 }
2338
Adam Cohen307fe232012-08-16 17:55:58 -07002339 // The layout from which the folder is being opened may be scaled, adjust the starting
2340 // view size by this scale factor.
2341 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002342 lp.customPosition = true;
2343 lp.x = mRectForFolderAnimation.left;
2344 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002345 lp.width = (int) (scale * width);
2346 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002347
2348 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2349 fi.draw(mFolderIconCanvas);
2350 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002351 if (fi.getFolder() != null) {
2352 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2353 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002354 }
Adam Cohen268c4752012-06-06 17:47:33 -07002355 // Just in case this image view is still in the drag layer from a previous animation,
2356 // we remove it and re-add it.
2357 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2358 mDragLayer.removeView(mFolderIconImageView);
2359 }
2360 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002361 if (fi.getFolder() != null) {
2362 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002363 }
Adam Cohen268c4752012-06-06 17:47:33 -07002364 }
2365
Adam Cohen2801caf2011-05-13 20:57:39 -07002366 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002367 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002368 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2369 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2370 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2371
Adam Cohenc51934b2011-07-26 21:07:43 -07002372 FolderInfo info = (FolderInfo) fi.getTag();
2373 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2374 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002375 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2376 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002377 }
2378
Adam Cohen268c4752012-06-06 17:47:33 -07002379 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2380 copyFolderIconToImage(fi);
2381 fi.setVisibility(View.INVISIBLE);
2382
Michael Jurka2ecf9952012-06-18 12:52:28 -07002383 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002384 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002385 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2386 oa.start();
2387 }
2388
Adam Cohen268c4752012-06-06 17:47:33 -07002389 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002390 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002391 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2392 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2393 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2394
Adam Cohen268c4752012-06-06 17:47:33 -07002395 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002396
Adam Cohen268c4752012-06-06 17:47:33 -07002397 // We remove and re-draw the FolderIcon in-case it has changed
2398 mDragLayer.removeView(mFolderIconImageView);
2399 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002400 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002401 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002402 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002403 oa.addListener(new AnimatorListenerAdapter() {
2404 @Override
2405 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002406 if (cl != null) {
2407 cl.clearFolderLeaveBehind();
2408 // Remove the ImageView copy of the FolderIcon and make the original visible.
2409 mDragLayer.removeView(mFolderIconImageView);
2410 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002411 }
2412 }
2413 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002414 oa.start();
2415 }
2416
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002418 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 * is animated relative to the specified View. If the View is null, no animation
2420 * is played.
2421 *
2422 * @param folderInfo The FolderInfo describing the folder to open.
2423 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002424 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002425 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002426 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427
Adam Cohena9cf38f2011-05-02 15:36:58 -07002428 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429
Adam Cohen4554ee12011-08-03 16:13:21 -07002430 // Just verify that the folder hasn't already been added to the DragLayer.
2431 // There was a one-off crash where the folder had a parent already.
2432 if (folder.getParent() == null) {
2433 mDragLayer.addView(folder);
2434 mDragController.addDropTarget((DropTarget) folder);
2435 } else {
2436 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2437 folder.getParent() + ").");
2438 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002439 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002440 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002441
2442 // Notify the accessibility manager that this folder "window" has appeared and occluded
2443 // the workspace items
2444 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2445 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002446 }
2447
2448 public void closeFolder() {
2449 Folder folder = mWorkspace.getOpenFolder();
2450 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002451 if (folder.isEditingName()) {
2452 folder.dismissEditingName();
2453 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002454 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002455
2456 // Dismiss the folder cling
2457 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002458 }
2459 }
2460
2461 void closeFolder(Folder folder) {
2462 folder.getInfo().opened = false;
2463
2464 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2465 if (parent != null) {
2466 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2467 shrinkAndFadeInFolderIcon(fi);
2468 }
2469 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002470
2471 // Notify the accessibility manager that this folder "window" has disappeard and no
2472 // longer occludeds the workspace items
2473 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 }
2475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002477 if (!isDraggingEnabled()) return false;
2478 if (isWorkspaceLocked()) return false;
2479 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480
2481 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002482 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 }
2484
Michael Jurka0280c3b2010-09-17 15:00:07 -07002485 resetAddInfo();
2486 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002488 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 return true;
2490 }
2491
Winson Chung3d503fb2011-07-13 17:25:49 -07002492 // The hotseat touch handling does not go through Workspace, and we always allow long press
2493 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002494 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002495 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2496 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002497 if (itemUnderLongClick == null) {
2498 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002499 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2500 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002501 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002502 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2503 mWorkspace.startReordering();
2504 } else {
2505 startWallpaper();
2506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002508 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002510 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 }
2512 }
2513 }
2514 return true;
2515 }
2516
Winson Chung3d503fb2011-07-13 17:25:49 -07002517 boolean isHotseatLayout(View layout) {
2518 return mHotseat != null && layout != null &&
2519 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2520 }
2521 Hotseat getHotseat() {
2522 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002523 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002524 SearchDropTargetBar getSearchBar() {
2525 return mSearchDropTargetBar;
2526 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002527
Winson Chung3d503fb2011-07-13 17:25:49 -07002528 /**
2529 * Returns the CellLayout of the specified container at the specified screen.
2530 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002531 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002532 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2533 if (mHotseat != null) {
2534 return mHotseat.getLayout();
2535 } else {
2536 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002537 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002538 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002539 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002540 }
2541 }
2542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 Workspace getWorkspace() {
2544 return mWorkspace;
2545 }
2546
Daniel Sandler843e8602010-06-07 14:59:01 -04002547 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002548 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002549 }
2550
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002551 /**
2552 * Helper method for the cameraZoomIn/cameraZoomOut animations
2553 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002554 * @param scaleFactor The scale factor used for the zoom
2555 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002556 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002557 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002558 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002559 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002560
Winson Chung18f41f82012-05-09 13:28:10 -07002561 void disableWallpaperIfInAllApps() {
2562 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002563 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002564 if (mAppsCustomizeTabHost != null &&
2565 !mAppsCustomizeTabHost.isTransitioning()) {
2566 updateWallpaperVisibility(false);
2567 }
2568 }
2569 }
2570
Craig Mautner360310b2012-10-26 15:13:08 -07002571 private void setWorkspaceBackground(boolean workspace) {
2572 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002573 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002574 }
2575
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002576 void updateWallpaperVisibility(boolean visible) {
2577 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2578 int curflags = getWindow().getAttributes().flags
2579 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2580 if (wpflags != curflags) {
2581 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2582 }
Craig Mautner360310b2012-10-26 15:13:08 -07002583 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002584 }
2585
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002586 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2587 if (v instanceof LauncherTransitionable) {
2588 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2589 }
2590 }
2591
Michael Jurkabed61d22012-02-14 22:51:29 -08002592 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2593 if (v instanceof LauncherTransitionable) {
2594 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2595 }
Winson Chung70442722012-02-10 15:43:22 -08002596
2597 // Update the workspace transition step as well
2598 dispatchOnLauncherTransitionStep(v, 0f);
2599 }
2600
2601 private void dispatchOnLauncherTransitionStep(View v, float t) {
2602 if (v instanceof LauncherTransitionable) {
2603 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2604 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002605 }
2606
2607 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2608 if (v instanceof LauncherTransitionable) {
2609 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2610 }
Winson Chung70442722012-02-10 15:43:22 -08002611
2612 // Update the workspace transition step as well
2613 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002614 }
2615
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002616 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002617 * Things to test when changing the following seven functions.
2618 * - Home from workspace
2619 * - from center screen
2620 * - from other screens
2621 * - Home from all apps
2622 * - from center screen
2623 * - from other screens
2624 * - Back from all apps
2625 * - from center screen
2626 * - from other screens
2627 * - Launch app from workspace and quit
2628 * - with back
2629 * - with home
2630 * - Launch app from all apps and quit
2631 * - with back
2632 * - with home
2633 * - Go to a screen that's not the default, then all
2634 * apps, and launch and app, and go back
2635 * - with back
2636 * -with home
2637 * - On workspace, long press power and go back
2638 * - with back
2639 * - with home
2640 * - On all apps, long press power and go back
2641 * - with back
2642 * - with home
2643 * - On workspace, power off
2644 * - On all apps, power off
2645 * - Launch an app and turn off the screen while in that app
2646 * - Go back with home key
2647 * - Go back with back key TODO: make this not go to workspace
2648 * - From all apps
2649 * - From workspace
2650 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2651 * - From all apps
2652 * - From the center workspace
2653 * - From another workspace
2654 */
2655
2656 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002657 * Zoom the camera out from the workspace to reveal 'toView'.
2658 * Assumes that the view to show is anchored at either the very top or very bottom
2659 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002660 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002661 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002662 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002663 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002664 mStateAnimation.cancel();
2665 mStateAnimation = null;
2666 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002667 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002668
Winson Chungf0ea4d32011-06-06 14:27:16 -07002669 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2670 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2671 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002672 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002673 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002674 final int startDelay =
2675 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002676
Michael Jurkab3e22d92011-10-31 15:58:33 -07002677 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002678
Michael Jurkad74c9842011-07-10 12:44:21 -07002679 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002680 Animator workspaceAnim =
2681 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002682
2683 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002684 toView.setScaleX(scale);
2685 toView.setScaleY(scale);
2686 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2687 scaleAnim.
2688 scaleX(1f).scaleY(1f).
2689 setDuration(duration).
2690 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002691
Winson Chungf0ea4d32011-06-06 14:27:16 -07002692 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002693 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002694 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002695 .ofFloat(toView, "alpha", 0f, 1f)
2696 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002697 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002698 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2699 @Override
2700 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002701 if (animation == null) {
2702 throw new RuntimeException("animation is null");
2703 }
Winson Chung70442722012-02-10 15:43:22 -08002704 float t = (Float) animation.getAnimatedValue();
2705 dispatchOnLauncherTransitionStep(fromView, t);
2706 dispatchOnLauncherTransitionStep(toView, t);
2707 }
2708 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002709
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002710 // toView should appear right at the end of the workspace shrink
2711 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002712 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002713 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002714 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002715
2716 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002717 boolean animationCancelled = false;
2718
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002719 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002720 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002721 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002722 // Prepare the position
2723 toView.setTranslationX(0.0f);
2724 toView.setTranslationY(0.0f);
2725 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002726 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002727 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002728 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002729 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002730 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2731 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002732
Daniel Sandlere4f98912013-06-25 15:13:26 -04002733 if (mWorkspace != null
2734 && !springLoaded
2735 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002736 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002737 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002738 if (!animationCancelled) {
2739 updateWallpaperVisibility(false);
2740 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002741
2742 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002743 if (mSearchDropTargetBar != null) {
2744 mSearchDropTargetBar.hideSearchBar(false);
2745 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002746 }
2747
Adam Cohencff6af82011-09-13 14:51:53 -07002748 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002749 public void onAnimationCancel(Animator animation) {
2750 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002751 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002752 });
2753
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002754 if (workspaceAnim != null) {
2755 mStateAnimation.play(workspaceAnim);
2756 }
Michael Jurka1899a362011-11-03 13:50:45 -07002757
2758 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002759
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002760 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2761 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002762
2763 // If any of the objects being animated haven't been measured/laid out
2764 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002765 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002766 (mWorkspace.getMeasuredWidth() == 0) ||
2767 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002768 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002769 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002770
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002771 final AnimatorSet stateAnimation = mStateAnimation;
2772 final Runnable startAnimRunnable = new Runnable() {
2773 public void run() {
2774 // Check that mStateAnimation hasn't changed while
2775 // we waited for a layout/draw pass
2776 if (mStateAnimation != stateAnimation)
2777 return;
2778 setPivotsForZoom(toView, scale);
2779 dispatchOnLauncherTransitionStart(fromView, animated, false);
2780 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002781 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002782 }
2783 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002784 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002785 final ViewTreeObserver observer = toView.getViewTreeObserver();
2786 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2787 public void onGlobalLayout() {
2788 startAnimRunnable.run();
2789 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2790 }
2791 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002792 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002793 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002794 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002795 } else {
2796 toView.setTranslationX(0.0f);
2797 toView.setTranslationY(0.0f);
2798 toView.setScaleX(1.0f);
2799 toView.setScaleY(1.0f);
2800 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002801 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002802
Daniel Sandlere4f98912013-06-25 15:13:26 -04002803 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002804 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002805
2806 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002807 if (mSearchDropTargetBar != null) {
2808 mSearchDropTargetBar.hideSearchBar(false);
2809 }
Michael Jurkaabded662011-03-04 12:06:57 -08002810 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002811 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002812 dispatchOnLauncherTransitionStart(fromView, animated, false);
2813 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002814 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002815 dispatchOnLauncherTransitionStart(toView, animated, false);
2816 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002817 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002818 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002819 }
2820
2821 /**
2822 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002823 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002824 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002825 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002826 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2827 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002828
Michael Jurkab3e22d92011-10-31 15:58:33 -07002829 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002830 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002831 mStateAnimation.cancel();
2832 mStateAnimation = null;
2833 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002834 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002835
Winson Chungf0ea4d32011-06-06 14:27:16 -07002836 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002837 final int fadeOutDuration =
2838 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002839 final float scaleFactor = (float)
2840 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2841 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002842 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002843 Animator workspaceAnim = null;
2844
2845 if (toState == State.WORKSPACE) {
2846 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2847 workspaceAnim = mWorkspace.getChangeStateAnimation(
2848 Workspace.State.NORMAL, animated, stagger);
2849 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2850 workspaceAnim = mWorkspace.getChangeStateAnimation(
2851 Workspace.State.SPRING_LOADED, animated);
2852 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002853
Michael Jurkab3e22d92011-10-31 15:58:33 -07002854 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002855 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002856 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002857 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002858 final LauncherViewPropertyAnimator scaleAnim =
2859 new LauncherViewPropertyAnimator(fromView);
2860 scaleAnim.
2861 scaleX(scaleFactor).scaleY(scaleFactor).
2862 setDuration(duration).
2863 setInterpolator(new Workspace.ZoomInInterpolator());
2864
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002865 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002866 .ofFloat(fromView, "alpha", 1f, 0f)
2867 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002868 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002869 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2870 @Override
2871 public void onAnimationUpdate(ValueAnimator animation) {
2872 float t = 1f - (Float) animation.getAnimatedValue();
2873 dispatchOnLauncherTransitionStep(fromView, t);
2874 dispatchOnLauncherTransitionStep(toView, t);
2875 }
2876 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002877
Michael Jurka2ecf9952012-06-18 12:52:28 -07002878 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002879
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002880 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2881 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002882 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002883
2884 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002885 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002886 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002887 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002888 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002889 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2890 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002891 if (onCompleteRunnable != null) {
2892 onCompleteRunnable.run();
2893 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002894 mAppsCustomizeContent.updateCurrentPageScroll();
2895 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002896 }
2897 });
2898
Winson Chungf0ea4d32011-06-06 14:27:16 -07002899 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002900 if (workspaceAnim != null) {
2901 mStateAnimation.play(workspaceAnim);
2902 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002903 dispatchOnLauncherTransitionStart(fromView, animated, true);
2904 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002905 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002906 } else {
2907 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002908 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002909 dispatchOnLauncherTransitionStart(fromView, animated, true);
2910 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002911 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002912 dispatchOnLauncherTransitionStart(toView, animated, true);
2913 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002914 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002915 }
2916
Michael Jurkae326f182011-11-21 14:05:46 -08002917 @Override
2918 public void onTrimMemory(int level) {
2919 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002920 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002921 mAppsCustomizeTabHost.onTrimMemory();
2922 }
2923 }
2924
Winson Chung18f41f82012-05-09 13:28:10 -07002925 @Override
2926 public void onWindowFocusChanged(boolean hasFocus) {
2927 if (!hasFocus) {
2928 // When another window occludes launcher (like the notification shade, or recents),
2929 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2930 updateWallpaperVisibility(true);
2931 } else {
2932 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002933 mWorkspace.postDelayed(new Runnable() {
2934 @Override
2935 public void run() {
2936 disableWallpaperIfInAllApps();
2937 }
2938 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002939 }
2940 }
2941
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002942 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002943 showWorkspace(animated, null);
2944 }
2945
2946 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002947 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002948 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002949 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002950 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002951
Winson Chungc7d2b602012-05-16 17:02:20 -07002952 // Show the search bar (only animate if we were showing the drop target bar in spring
2953 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002954 if (mSearchDropTargetBar != null) {
2955 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2956 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002957
Michael Jurkab737ee62011-11-15 15:57:22 -08002958 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002959 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002960
2961 // Set focus to the AppsCustomize button
2962 if (mAllAppsButton != null) {
2963 mAllAppsButton.requestFocus();
2964 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002965 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002966
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002967 // Change the state *after* we've called all the transition code
2968 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002969
Winson Chung5fb63472011-02-02 17:03:37 -08002970 // Resume the auto-advance of widgets
2971 mUserPresent = true;
2972 updateRunning();
2973
alanv1d4fde62012-10-17 13:15:47 -07002974 // Send an accessibility event to announce the context change
2975 getWindow().getDecorView()
2976 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002977
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002978 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002979 }
2980
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002981 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002982 }
2983
Michael Jurkab3e22d92011-10-31 15:58:33 -07002984 void showAllApps(boolean animated) {
2985 if (mState != State.WORKSPACE) return;
2986
2987 showAppsCustomizeHelper(animated, false);
2988 mAppsCustomizeTabHost.requestFocus();
2989
Michael Jurkab3e22d92011-10-31 15:58:33 -07002990 // Change the state *after* we've called all the transition code
2991 mState = State.APPS_CUSTOMIZE;
2992
2993 // Pause the auto-advance of widgets until we are out of AllApps
2994 mUserPresent = false;
2995 updateRunning();
2996 closeFolder();
2997
2998 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002999 getWindow().getDecorView()
3000 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003001 }
3002
Adam Cohen7777d962011-08-18 18:58:38 -07003003 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003004 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003005 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08003006 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07003007 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003008 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003009 }
Adam Cohen7777d962011-08-18 18:58:38 -07003010
Adam Cohened66b2b2012-01-23 17:28:51 -08003011 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3012 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003013 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3014
Winson Chunge7a03942011-08-05 15:05:12 -07003015 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003016 @Override
3017 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003018 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003019 // Before we show workspace, hide all apps again because
3020 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3021 // clean up our state transition functions
3022 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003023 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003024 } else {
3025 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003026 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003027 }
3028 }, (extendedDelay ?
3029 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3030 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3031 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003032
Michael Jurkad3ef3062010-11-23 16:23:58 -08003033 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003034 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003035 final boolean animated = true;
3036 final boolean springLoaded = true;
3037 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003038 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003039 }
3040 // Otherwise, we are not in spring loaded mode, so don't do anything.
3041 }
3042
Michael Jurkab737ee62011-11-15 15:57:22 -08003043 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003044 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003045 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003046 }
3047 }
3048
3049 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003050 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003051 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003052 if (mDividerAnimator != null) {
3053 mDividerAnimator.cancel();
3054 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003055 mDividerAnimator = null;
3056 }
3057 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003058 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003059 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003060 int duration = 0;
3061 if (mSearchDropTargetBar != null) {
3062 duration = mSearchDropTargetBar.getTransitionInDuration();
3063 }
3064 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003065 mDividerAnimator.start();
3066 }
3067 }
3068 }
3069
Michael Jurkab3e22d92011-10-31 15:58:33 -07003070 void lockAllApps() {
3071 // TODO
3072 }
3073
3074 void unlockAllApps() {
3075 // TODO
3076 }
3077
Winson Chungf0ea4d32011-06-06 14:27:16 -07003078 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003079 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003080 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003081 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003082 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003083 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003084 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003085 int duration = 0;
3086 if (mSearchDropTargetBar != null) {
3087 duration = mSearchDropTargetBar.getTransitionInDuration();
3088 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003089 mHotseat.animate().alpha(1f).setDuration(duration);
3090 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003091 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003092 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003093 }
3094 }
3095 }
3096
3097 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003098 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003099 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003100 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003101 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003102 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003103 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003104 int duration = 0;
3105 if (mSearchDropTargetBar != null) {
3106 duration = mSearchDropTargetBar.getTransitionOutDuration();
3107 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003108 mHotseat.animate().alpha(0f).setDuration(duration);
3109 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003110 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003111 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003112 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003113 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003114 }
3115
Patrick Dubroy5f445422011-02-18 14:35:21 -08003116 /**
3117 * Add an item from all apps or customize onto the given workspace screen.
3118 * If layout is null, add to the current screen.
3119 */
3120 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003121 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003122 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003123 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003124 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003125
Winson Chungdff8ebb2011-09-08 17:25:31 -07003126 /** Maps the current orientation to an index for referencing orientation correct global icons */
3127 private int getCurrentOrientationIndexForGlobalIcons() {
3128 // default - 0, landscape - 1
3129 switch (getResources().getConfiguration().orientation) {
3130 case Configuration.ORIENTATION_LANDSCAPE:
3131 return 1;
3132 default:
3133 return 0;
3134 }
3135 }
3136
Michael Jurkaae65ee32012-04-30 11:45:54 -07003137 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003138 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003139 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003140 // Look for the toolbar icon specified in the activity meta-data
3141 Bundle metaData = packageManager.getActivityInfo(
3142 activityName, PackageManager.GET_META_DATA).metaData;
3143 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003144 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003145 if (iconResId != 0) {
3146 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003147 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003148 }
3149 }
3150 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003151 // This can happen if the activity defines an invalid drawable
3152 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3153 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003154 } catch (Resources.NotFoundException nfe) {
3155 // This can happen if the activity defines an invalid drawable
3156 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3157 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003158 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003159 return null;
3160 }
3161
3162 // if successful in getting icon, return it; otherwise, set button to use default drawable
3163 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003164 int buttonId, ComponentName activityName, int fallbackDrawableId,
3165 String toolbarResourceName) {
3166 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003167 Resources r = getResources();
3168 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3169 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003170
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003171 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003172 // If we were unable to find the icon via the meta-data, use a generic one
3173 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003174 toolbarIcon = r.getDrawable(fallbackDrawableId);
3175 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003176 if (button != null) {
3177 button.setCompoundDrawables(toolbarIcon, null, null, null);
3178 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003179 return null;
3180 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003181 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003182 if (button != null) {
3183 button.setCompoundDrawables(toolbarIcon, null, null, null);
3184 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003185 return toolbarIcon.getConstantState();
3186 }
3187 }
3188
3189 // if successful in getting icon, return it; otherwise, set button to use default drawable
3190 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003191 int buttonId, ComponentName activityName, int fallbackDrawableId,
3192 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003193 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003194 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003195
Michael Jurka19e0fc52011-07-22 18:00:21 -07003196 if (button != null) {
3197 // If we were unable to find the icon via the meta-data, use a
3198 // generic one
3199 if (toolbarIcon == null) {
3200 button.setImageResource(fallbackDrawableId);
3201 } else {
3202 button.setImageDrawable(toolbarIcon);
3203 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003204 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003205
3206 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3207
Michael Jurka0423dcf2010-10-05 14:56:18 -07003208 }
3209
Winson Chung1b884092012-06-08 17:13:02 -07003210 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003211 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003212 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003213 }
3214
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003215 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003216 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003217 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003218 }
3219
Winson Chungbb185bd2011-11-21 12:31:42 -08003220 private void invalidatePressedFocusedStates(View container, View button) {
3221 if (container instanceof HolographicLinearLayout) {
3222 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3223 layout.invalidatePressedFocusedStates();
3224 } else if (button instanceof HolographicImageView) {
3225 HolographicImageView view = (HolographicImageView) button;
3226 view.invalidatePressedFocusedStates();
3227 }
3228 }
3229
Winson Chungc51db6a2011-10-05 11:44:49 -07003230 private boolean updateGlobalSearchIcon() {
3231 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003232 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003233 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003234 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003235
Winson Chung1cad91e2011-05-25 17:41:01 -07003236 final SearchManager searchManager =
3237 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3238 ComponentName activityName = searchManager.getGlobalSearchActivity();
3239 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003240 int coi = getCurrentOrientationIndexForGlobalIcons();
3241 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003242 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3243 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3244 if (sGlobalSearchIcon[coi] == null) {
3245 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3246 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3247 TOOLBAR_ICON_METADATA_NAME);
3248 }
3249
Winson Chungc51db6a2011-10-05 11:44:49 -07003250 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3251 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003252 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003253 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003254 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003255 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003256 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3257 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3258 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003259 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003260 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003262 }
3263 }
3264
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003265 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003266 final View searchButtonContainer = findViewById(R.id.search_button_container);
3267 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003268 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003269 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003270 }
3271
Winson Chungc51db6a2011-10-05 11:44:49 -07003272 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003273 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003274 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003275
Winson Chungc51db6a2011-10-05 11:44:49 -07003276 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003277 final SearchManager searchManager =
3278 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3279 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3280
3281 ComponentName activityName = null;
3282 if (globalSearchActivity != null) {
3283 // Check if the global search activity handles voice search
3284 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3285 intent.setPackage(globalSearchActivity.getPackageName());
3286 activityName = intent.resolveActivity(getPackageManager());
3287 }
3288
3289 if (activityName == null) {
3290 // Fallback: check if an activity other than the global search activity
3291 // resolves this
3292 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3293 activityName = intent.resolveActivity(getPackageManager());
3294 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003295 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003296 int coi = getCurrentOrientationIndexForGlobalIcons();
3297 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003298 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3299 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3300 if (sVoiceSearchIcon[coi] == null) {
3301 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3302 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3303 TOOLBAR_ICON_METADATA_NAME);
3304 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003305 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003306 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003307 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003308 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003309 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003310 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003311 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003312 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003313 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003314 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003315 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003316 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003317
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003318 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003319 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3320 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003321 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003322 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003323 }
3324
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003325 public void setVoiceButtonProxyVisible(boolean visible) {
3326 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3327 if (voiceButtonProxy != null) {
3328 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3329 }
3330 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003331 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003332 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003333 */
3334 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003335 final View marketButton = findViewById(R.id.market_button);
3336 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3337 // Find the app market activity by resolving an intent.
3338 // (If multiple app markets are installed, it will return the ResolverActivity.)
3339 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003340 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003341 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003342 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003343 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003344 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3345 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003346 marketButton.setVisibility(View.VISIBLE);
3347 } else {
3348 // We should hide and disable the view so that we don't try and restore the visibility
3349 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3350 marketButton.setVisibility(View.GONE);
3351 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003352 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003353 }
3354
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003355 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003356 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3357 Resources r = getResources();
3358 Drawable marketIconDrawable = d.newDrawable(r);
3359 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3360 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3361 marketIconDrawable.setBounds(0, 0, w, h);
3362
3363 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003364 }
3365
Michael Jurkad7c28052012-04-27 15:43:36 -07003366 @Override
3367 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003368 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003369 final List<CharSequence> text = event.getText();
3370 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003371 // Populate event with a fake title based on the current state.
3372 if (mState == State.APPS_CUSTOMIZE) {
3373 text.add(getString(R.string.all_apps_button_label));
3374 } else {
3375 text.add(getString(R.string.all_apps_home_button_label));
3376 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003377 return result;
3378 }
3379
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003380 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003381 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003382 */
3383 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3384 @Override
3385 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003386 closeSystemDialogs();
3387 }
3388 }
3389
3390 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003391 * Receives notifications whenever the appwidgets are reset.
3392 */
3393 private class AppWidgetResetObserver extends ContentObserver {
3394 public AppWidgetResetObserver() {
3395 super(new Handler());
3396 }
3397
3398 @Override
3399 public void onChange(boolean selfChange) {
3400 onAppWidgetReset();
3401 }
3402 }
3403
3404 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003405 * If the activity is currently paused, signal that we need to run the passed Runnable
3406 * in onResume.
3407 *
3408 * This needs to be called from incoming places where resources might have been loaded
3409 * while we are paused. That is becaues the Configuration might be wrong
3410 * when we're not running, and if it comes back to what it was when we
3411 * were paused, we are not restarted.
3412 *
3413 * Implementation of the method from LauncherModel.Callbacks.
3414 *
3415 * @return true if we are currently paused. The caller might be able to
3416 * skip some work in that case since we will come back again.
3417 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003418 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003419 if (mPaused) {
3420 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003421 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003422 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003423 }
3424 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003425 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003426 return true;
3427 } else {
3428 return false;
3429 }
3430 }
3431
Michael Jurkac402cd92013-05-20 15:49:32 +02003432 private boolean waitUntilResume(Runnable run) {
3433 return waitUntilResume(run, false);
3434 }
3435
Michael Jurka1e2f4652013-07-08 18:03:46 -07003436 public void addOnResumeCallback(Runnable run) {
3437 mBindOnResumeCallbacks.add(run);
3438 }
3439
3440 public void removeOnResumeCallback(Runnable run) {
3441 mBindOnResumeCallbacks.remove(run);
3442 }
3443
Michael Jurka7607c2f2013-04-03 14:33:19 -07003444 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003445 * If the activity is currently paused, signal that we need to re-run the loader
3446 * in onResume.
3447 *
3448 * This needs to be called from incoming places where resources might have been loaded
3449 * while we are paused. That is becaues the Configuration might be wrong
3450 * when we're not running, and if it comes back to what it was when we
3451 * were paused, we are not restarted.
3452 *
3453 * Implementation of the method from LauncherModel.Callbacks.
3454 *
3455 * @return true if we are currently paused. The caller might be able to
3456 * skip some work in that case since we will come back again.
3457 */
3458 public boolean setLoadOnResume() {
3459 if (mPaused) {
3460 Log.i(TAG, "setLoadOnResume");
3461 mOnResumeNeedsLoad = true;
3462 return true;
3463 } else {
3464 return false;
3465 }
3466 }
3467
3468 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003469 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003470 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003472 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003473 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003474 } else {
3475 return SCREEN_COUNT / 2;
3476 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003478
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 /**
3480 * Refreshes the shortcuts shown on the workspace.
3481 *
3482 * Implementation of the method from LauncherModel.Callbacks.
3483 */
3484 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003485 // If we're starting binding all over again, clear any bind calls we'd postponed in
3486 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3487 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003488 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003489
Michael Jurka7607c2f2013-04-03 14:33:19 -07003490 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003491 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003492 int count = workspace.getChildCount();
3493 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003494 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003495 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3496 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003498 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003499 if (mHotseat != null) {
3500 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003501 }
3502 }
3503
Adam Cohendcd297f2013-06-18 13:13:40 -07003504 @Override
3505 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003506 bindAddScreens(orderedScreenIds);
3507 mWorkspace.addExtraEmptyScreen();
3508 }
3509
3510 @Override
3511 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003512 int count = orderedScreenIds.size();
3513 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003514 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003515 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003516 }
3517
Adam Cohen39a06042013-07-19 14:30:12 -07003518 private boolean shouldShowWeightWatcher() {
3519 String spKey = LauncherAppState.getSharedPreferencesKey();
3520 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3521 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3522
3523 return show;
3524 }
3525
3526 private void toggleShowWeightWatcher() {
3527 String spKey = LauncherAppState.getSharedPreferencesKey();
3528 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3529 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3530
3531 show = !show;
3532
3533 SharedPreferences.Editor editor = sp.edit();
3534 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3535 editor.commit();
3536
3537 if (mWeightWatcher != null) {
3538 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3539 }
3540 }
3541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003542 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543 * Bind the items start-end from the list.
3544 *
3545 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003546 */
Winson Chung64359a52013-07-08 17:17:08 -07003547 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3548 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003549 if (waitUntilResume(new Runnable() {
3550 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003551 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003552 }
3553 })) {
3554 return;
3555 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003556
Winson Chungf0c6ae02012-03-21 16:10:31 -07003557 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003558 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3559 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003560 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003561 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003562 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003563 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003565
3566 // Short circuit if we are loading dock items for a configuration which has no dock
3567 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3568 mHotseat == null) {
3569 continue;
3570 }
3571
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 switch (item.itemType) {
3573 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3574 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003575 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003576 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003577
Winson Chung64359a52013-07-08 17:17:08 -07003578 /*
3579 * TODO: FIX collision case
3580 */
Winson Chungce376632013-07-11 16:12:41 -07003581 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3582 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3583 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3584 throw new RuntimeException("OCCUPIED");
3585 }
Winson Chung64359a52013-07-08 17:17:08 -07003586 }
3587
Adam Cohendcd297f2013-06-18 13:13:40 -07003588 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3589 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003590 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003591 // Animate all the applications up now
3592 shortcut.setAlpha(0f);
3593 shortcut.setScaleX(0f);
3594 shortcut.setScaleY(0f);
3595 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003596 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003597 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003599 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003600 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003601 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003602 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003603 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3604 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003607 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003608
Winson Chung997a9232013-07-24 15:33:46 -07003609 if (animateIcons) {
3610 // Animate to the correct page
3611 if (newShortcutsScreenId > -1) {
3612 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3613 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3614 if (newShortcutsScreenId != currentScreenId) {
3615 mWorkspace.snapToPage(newScreenIndex);
3616 }
3617 }
3618
Winson Chung64359a52013-07-08 17:17:08 -07003619 // We post the animation slightly delayed to prevent slowdowns when we are loading
3620 // right after we return to launcher.
3621 mWorkspace.postDelayed(new Runnable() {
3622 public void run() {
3623 anim.playTogether(bounceAnims);
3624 anim.start();
3625 }
3626 }, NEW_APPS_ANIMATION_DELAY);
3627 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003629 }
3630
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 /**
3632 * Implementation of the method from LauncherModel.Callbacks.
3633 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003634 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3635 if (waitUntilResume(new Runnable() {
3636 public void run() {
3637 bindFolders(folders);
3638 }
3639 })) {
3640 return;
3641 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003642 sFolders.clear();
3643 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003644 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003645
3646 /**
3647 * Add the views for a widget to the workspace.
3648 *
3649 * Implementation of the method from LauncherModel.Callbacks.
3650 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003651 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3652 if (waitUntilResume(new Runnable() {
3653 public void run() {
3654 bindAppWidget(item);
3655 }
3656 })) {
3657 return;
3658 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003659
Daniel Sandler843e8602010-06-07 14:59:01 -04003660 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3661 if (DEBUG_WIDGETS) {
3662 Log.d(TAG, "bindAppWidget: " + item);
3663 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 final Workspace workspace = mWorkspace;
3665
3666 final int appWidgetId = item.appWidgetId;
3667 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003668 if (DEBUG_WIDGETS) {
3669 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3670 }
3671
Joe Onorato9c1289c2009-08-17 11:03:03 -04003672 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3673
Joe Onorato9c1289c2009-08-17 11:03:03 -04003674 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003675 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003676
Adam Cohendcd297f2013-06-18 13:13:40 -07003677 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003678 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003679 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3680
Joe Onorato9c1289c2009-08-17 11:03:03 -04003681 workspace.requestLayout();
3682
Daniel Sandler843e8602010-06-07 14:59:01 -04003683 if (DEBUG_WIDGETS) {
3684 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3685 + (SystemClock.uptimeMillis()-start) + "ms");
3686 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003687 }
3688
Adam Cohen1462de32012-07-24 22:34:36 -07003689 public void onPageBoundSynchronously(int page) {
3690 mSynchronouslyBoundPages.add(page);
3691 }
3692
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 /**
3694 * Callback saying that there aren't any more items to bind.
3695 *
3696 * Implementation of the method from LauncherModel.Callbacks.
3697 */
Adam Cohene25af792013-06-06 23:08:25 -07003698 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003699 if (waitUntilResume(new Runnable() {
3700 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003701 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003702 }
3703 })) {
3704 return;
3705 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003706 if (mSavedState != null) {
3707 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003708 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003709 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003710 mSavedState = null;
3711 }
3712
Adam Cohen1462de32012-07-24 22:34:36 -07003713 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003714
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003715 // If we received the result of any pending adds while the loader was running (e.g. the
3716 // widget configuration forced an orientation change), process them now.
3717 for (int i = 0; i < sPendingAddList.size(); i++) {
3718 completeAdd(sPendingAddList.get(i));
3719 }
3720 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003721
Winson Chungc51db6a2011-10-05 11:44:49 -07003722 // Update the market app icon as necessary (the other icons will be managed in response to
3723 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003724 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003725
Winson Chungf0c6ae02012-03-21 16:10:31 -07003726 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003727 if (upgradePath) {
3728 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003729 mWorkspace.stripDuplicateApps();
3730 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003731 }
Adam Cohen99894d92013-06-14 11:22:59 -07003732
Adam Cohen66a01fd2013-06-11 12:48:00 -07003733 mWorkspace.post(new Runnable() {
3734 @Override
3735 public void run() {
3736 onFinishBindingItems();
3737 }
3738 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003739 }
3740
Winson Chunga2413752012-04-03 14:22:34 -07003741 private boolean canRunNewAppsAnimation() {
3742 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3743 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3744 }
3745
Winson Chungc9168342013-06-26 14:54:55 -07003746 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3747 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3748 PropertyValuesHolder.ofFloat("alpha", 1f),
3749 PropertyValuesHolder.ofFloat("scaleX", 1f),
3750 PropertyValuesHolder.ofFloat("scaleY", 1f));
3751 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3752 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3753 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3754 return bounceAnim;
3755 }
3756
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003757 @Override
3758 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003759 boolean searchVisible = updateGlobalSearchIcon();
3760 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003761 if (mSearchDropTargetBar != null) {
3762 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3763 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003764 }
3765
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003766 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003767 * Add the icons for all apps.
3768 *
3769 * Implementation of the method from LauncherModel.Callbacks.
3770 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003771 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003772 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3773 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003774 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003775 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003776 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003777 }
3778
3779 /**
3780 * A package was updated.
3781 *
3782 * Implementation of the method from LauncherModel.Callbacks.
3783 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003784 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3785 if (waitUntilResume(new Runnable() {
3786 public void run() {
3787 bindAppsUpdated(apps);
3788 }
3789 })) {
3790 return;
3791 }
3792
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003793 if (mWorkspace != null) {
3794 mWorkspace.updateShortcuts(apps);
3795 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 }
3797
3798 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003799 * A package was uninstalled. We take both the super set of packageNames
3800 * in addition to specific applications to remove, the reason being that
3801 * this can be called when a package is updated as well. In that scenario,
3802 * we only remove specific components from the workspace, where as
3803 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003804 *
3805 * Implementation of the method from LauncherModel.Callbacks.
3806 */
Winson Chung83892cc2013-05-01 16:53:33 -07003807 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3808 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003809 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003810 if (waitUntilResume(new Runnable() {
3811 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003812 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003813 }
3814 })) {
3815 return;
3816 }
3817
Winson Chung64359a52013-07-08 17:17:08 -07003818 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003819 mWorkspace.removeItemsByPackageName(packageNames);
3820 } else {
3821 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003822 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003823
Winson Chunga1820962011-10-03 16:31:06 -07003824 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003825 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003826 }
Joe Onoratobe386092009-11-17 17:32:16 -08003827
3828 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003829 * A number of packages were updated.
3830 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003831 private ArrayList<Object> mWidgetsAndShortcuts;
3832 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003833 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003834 bindPackagesUpdated(mWidgetsAndShortcuts);
3835 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003836 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003837 };
3838
3839 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3840 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3841 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003842 return;
3843 }
3844
Winson Chung64359a52013-07-08 17:17:08 -07003845 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003846 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003847 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003848 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003849 }
3850
Winson Chung400438b2011-01-16 17:53:48 -08003851 private int mapConfigurationOriActivityInfoOri(int configOri) {
3852 final Display d = getWindowManager().getDefaultDisplay();
3853 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3854 switch (d.getRotation()) {
3855 case Surface.ROTATION_0:
3856 case Surface.ROTATION_180:
3857 // We are currently in the same basic orientation as the natural orientation
3858 naturalOri = configOri;
3859 break;
3860 case Surface.ROTATION_90:
3861 case Surface.ROTATION_270:
3862 // We are currently in the other basic orientation to the natural orientation
3863 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3864 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3865 break;
3866 }
3867
3868 int[] oriMap = {
3869 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3870 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3871 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3872 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3873 };
3874 // Since the map starts at portrait, we need to offset if this device's natural orientation
3875 // is landscape.
3876 int indexOffset = 0;
3877 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3878 indexOffset = 1;
3879 }
3880 return oriMap[(d.getRotation() + indexOffset) % 4];
3881 }
Adam Cohen446e9402011-09-15 18:21:21 -07003882
Winson Chung4b919f82012-05-01 10:44:08 -07003883 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003884 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003885 getResources().getBoolean(R.bool.allow_rotation);
3886 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003887 }
Winson Chung4b919f82012-05-01 10:44:08 -07003888 public void lockScreenOrientation() {
3889 if (isRotationEnabled()) {
3890 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3891 .getConfiguration().orientation));
3892 }
3893 }
3894 public void unlockScreenOrientation(boolean immediate) {
3895 if (isRotationEnabled()) {
3896 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003897 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003898 } else {
3899 mHandler.postDelayed(new Runnable() {
3900 public void run() {
3901 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3902 }
3903 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003904 }
Winson Chung4b919f82012-05-01 10:44:08 -07003905 }
Winson Chung400438b2011-01-16 17:53:48 -08003906 }
3907
Winson Chung82f55532011-08-09 14:14:23 -07003908 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003909 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003910 if (DISABLE_CLINGS) {
3911 return false;
3912 }
3913
Brett Chabot2a9e2282011-08-23 15:03:13 -07003914 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003915 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003916
Michael Jurkae233a8b2013-03-19 13:49:20 +01003917 // Restricted secondary users (child mode) will potentially have very few apps
3918 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003919// boolean supportsLimitedUsers =
3920// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3921// Account[] accounts = AccountManager.get(this).getAccounts();
3922// if (supportsLimitedUsers && accounts.length == 0) {
3923// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3924// Bundle restrictions = um.getUserRestrictions();
3925// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3926// return false;
3927// }
3928// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003929 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003930 }
Michael Jurka22143132012-10-04 17:42:38 +02003931
Winson Chung7d7541e2011-09-16 20:14:36 -07003932 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003933 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003934 if (cling != null) {
3935 cling.init(this, positionData);
3936 cling.setVisibility(View.VISIBLE);
3937 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3938 if (animate) {
3939 cling.buildLayer();
3940 cling.setAlpha(0f);
3941 cling.animate()
3942 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003943 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003944 .setDuration(SHOW_CLING_DURATION)
3945 .setStartDelay(delay)
3946 .start();
3947 } else {
3948 cling.setAlpha(1f);
3949 }
Michael Jurka22143132012-10-04 17:42:38 +02003950 cling.setFocusableInTouchMode(true);
3951 cling.post(new Runnable() {
3952 public void run() {
3953 cling.setFocusable(true);
3954 cling.requestFocus();
3955 }
3956 });
3957 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3958 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003959 }
3960 return cling;
3961 }
Michael Jurka22143132012-10-04 17:42:38 +02003962
Winson Chung7d7541e2011-09-16 20:14:36 -07003963 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003964 // To catch cases where siblings of top-level views are made invisible, just check whether
3965 // the cling is directly set to GONE before dismissing it.
3966 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003967 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003968 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003969 anim.addListener(new AnimatorListenerAdapter() {
3970 public void onAnimationEnd(Animator animation) {
3971 cling.setVisibility(View.GONE);
3972 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003973 // We should update the shared preferences on a background thread
3974 new Thread("dismissClingThread") {
3975 public void run() {
3976 SharedPreferences.Editor editor = mSharedPrefs.edit();
3977 editor.putBoolean(flag, true);
3978 editor.commit();
3979 }
3980 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003981 };
3982 });
3983 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003984 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003985 }
3986 }
Michael Jurka22143132012-10-04 17:42:38 +02003987
Winson Chung9d9d74f2011-09-19 11:49:12 -07003988 private void removeCling(int id) {
3989 final View cling = findViewById(id);
3990 if (cling != null) {
3991 final ViewGroup parent = (ViewGroup) cling.getParent();
3992 parent.post(new Runnable() {
3993 @Override
3994 public void run() {
3995 parent.removeView(cling);
3996 }
3997 });
Michael Jurka22143132012-10-04 17:42:38 +02003998 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003999 }
4000 }
Michael Jurka974c3862012-05-22 22:00:31 -07004001
4002 private boolean skipCustomClingIfNoAccounts() {
4003 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4004 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4005 if (customCling) {
4006 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004007 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004008 Account[] accounts = am.getAccountsByType("com.google");
4009 return accounts.length == 0;
4010 }
4011 return false;
4012 }
4013
Winson Chung7d7541e2011-09-16 20:14:36 -07004014 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004015 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004016 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004017 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4018 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004019 // If we're not using the default workspace layout, replace workspace cling
4020 // with a custom workspace cling (usually specified in an overlay)
4021 // For now, only do this on tablets
4022 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004023 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004024 // Use a custom cling
4025 View cling = findViewById(R.id.workspace_cling);
4026 ViewGroup clingParent = (ViewGroup) cling.getParent();
4027 int clingIndex = clingParent.indexOfChild(cling);
4028 clingParent.removeViewAt(clingIndex);
4029 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4030 clingParent.addView(customCling, clingIndex);
4031 customCling.setId(R.id.workspace_cling);
4032 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004033 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004034 } else {
4035 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004036 }
4037 }
4038 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004039 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004040 if (isClingsEnabled() &&
4041 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004042 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004043 } else {
4044 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004045 }
4046 }
4047 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004048 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004049 if (isClingsEnabled() &&
4050 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4051 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004052 } else {
4053 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004054 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004055 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004056 }
4057 public boolean isFolderClingVisible() {
4058 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004059 if (cling != null) {
4060 return cling.getVisibility() == View.VISIBLE;
4061 }
4062 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004063 }
Winson Chung82f55532011-08-09 14:14:23 -07004064 public void dismissWorkspaceCling(View v) {
4065 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004066 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004067 }
4068 public void dismissAllAppsCling(View v) {
4069 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004070 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4071 }
4072 public void dismissFolderCling(View v) {
4073 Cling cling = (Cling) findViewById(R.id.folder_cling);
4074 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004075 }
4076
Winson Chung80baf5a2010-08-09 16:03:15 -07004077 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004078 * Prints out out state for debugging.
4079 */
4080 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004081 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004082 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004083 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4084 Log.d(TAG, "mRestoring=" + mRestoring);
4085 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4086 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004087 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004088 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004089
Winson Chung785d2eb2011-04-14 16:08:02 -07004090 if (mAppsCustomizeContent != null) {
4091 mAppsCustomizeContent.dumpState();
4092 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004093 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004094 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004095
4096 @Override
4097 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4098 super.dump(prefix, fd, writer, args);
4099 writer.println(" ");
4100 writer.println("Debug logs: ");
4101 for (int i = 0; i < sDumpLogs.size(); i++) {
4102 writer.println(" " + sDumpLogs.get(i));
4103 }
4104 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004105
4106 public static void dumpDebugLogsToConsole() {
4107 Log.d(TAG, "");
4108 Log.d(TAG, "*********************");
4109 Log.d(TAG, "Launcher debug logs: ");
4110 for (int i = 0; i < sDumpLogs.size(); i++) {
4111 Log.d(TAG, " " + sDumpLogs.get(i));
4112 }
4113 Log.d(TAG, "*********************");
4114 Log.d(TAG, "");
4115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004116}
Michael Jurka2763be32011-02-24 11:19:57 -08004117
Michael Jurkaabded662011-03-04 12:06:57 -08004118interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004119 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004120 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004121 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004122 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004123 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004124}