blob: 1f4c120426b48ffa560b12a6f09f1664e6b8f207 [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
Daniel Sandlerff02d492013-08-05 02:12:05 -0400346 private Stats mStats;
347
Michael Jurka0a457bf2012-11-19 14:05:05 -0800348 private static boolean isPropertyEnabled(String propertyName) {
349 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700350 }
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 @Override
353 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700354 if (DEBUG_STRICT_MODE) {
355 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
356 .detectDiskReads()
357 .detectDiskWrites()
358 .detectNetwork() // or .detectAll() for all detectable problems
359 .penaltyLog()
360 .build());
361 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
362 .detectLeakedSqlLiteObjects()
363 .detectLeakedClosableObjects()
364 .penaltyLog()
365 .penaltyDeath()
366 .build());
367 }
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400370
371 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
372 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400373 LauncherAppState app = LauncherAppState.getInstance();
374 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700375 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800376 mModel = app.setLauncher(this);
377 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400378 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700380
Daniel Sandlerff02d492013-08-05 02:12:05 -0400381 mStats = new Stats(this);
382
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700383 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700384 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
385 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700386
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700387 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
388 // this also ensures that any synchronous binding below doesn't re-trigger another
389 // LauncherModel load.
390 mPaused = false;
391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200393 android.os.Debug.startMethodTracing(
394 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
396
397 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 setContentView(R.layout.launcher);
399 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700400 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800402 registerContentObservers();
403
Joe Onorato7c312c12009-08-13 21:36:53 -0700404 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 mSavedState = savedInstanceState;
407 restoreState(mSavedState);
408
Winson Chunga12a2502010-12-20 14:41:35 -0800409 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700410 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200411 mAppsCustomizeContent.onPackagesUpdated(
412 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700413 }
Winson Chunga12a2502010-12-20 14:41:35 -0800414
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 if (PROFILE_STARTUP) {
416 android.os.Debug.stopMethodTracing();
417 }
418
419 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700420 if (sPausedFromUserAction) {
421 // If the user leaves launcher, then we should just load items asynchronously when
422 // they return.
423 mModel.startLoader(true, -1);
424 } else {
425 // We only load the page synchronously if the user rotates (or triggers a
426 // configuration change) while launcher is in the foreground
427 mModel.startLoader(true, mWorkspace.getCurrentPage());
428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 }
430
431 // For handling default keys
432 mDefaultKeySsb = new SpannableStringBuilder();
433 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800434
435 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
436 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800437
Adam Cohenf9426d52012-06-04 17:26:21 -0700438 updateGlobalIcons();
439
440 // On large interfaces, we want the screen to auto-rotate based on the current orientation
441 unlockScreenOrientation(true);
442 }
443
Winson Chung4a2afa32012-07-19 14:53:05 -0700444 protected void onUserLeaveHint() {
445 super.onUserLeaveHint();
446 sPausedFromUserAction = true;
447 }
448
Winson Chung98ca0f72013-07-29 12:58:51 -0700449 /** To be overriden by subclasses to hint to Launcher that we have custom content */
450 protected boolean hasCustomContentToLeft() {
451 return false;
452 }
453
Adam Cohenf9426d52012-06-04 17:26:21 -0700454 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700455 boolean searchVisible = false;
456 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800457 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700458 int coi = getCurrentOrientationIndexForGlobalIcons();
459 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
460 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700461 updateAppMarketIcon();
462 searchVisible = updateGlobalSearchIcon();
463 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700464 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700465 if (sGlobalSearchIcon[coi] != null) {
466 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700467 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700468 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700469 if (sVoiceSearchIcon[coi] != null) {
470 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700471 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700472 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700473 if (sAppMarketIcon[coi] != null) {
474 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800475 }
Winson Chungadf0c182012-08-23 14:59:07 -0700476 if (mSearchDropTargetBar != null) {
477 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
Romain Guycbb89e42009-06-08 15:52:54 -0700480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700482 if (sLocaleConfiguration == null) {
483 new AsyncTask<Void, Void, LocaleConfiguration>() {
484 @Override
485 protected LocaleConfiguration doInBackground(Void... unused) {
486 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
487 readConfiguration(Launcher.this, localeConfiguration);
488 return localeConfiguration;
489 }
490
491 @Override
492 protected void onPostExecute(LocaleConfiguration result) {
493 sLocaleConfiguration = result;
494 checkForLocaleChange(); // recursive, but now with a locale configuration
495 }
496 }.execute();
497 return;
498 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700499
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 final Configuration configuration = getResources().getConfiguration();
501
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700502 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503 final String locale = configuration.locale.toString();
504
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700505 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800506 final int mcc = configuration.mcc;
507
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700508 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 final int mnc = configuration.mnc;
510
Romain Guy84f296c2009-11-04 15:00:44 -0800511 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512
Romain Guy84f296c2009-11-04 15:00:44 -0800513 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700514 sLocaleConfiguration.locale = locale;
515 sLocaleConfiguration.mcc = mcc;
516 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700517
Joe Onorato0589f0f2010-02-08 13:44:00 -0800518 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700519
520 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
521 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700522 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700523 public void run() {
524 writeConfiguration(Launcher.this, localeConfiguration);
525 }
526 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700527 }
528 }
529
530 private static class LocaleConfiguration {
531 public String locale;
532 public int mcc = -1;
533 public int mnc = -1;
534 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700535
Romain Guy98d01652009-06-30 16:21:04 -0700536 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
537 DataInputStream in = null;
538 try {
539 in = new DataInputStream(context.openFileInput(PREFERENCES));
540 configuration.locale = in.readUTF();
541 configuration.mcc = in.readInt();
542 configuration.mnc = in.readInt();
543 } catch (FileNotFoundException e) {
544 // Ignore
545 } catch (IOException e) {
546 // Ignore
547 } finally {
548 if (in != null) {
549 try {
550 in.close();
551 } catch (IOException e) {
552 // Ignore
553 }
554 }
555 }
556 }
557
558 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
559 DataOutputStream out = null;
560 try {
561 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
562 out.writeUTF(configuration.locale);
563 out.writeInt(configuration.mcc);
564 out.writeInt(configuration.mnc);
565 out.flush();
566 } catch (FileNotFoundException e) {
567 // Ignore
568 } catch (IOException e) {
569 //noinspection ResultOfMethodCallIgnored
570 context.getFileStreamPath(PREFERENCES).delete();
571 } finally {
572 if (out != null) {
573 try {
574 out.close();
575 } catch (IOException e) {
576 // Ignore
577 }
578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
580 }
581
Bjorn Bringertc459e522013-06-07 19:36:01 +0100582 public LayoutInflater getInflater() {
583 return mInflater;
584 }
585
Adam Cohen716b51e2011-06-30 12:09:54 -0700586 public DragLayer getDragLayer() {
587 return mDragLayer;
588 }
589
Winson Chung36a62fe2012-05-06 18:04:42 -0700590 boolean isDraggingEnabled() {
591 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
592 // that is subsequently removed from the workspace in startBinding().
593 return !mModel.isLoadingWorkspace();
594 }
595
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 static int getScreen() {
597 synchronized (sLock) {
598 return sScreen;
599 }
600 }
601
602 static void setScreen(int screen) {
603 synchronized (sLock) {
604 sScreen = screen;
605 }
606 }
607
Winson Chung557d6ed2011-07-08 15:34:52 -0700608 /**
609 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
610 * a configuration step, this allows the proper animations to run after other transitions.
611 */
612 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700613 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800614 switch (args.requestCode) {
615 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700616 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700617 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 break;
619 case REQUEST_PICK_SHORTCUT:
620 processShortcut(args.intent);
621 break;
622 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700623 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700624 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700625 result = true;
626 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800627 case REQUEST_CREATE_APPWIDGET:
628 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700629 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700630 result = true;
631 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800632 case REQUEST_PICK_WALLPAPER:
633 // We just wanted the activity result here so we can clear mWaitingForResult
634 break;
635 }
Michael Jurka27614d22012-04-02 06:40:22 -0700636 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
637 // if you turned the screen off and then back while in All Apps, Launcher would not
638 // return to the workspace. Clearing mAddInfo.container here fixes this issue
639 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700640 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800641 }
642
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700644 protected void onActivityResult(
645 final int requestCode, final int resultCode, final Intent data) {
646 if (requestCode == REQUEST_BIND_APPWIDGET) {
647 int appWidgetId = data != null ?
648 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
649 if (resultCode == RESULT_CANCELED) {
650 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
651 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700652 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700653 }
654 return;
655 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700656 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800657 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
658 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700659 mWaitingForResult = false;
660
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 // We have special handling for widgets
662 if (isWidgetDrop) {
663 int appWidgetId = data != null ?
664 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700665 if (appWidgetId < 0) {
666 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
667 "widget configuration activity.");
668 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
669 } else {
670 completeTwoStageWidgetDrop(resultCode, appWidgetId);
671 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 return;
673 }
674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 // The pattern used here is that a user PICKs a specific application,
676 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
679 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700680 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 final PendingAddArguments args = new PendingAddArguments();
682 args.requestCode = requestCode;
683 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700684 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700685 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700686 args.cellX = mPendingAddInfo.cellX;
687 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 if (isWorkspaceLocked()) {
689 sPendingAddList.add(args);
690 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700691 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800694 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700695 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800696 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
697 null);
698 }
699
700 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700701 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700702 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800703 Runnable onCompleteRunnable = null;
704 int animationType = 0;
705
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700706 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800707 if (resultCode == RESULT_OK) {
708 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
709 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700710 mPendingAddWidgetInfo);
711 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800712 onCompleteRunnable = new Runnable() {
713 @Override
714 public void run() {
715 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800717 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
718 null);
719 }
720 };
721 } else if (resultCode == RESULT_CANCELED) {
722 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
723 onCompleteRunnable = new Runnable() {
724 @Override
725 public void run() {
726 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
727 null);
728 }
729 };
730 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700731 if (mDragLayer.getAnimatedView() != null) {
732 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
733 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
734 animationType, boundWidget, true);
735 } else {
736 // The animated view may be null in the case of a rotation during widget configuration
737 onCompleteRunnable.run();
738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 }
740
741 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700742 protected void onStop() {
743 super.onStop();
744 FirstFrameAnimatorHelper.setIsVisible(false);
745 }
746
747 @Override
748 protected void onStart() {
749 super.onStart();
750 FirstFrameAnimatorHelper.setIsVisible(true);
751 }
752
753 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200755 long startTime = 0;
756 if (DEBUG_RESUME_TIME) {
757 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400758 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700761
Winson Chung4a2afa32012-07-19 14:53:05 -0700762 // Restore the previous launcher state
763 if (mOnResumeState == State.WORKSPACE) {
764 showWorkspace(false);
765 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
766 showAllApps(false);
767 }
768 mOnResumeState = State.NONE;
769
Craig Mautner360310b2012-10-26 15:13:08 -0700770 // Background was set to gradient in onPause(), restore to black if in all apps.
771 setWorkspaceBackground(mState == State.WORKSPACE);
772
Winson Chungde0fb8f2012-05-08 14:37:08 -0700773 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700774 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700775
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800776 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700777 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700778 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700780 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400781 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700782 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700784 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200785 // We might have postponed some bind calls until onResume (see waitUntilResume) --
786 // execute them here
787 long startTimeCallbacks = 0;
788 if (DEBUG_RESUME_TIME) {
789 startTimeCallbacks = System.currentTimeMillis();
790 }
791
792 if (mAppsCustomizeContent != null) {
793 mAppsCustomizeContent.setBulkBind(true);
794 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700795 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
796 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200797 }
798 if (mAppsCustomizeContent != null) {
799 mAppsCustomizeContent.setBulkBind(false);
800 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700801 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200802 if (DEBUG_RESUME_TIME) {
803 Log.d(TAG, "Time spent processing callbacks in onResume: " +
804 (System.currentTimeMillis() - startTimeCallbacks));
805 }
Michael Jurka447bf842013-05-15 14:52:15 +0200806 }
Michael Jurka54554252013-08-01 12:52:23 +0200807 if (mOnResumeCallbacks.size() > 0) {
808 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
809 mOnResumeCallbacks.get(i).run();
810 }
811 mOnResumeCallbacks.clear();
812 }
Winson Chunge4e50662012-01-23 14:45:13 -0800813
814 // Reset the pressed state of icons that were locked in the press state while activities
815 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800816 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800817 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800818 mWaitingForResume.setStayPressed(false);
819 }
Winson Chunge4e50662012-01-23 14:45:13 -0800820 if (mAppsCustomizeContent != null) {
821 // Resets the previous all apps icon press state
822 mAppsCustomizeContent.resetDrawableState();
823 }
Adam Cohen06dff352012-06-01 17:17:08 -0700824 // It is possible that widgets can receive updates while launcher is not in the foreground.
825 // Consequently, the widgets will be inflated in the orientation of the foreground activity
826 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
827 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700828 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700829
830 // Again, as with the above scenario, it's possible that one or more of the global icons
831 // were updated in the wrong orientation.
832 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200833 if (DEBUG_RESUME_TIME) {
834 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
835 }
Adam Cohen06dff352012-06-01 17:17:08 -0700836 }
837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700839 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700840 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
841 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
842 // when Launcher resumes and we are still in AllApps.
843 updateWallpaperVisibility(true);
844
Winson Chung997a9232013-07-24 15:33:46 -0700845 // Ensure that items added to Launcher are queued until Launcher returns
846 InstallShortcutReceiver.enableInstallQueue();
847
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700848 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700849 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800850 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700851 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700852 }
Romain Guycbb89e42009-06-08 15:52:54 -0700853
Adam Cohen66a01fd2013-06-11 12:48:00 -0700854 protected void onFinishBindingItems() {
855 }
856
Adam Cohenbffe7452013-07-22 18:21:45 -0700857 QSBScroller mQsbScroller = new QSBScroller() {
858 int scrollY = 0;
859
860 @Override
861 public void setScrollY(int scroll) {
862 scrollY = scroll;
863
864 if (mWorkspace.isOnOrMovingToCustomContent()) {
865 mSearchDropTargetBar.setTranslationY(- scrollY);
866 }
867 }
868 };
869
870 public void resetQSBScroll() {
871 mSearchDropTargetBar.animate().translationY(0).start();
872 }
873
874 public interface CustomContentCallbacks {
875 // Custom content is completely shown
876 public void onShow();
877
878 // Custom content is completely hidden
879 public void onHide();
880 }
881
882 public interface QSBScroller {
883 public void setScrollY(int scrollY);
884 }
885
Adam Cohen66a01fd2013-06-11 12:48:00 -0700886 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700887 public QSBScroller addToCustomContentPage(View customContent) {
888 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700889 }
890
Winson Chung98ca0f72013-07-29 12:58:51 -0700891 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700892 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700893 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700894 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700895 }
896
Adam Cohenedb40762013-07-18 16:45:45 -0700897 // The custom content needs to offset its content to account for the QSB
898 public int getTopOffsetForCustomContent() {
899 return mWorkspace.getPaddingTop();
900 }
901
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700902 @Override
903 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904 // Flag the loader to stop early before switching
905 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700906 if (mAppsCustomizeContent != null) {
907 mAppsCustomizeContent.surrender();
908 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800909 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700910 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700911
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800912 // We can't hide the IME if it was forced open. So don't bother
913 /*
914 @Override
915 public void onWindowFocusChanged(boolean hasFocus) {
916 super.onWindowFocusChanged(hasFocus);
917
918 if (hasFocus) {
919 final InputMethodManager inputManager = (InputMethodManager)
920 getSystemService(Context.INPUT_METHOD_SERVICE);
921 WindowManager.LayoutParams lp = getWindow().getAttributes();
922 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
923 android.os.Handler()) {
924 protected void onReceiveResult(int resultCode, Bundle resultData) {
925 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
926 }
927 });
928 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
929 }
930 }
931 */
932
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 private boolean acceptFilter() {
934 final InputMethodManager inputManager = (InputMethodManager)
935 getSystemService(Context.INPUT_METHOD_SERVICE);
936 return !inputManager.isFullscreenMode();
937 }
938
939 @Override
940 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700941 final int uniChar = event.getUnicodeChar();
942 final boolean handled = super.onKeyDown(keyCode, event);
943 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
944 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
946 keyCode, event);
947 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700948 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700949 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700950 // showSearchDialog()
951 // If there are multiple keystrokes before the search dialog takes focus,
952 // onSearchRequested() will be called for every keystroke,
953 // but it is idempotent, so it's fine.
954 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 }
956 }
957
Joe Onorato8a9625e2010-01-28 15:55:35 -0800958 // Eat the long press event so the keyboard doesn't come up.
959 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
960 return true;
961 }
962
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 return handled;
964 }
965
Karl Rosaen138a0412009-04-23 19:00:21 -0700966 private String getTypedText() {
967 return mDefaultKeySsb.toString();
968 }
969
970 private void clearTypedText() {
971 mDefaultKeySsb.clear();
972 mDefaultKeySsb.clearSpans();
973 Selection.setSelection(mDefaultKeySsb, 0);
974 }
975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700977 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
978 * State
979 */
980 private static State intToState(int stateOrdinal) {
981 State state = State.WORKSPACE;
982 final State[] stateValues = State.values();
983 for (int i = 0; i < stateValues.length; i++) {
984 if (stateValues[i].ordinal() == stateOrdinal) {
985 state = stateValues[i];
986 break;
987 }
988 }
989 return state;
990 }
991
992 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 * Restores the previous state, if it exists.
994 *
995 * @param savedState The previous state.
996 */
997 private void restoreState(Bundle savedState) {
998 if (savedState == null) {
999 return;
1000 }
1001
Michael Jurka883f55b2010-10-21 15:47:14 -07001002 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001003 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001004 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001005 }
1006
Winson Chungf0c6ae02012-03-21 16:10:31 -07001007 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001009 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 }
1011
Winson Chung3d503fb2011-07-13 17:25:49 -07001012 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001013 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001014
Winson Chung3d503fb2011-07-13 17:25:49 -07001015 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1016 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001017 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001018 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1019 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001020 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1021 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1022 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001023 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 mRestoring = true;
1025 }
1026
1027 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1028 if (renameFolder) {
1029 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001030 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 mRestoring = true;
1032 }
Winson Chunga12a2502010-12-20 14:41:35 -08001033
Winson Chung785d2eb2011-04-14 16:08:02 -07001034 // Restore the AppsCustomize tab
1035 if (mAppsCustomizeTabHost != null) {
1036 String curTab = savedState.getString("apps_customize_currentTab");
1037 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001038 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001039 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001040 mAppsCustomizeContent.loadAssociatedPages(
1041 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001042 }
1043
Winson Chung5afbf7b2011-07-25 11:53:08 -07001044 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1045 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 }
1048
1049 /**
1050 * Finds all the views we need and configure them properly.
1051 */
1052 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001053 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001054
Craig Mautner360310b2012-10-26 15:13:08 -07001055 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001056 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1057 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001058 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001059
1060 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1061 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062
Winson Chung4c98d922011-05-31 16:50:48 -07001063 // Setup the drag layer
1064 mDragLayer.setup(this, dragController);
1065
Winson Chung3d503fb2011-07-13 17:25:49 -07001066 // Setup the hotseat
1067 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1068 if (mHotseat != null) {
1069 mHotseat.setup(this);
1070 }
1071
Winson Chung4c98d922011-05-31 16:50:48 -07001072 // Setup the workspace
1073 mWorkspace.setHapticFeedbackEnabled(false);
1074 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001075 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001076 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001077
Winson Chungf0ea4d32011-06-06 14:27:16 -07001078 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001079 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001080
Winson Chungf0ea4d32011-06-06 14:27:16 -07001081 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001082 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001083 mAppsCustomizeContent = (AppsCustomizePagedView)
1084 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1085 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001086
Winson Chung3d503fb2011-07-13 17:25:49 -07001087 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001088 dragController.setDragScoller(mWorkspace);
1089 dragController.setScrollView(mDragLayer);
1090 dragController.setMoveTarget(mWorkspace);
1091 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001092 if (mSearchDropTargetBar != null) {
1093 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001094 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001095
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001096 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001097 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001098 mWeightWatcher = new WeightWatcher(this);
1099 mWeightWatcher.setAlpha(0.5f);
1100 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001101 new FrameLayout.LayoutParams(
1102 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001103 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001104 Gravity.BOTTOM)
1105 );
Adam Cohen39a06042013-07-19 14:30:12 -07001106
1107 boolean show = shouldShowWeightWatcher();
1108 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 }
1111
1112 /**
1113 * Creates a view representing a shortcut.
1114 *
1115 * @param info The data structure describing the shortcut.
1116 *
1117 * @return A View inflated from R.layout.application.
1118 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001119 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001121 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 }
1123
1124 /**
1125 * Creates a view representing a shortcut inflated from the specified resource.
1126 *
1127 * @param layoutResId The id of the XML layout used to create the shortcut.
1128 * @param parent The group the shortcut belongs to.
1129 * @param info The data structure describing the shortcut.
1130 *
1131 * @return A View inflated from layoutResId.
1132 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001133 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001134 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1135 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 return favorite;
1138 }
1139
1140 /**
1141 * Add an application shortcut to the workspace.
1142 *
1143 * @param data The intent describing the application.
1144 * @param cellInfo The position on screen where to create the shortcut.
1145 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001146 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001147 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001148 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149
Adam Cohenfbba09b2011-07-18 21:43:05 -07001150 // First we check if we already know the exact location where we want to add this item.
1151 if (cellX >= 0 && cellY >= 0) {
1152 cellXY[0] = cellX;
1153 cellXY[1] = cellY;
1154 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001155 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 return;
1157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001159 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001160
Romain Guy73b979d2009-06-09 12:57:21 -07001161 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001162 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001164 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001165 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001166 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001167 } else {
1168 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170 }
Romain Guycbb89e42009-06-08 15:52:54 -07001171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 /**
1173 * Add a shortcut to the workspace.
1174 *
1175 * @param data The intent describing the shortcut.
1176 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001178 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001179 int cellY) {
1180 int[] cellXY = mTmpAddItemCellCoordinates;
1181 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001182 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001183
Michael Jurkad3ef3062010-11-23 16:23:58 -08001184 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001185
Adam Cohen558baaf2011-08-15 15:22:57 -07001186 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001187 if (info == null) {
1188 return;
1189 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001190 final View view = createShortcut(info);
1191
Adam Cohenfbba09b2011-07-18 21:43:05 -07001192 // First we check if we already know the exact location where we want to add this item.
1193 if (cellX >= 0 && cellY >= 0) {
1194 cellXY[0] = cellX;
1195 cellXY[1] = cellY;
1196 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001197
1198 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001199 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001200 true, null,null)) {
1201 return;
1202 }
1203 DragObject dragObject = new DragObject();
1204 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001205 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1206 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001207 return;
1208 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001209 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001210 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001211 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001212 foundCellSpan = (result != null);
1213 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001214 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001215 }
1216
1217 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001218 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001219 return;
1220 }
1221
Adam Cohendcd297f2013-06-18 13:13:40 -07001222 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223
1224 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001225 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001226 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228 }
1229
Adam Cohen2f093b62012-04-30 18:59:53 -07001230 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1231 int minHeight) {
1232 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001233 // We want to account for the extra amount of padding that we are adding to the widget
1234 // to ensure that it gets the full amount of space that it has requested
1235 int requiredWidth = minWidth + padding.left + padding.right;
1236 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001237 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001238 }
1239
Adam Cohen2f093b62012-04-30 18:59:53 -07001240 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1241 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001242 }
1243
Adam Cohen2f093b62012-04-30 18:59:53 -07001244 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1245 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001246 }
1247
Adam Cohen2f093b62012-04-30 18:59:53 -07001248 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1249 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001250 }
1251
Adam Cohen2f093b62012-04-30 18:59:53 -07001252 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1253 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001254 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 }
1256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001258 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001260 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001261 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001263 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001264 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1265 if (appWidgetInfo == null) {
1266 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1267 }
Romain Guycbb89e42009-06-08 15:52:54 -07001268
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001269 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001270 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001271
Adam Cohen2f093b62012-04-30 18:59:53 -07001272 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1273 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001276 // We have saved the position to which the widget was dragged-- this really only matters
1277 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001278 int[] cellXY = mTmpAddItemCellCoordinates;
1279 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001280 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001281 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001282 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1283 cellXY[0] = mPendingAddInfo.cellX;
1284 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001285 spanXY[0] = mPendingAddInfo.spanX;
1286 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001287 foundCellSpan = true;
1288 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001289 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001290 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1292 spanXY[1], cellXY, finalSpan);
1293 spanXY[0] = finalSpan[0];
1294 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001295 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001296 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001297 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001298 }
1299
Michael Jurka0280c3b2010-09-17 15:00:07 -07001300 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001301 if (appWidgetId != -1) {
1302 // Deleting an app widget ID is a void call but writes to disk before returning
1303 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001304 new Thread("deleteAppWidgetId") {
1305 public void run() {
1306 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1307 }
1308 }.start();
1309 }
Winson Chung93eef082012-03-23 15:59:27 -07001310 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001311 return;
1312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001314 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001315 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1316 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001317 launcherInfo.spanX = spanXY[0];
1318 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001319 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1320 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001323 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324
1325 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001326 if (hostView == null) {
1327 // Perform actual inflation because we're live
1328 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1329 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1330 } else {
1331 // The AppWidgetHostView has already been inflated and instantiated
1332 launcherInfo.hostView = hostView;
1333 }
Romain Guycbb89e42009-06-08 15:52:54 -07001334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001336 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001337 launcherInfo.notifyWidgetSizeChanged(this);
1338
Adam Cohendcd297f2013-06-18 13:13:40 -07001339 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001340 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001341
1342 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001344 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
Romain Guycbb89e42009-06-08 15:52:54 -07001346
Adam Cohended9f8d2010-11-03 13:25:16 -07001347 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1348 @Override
1349 public void onReceive(Context context, Intent intent) {
1350 final String action = intent.getAction();
1351 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1352 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001353 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001354 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001355
Winson Chungc100e8e2011-08-09 16:02:43 -07001356 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001357 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001358 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1359 mAppsCustomizeTabHost.reset();
1360 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001361 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001362 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1363 mUserPresent = true;
1364 updateRunning();
1365 }
1366 }
1367 };
1368
1369 @Override
1370 public void onAttachedToWindow() {
1371 super.onAttachedToWindow();
1372
1373 // Listen for broadcasts related to user-presence
1374 final IntentFilter filter = new IntentFilter();
1375 filter.addAction(Intent.ACTION_SCREEN_OFF);
1376 filter.addAction(Intent.ACTION_USER_PRESENT);
1377 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001378 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001379 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001380 mVisible = true;
1381 }
1382
1383 @Override
1384 public void onDetachedFromWindow() {
1385 super.onDetachedFromWindow();
1386 mVisible = false;
1387
Adam Cohend113e0c2010-11-11 10:48:05 -08001388 if (mAttached) {
1389 unregisterReceiver(mReceiver);
1390 mAttached = false;
1391 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001392 updateRunning();
1393 }
1394
1395 public void onWindowVisibilityChanged(int visibility) {
1396 mVisible = visibility == View.VISIBLE;
1397 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001398 // The following code used to be in onResume, but it turns out onResume is called when
1399 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1400 // is a more appropriate event to handle
1401 if (mVisible) {
1402 mAppsCustomizeTabHost.onWindowVisible();
1403 if (!mWorkspaceLoading) {
1404 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001405 // We want to let Launcher draw itself at least once before we force it to build
1406 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001407 // apps is nice and speedy.
1408 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001409 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001410 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001411 if (mStarted) return;
1412 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001413 // We delay the layer building a bit in order to give
1414 // other message processing a time to run. In particular
1415 // this avoids a delay in hiding the IME if it was
1416 // currently shown, because doing that may involve
1417 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001418 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001419 final ViewTreeObserver.OnDrawListener listener = this;
1420 mWorkspace.post(new Runnable() {
1421 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001422 if (mWorkspace != null &&
1423 mWorkspace.getViewTreeObserver() != null) {
1424 mWorkspace.getViewTreeObserver().
1425 removeOnDrawListener(listener);
1426 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001427 }
1428 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001429 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001430 }
1431 });
1432 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001433 // When Launcher comes back to foreground, a different Activity might be responsible for
1434 // the app market intent, so refresh the icon
1435 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001436 clearTypedText();
1437 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001438 }
1439
1440 private void sendAdvanceMessage(long delay) {
1441 mHandler.removeMessages(ADVANCE_MSG);
1442 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1443 mHandler.sendMessageDelayed(msg, delay);
1444 mAutoAdvanceSentTime = System.currentTimeMillis();
1445 }
1446
1447 private void updateRunning() {
1448 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1449 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1450 mAutoAdvanceRunning = autoAdvanceRunning;
1451 if (autoAdvanceRunning) {
1452 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1453 sendAdvanceMessage(delay);
1454 } else {
1455 if (!mWidgetsToAdvance.isEmpty()) {
1456 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1457 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1458 }
1459 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001460 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001461 }
1462 }
1463 }
1464
1465 private final Handler mHandler = new Handler() {
1466 @Override
1467 public void handleMessage(Message msg) {
1468 if (msg.what == ADVANCE_MSG) {
1469 int i = 0;
1470 for (View key: mWidgetsToAdvance.keySet()) {
1471 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1472 final int delay = mAdvanceStagger * i;
1473 if (v instanceof Advanceable) {
1474 postDelayed(new Runnable() {
1475 public void run() {
1476 ((Advanceable) v).advance();
1477 }
1478 }, delay);
1479 }
1480 i++;
1481 }
1482 sendAdvanceMessage(mAdvanceInterval);
1483 }
1484 }
1485 };
1486
1487 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001488 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001489 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1490 if (v instanceof Advanceable) {
1491 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001492 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001493 updateRunning();
1494 }
1495 }
1496
1497 void removeWidgetToAutoAdvance(View hostView) {
1498 if (mWidgetsToAdvance.containsKey(hostView)) {
1499 mWidgetsToAdvance.remove(hostView);
1500 updateRunning();
1501 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001502 }
1503
Joe Onorato9c1289c2009-08-17 11:03:03 -04001504 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001505 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001506 launcherInfo.hostView = null;
1507 }
1508
Winson Chung93eef082012-03-23 15:59:27 -07001509 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1510 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1511 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001512 }
1513
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001514 public LauncherAppWidgetHost getAppWidgetHost() {
1515 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Winson Chunga9abd0e2010-10-27 17:18:37 -07001518 public LauncherModel getModel() {
1519 return mModel;
1520 }
1521
Bjorn Bringertc459e522013-06-07 19:36:01 +01001522 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001523 getWindow().closeAllPanels();
1524
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001525 // Whatever we were doing is hereby canceled.
1526 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001527 }
1528
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 @Override
1530 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001531 long startTime = 0;
1532 if (DEBUG_RESUME_TIME) {
1533 startTime = System.currentTimeMillis();
1534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 super.onNewIntent(intent);
1536
1537 // Close the menu
1538 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001539 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001540 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001541
Jamie Genniscb222e82012-10-23 15:44:26 -07001542 final boolean alreadyOnHome =
1543 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001544 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001545
Jamie Genniscb222e82012-10-23 15:44:26 -07001546 Runnable processIntent = new Runnable() {
1547 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001548 if (mWorkspace == null) {
1549 // Can be cases where mWorkspace is null, this prevents a NPE
1550 return;
1551 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001552 Folder openFolder = mWorkspace.getOpenFolder();
1553 // In all these cases, only animate if we're already on home
1554 mWorkspace.exitWidgetResizeMode();
1555 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1556 openFolder == null) {
1557 mWorkspace.moveToDefaultScreen(true);
1558 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001559
Jamie Genniscb222e82012-10-23 15:44:26 -07001560 closeFolder();
1561 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001562
Jamie Genniscb222e82012-10-23 15:44:26 -07001563 // If we are already on home, then just animate back to the workspace,
1564 // otherwise, just wait until onResume to set the state back to Workspace
1565 if (alreadyOnHome) {
1566 showWorkspace(true);
1567 } else {
1568 mOnResumeState = State.WORKSPACE;
1569 }
1570
1571 final View v = getWindow().peekDecorView();
1572 if (v != null && v.getWindowToken() != null) {
1573 InputMethodManager imm = (InputMethodManager)getSystemService(
1574 INPUT_METHOD_SERVICE);
1575 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1576 }
1577
1578 // Reset AllApps to its initial state
1579 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1580 mAppsCustomizeTabHost.reset();
1581 }
1582 }
1583 };
1584
1585 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1586 // Delay processing of the intent to allow the status bar animation to finish
1587 // first in order to avoid janky animations.
1588 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001589 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001590 // Process the intent immediately.
1591 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001592 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001593
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 }
Michael Jurka447bf842013-05-15 14:52:15 +02001595 if (DEBUG_RESUME_TIME) {
1596 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1597 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
1599
1600 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001601 public void onRestoreInstanceState(Bundle state) {
1602 super.onRestoreInstanceState(state);
1603 for (int page: mSynchronouslyBoundPages) {
1604 mWorkspace.restoreInstanceStateForChild(page);
1605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 }
1607
1608 @Override
1609 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001610 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001611 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
Michael Jurka883f55b2010-10-21 15:47:14 -07001613 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001614 // We close any open folder since it will not be re-opened, and we need to make sure
1615 // this state is reflected.
1616 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617
Adam Cohendcd297f2013-06-18 13:13:40 -07001618 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001619 mWaitingForResult) {
1620 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001621 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001622 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1623 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001624 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1625 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1626 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 }
1628
1629 if (mFolderInfo != null && mWaitingForResult) {
1630 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1631 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1632 }
Michael Jurka946ad472010-07-09 18:05:18 -07001633
Winson Chung785d2eb2011-04-14 16:08:02 -07001634 // Save the current AppsCustomize tab
1635 if (mAppsCustomizeTabHost != null) {
1636 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1637 if (currentTabTag != null) {
1638 outState.putString("apps_customize_currentTab", currentTabTag);
1639 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001640 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1641 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 }
1644
1645 @Override
1646 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001648
Winson Chunge7a03942011-08-05 15:05:12 -07001649 // Remove all pending runnables
1650 mHandler.removeMessages(ADVANCE_MSG);
1651 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001652 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001653
Winson Chungcd2b0142011-06-08 16:02:26 -07001654 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001655 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001656 mModel.stopLoader();
1657 app.setLauncher(null);
1658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001660 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001662 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001664 mAppWidgetHost = null;
1665
1666 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667
1668 TextKeyListener.getInstance().release();
1669
Winson Chung81b52252012-08-27 15:34:29 -07001670 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1671 // to prevent leaking Launcher activities on orientation change.
1672 if (mModel != null) {
1673 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1674 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001675
1676 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001677 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001678
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001679 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001680 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1681 mWorkspace.removeAllViews();
1682 mWorkspace = null;
1683 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001684
Michael Jurka2ecf9952012-06-18 12:52:28 -07001685 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 }
1687
Adam Cohena9cf38f2011-05-02 15:36:58 -07001688 public DragController getDragController() {
1689 return mDragController;
1690 }
1691
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 @Override
1693 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001694 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 super.startActivityForResult(intent, requestCode);
1696 }
1697
Winson Chung70d72102011-08-12 11:24:35 -07001698 /**
1699 * Indicates that we want global search for this activity by setting the globalSearch
1700 * argument for {@link #startSearch} to true.
1701 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001703 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001705
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001706 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001707
Karl Rosaen138a0412009-04-23 19:00:21 -07001708 if (initialQuery == null) {
1709 // Use any text typed in the launcher as the initial query
1710 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 if (appSearchData == null) {
1713 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001714 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 }
Winson Chungadf0c182012-08-23 14:59:07 -07001716 Rect sourceBounds = new Rect();
1717 if (mSearchDropTargetBar != null) {
1718 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1719 }
Romain Guycbb89e42009-06-08 15:52:54 -07001720
Bjorn Bringertc459e522013-06-07 19:36:01 +01001721 startSearch(initialQuery, selectInitialQuery,
1722 appSearchData, sourceBounds);
1723 }
1724
1725 public void startSearch(String initialQuery,
1726 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001727 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001728 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001729 }
1730
1731 /**
1732 * Starts the global search activity. This code is a copied from SearchManager
1733 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001734 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001735 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001736 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001737 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1738 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1739 if (globalSearchActivity == null) {
1740 Log.w(TAG, "No global search activity found.");
1741 return;
1742 }
1743 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1744 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1745 intent.setComponent(globalSearchActivity);
1746 // Make sure that we have a Bundle to put source in
1747 if (appSearchData == null) {
1748 appSearchData = new Bundle();
1749 } else {
1750 appSearchData = new Bundle(appSearchData);
1751 }
1752 // Set source to package name of app that starts global search, if not set already.
1753 if (!appSearchData.containsKey("source")) {
1754 appSearchData.putString("source", getPackageName());
1755 }
1756 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1757 if (!TextUtils.isEmpty(initialQuery)) {
1758 intent.putExtra(SearchManager.QUERY, initialQuery);
1759 }
1760 if (selectInitialQuery) {
1761 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1762 }
1763 intent.setSourceBounds(sourceBounds);
1764 try {
1765 startActivity(intent);
1766 } catch (ActivityNotFoundException ex) {
1767 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
1770
Winson Chung70d72102011-08-12 11:24:35 -07001771 @Override
1772 public boolean onCreateOptionsMenu(Menu menu) {
1773 if (isWorkspaceLocked()) {
1774 return false;
1775 }
1776
1777 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001778
1779 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1780 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1781 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001782 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1783 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1784 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001785 String helpUrl = getString(R.string.help_url);
1786 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001787 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1788 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1789
Winson Chung70d72102011-08-12 11:24:35 -07001790 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1791 .setIcon(android.R.drawable.ic_menu_gallery)
1792 .setAlphabeticShortcut('W');
1793 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1794 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001795 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001796 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001797 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1798 .setIcon(android.R.drawable.ic_menu_preferences)
1799 .setIntent(settings)
1800 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001801 if (!helpUrl.isEmpty()) {
1802 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1803 .setIcon(android.R.drawable.ic_menu_help)
1804 .setIntent(help)
1805 .setAlphabeticShortcut('H');
1806 }
Winson Chung70d72102011-08-12 11:24:35 -07001807 return true;
1808 }
1809
1810 @Override
1811 public boolean onPrepareOptionsMenu(Menu menu) {
1812 super.onPrepareOptionsMenu(menu);
1813
1814 if (mAppsCustomizeTabHost.isTransitioning()) {
1815 return false;
1816 }
1817 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1818 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1819
1820 return true;
1821 }
1822
1823 @Override
1824 public boolean onOptionsItemSelected(MenuItem item) {
1825 switch (item.getItemId()) {
1826 case MENU_WALLPAPER_SETTINGS:
1827 startWallpaper();
1828 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001829 }
1830
1831 return super.onOptionsItemSelected(item);
1832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001834 @Override
1835 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001836 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001837 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001838 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001839 }
1840
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 public boolean isWorkspaceLocked() {
1842 return mWorkspaceLoading || mWaitingForResult;
1843 }
1844
Michael Jurka0280c3b2010-09-17 15:00:07 -07001845 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001846 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001847 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001848 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1849 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001850 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001851 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001852 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001853
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001854 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1855 AppWidgetProviderInfo appWidgetInfo) {
1856 if (appWidgetInfo.configure != null) {
1857 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001858
Bjorn Bringert7984c942009-12-09 15:38:25 +00001859 // Launch over to configure widget, if needed
1860 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001861 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001862 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001863 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001865 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001866 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001867 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001868 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001869 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 }
1871 }
Romain Guycbb89e42009-06-08 15:52:54 -07001872
Adam Cohenfbba09b2011-07-18 21:43:05 -07001873 /**
1874 * Process a shortcut drop.
1875 *
1876 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001877 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001878 * @param cell The cell it should be added to, optional
1879 * @param position The location on the screen where it was dropped, optional
1880 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001881 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001882 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001883 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001884 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001885 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001886 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001887
1888 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001889 mPendingAddInfo.cellX = cell[0];
1890 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001891 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001892
1893 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1894 createShortcutIntent.setComponent(componentName);
1895 processShortcut(createShortcutIntent);
1896 }
1897
Adam Cohenfbba09b2011-07-18 21:43:05 -07001898 /**
1899 * Process a widget drop.
1900 *
1901 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001902 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001903 * @param cell The cell it should be added to, optional
1904 * @param position The location on the screen where it was dropped, optional
1905 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001906 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001907 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001908 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001909 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001910 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001911 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001912 mPendingAddInfo.minSpanX = info.minSpanX;
1913 mPendingAddInfo.minSpanY = info.minSpanY;
1914
Adam Cohenfbba09b2011-07-18 21:43:05 -07001915 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001916 mPendingAddInfo.cellX = cell[0];
1917 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001918 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001919 if (span != null) {
1920 mPendingAddInfo.spanX = span[0];
1921 mPendingAddInfo.spanY = span[1];
1922 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001923
Adam Cohened66b2b2012-01-23 17:28:51 -08001924 AppWidgetHostView hostView = info.boundWidget;
1925 int appWidgetId;
1926 if (hostView != null) {
1927 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001928 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001929 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001930 // In this case, we either need to start an activity to get permission to bind
1931 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001932 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001933 Bundle options = info.bindOptions;
1934
1935 boolean success = false;
1936 if (options != null) {
1937 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1938 info.componentName, options);
1939 } else {
1940 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1941 info.componentName);
1942 }
1943 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001944 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001945 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001946 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001947 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1948 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1949 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001950 // TODO: we need to make sure that this accounts for the options bundle.
1951 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001952 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1953 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001954 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001955 }
1956
Joe Onoratodeb98af2010-02-19 14:59:39 -08001957 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001958 // Handle case where user selected "Applications"
1959 String applicationName = getResources().getString(R.string.group_applications);
1960 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001961
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001962 if (applicationName != null && applicationName.equals(shortcutName)) {
1963 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1964 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001965
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001966 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1967 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001968 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001969 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001970 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001971 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974
Winson Chung24ab2f12010-09-16 14:10:47 -07001975 void processWallpaper(Intent intent) {
1976 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1977 }
1978
Adam Cohendcd297f2013-06-18 13:13:40 -07001979 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001980 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001981 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 folderInfo.title = getText(R.string.folder_name);
1983
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001985 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001986 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001987 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988
1989 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001990 FolderIcon newFolder =
1991 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001992 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001993 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001994 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
Romain Guycbb89e42009-06-08 15:52:54 -07001996
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001998 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001999 }
2000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002002 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07002004 Intent chooser = Intent.createChooser(pickWallpaper,
2005 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08002006 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
2007 // Removed in Eclair MR1
2008// WallpaperManager wm = (WallpaperManager)
2009// getSystemService(Context.WALLPAPER_SERVICE);
2010// WallpaperInfo wi = wm.getWallpaperInfo();
2011// if (wi != null && wi.getSettingsActivity() != null) {
2012// LabeledIntent li = new LabeledIntent(getPackageName(),
2013// R.string.configure_wallpaper, 0);
2014// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
2015// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
2016// }
Mike Clerona0618e42009-10-22 13:55:21 -07002017 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 }
2019
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002020 /**
2021 * Registers various content observers. The current implementation registers
2022 * only a favorites observer to keep track of the favorites applications.
2023 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002024 private void registerContentObservers() {
2025 ContentResolver resolver = getContentResolver();
2026 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2027 true, mWidgetObserver);
2028 }
2029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 @Override
2031 public boolean dispatchKeyEvent(KeyEvent event) {
2032 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2033 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002035 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002036 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002037 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002038 dumpState();
2039 return true;
2040 }
2041 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002042 }
2043 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2044 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002045 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 return true;
2047 }
2048 }
2049
2050 return super.dispatchKeyEvent(event);
2051 }
2052
Joe Onorato88ec0992009-11-19 13:16:06 -08002053 @Override
2054 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002055 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002056 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002057 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002058 Folder openFolder = mWorkspace.getOpenFolder();
2059 if (openFolder.isEditingName()) {
2060 openFolder.dismissEditingName();
2061 } else {
2062 closeFolder();
2063 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002064 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002065 mWorkspace.exitWidgetResizeMode();
2066
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002067 // Back button is a no-op here, but give at least some feedback for the button press
2068 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002069 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002070 }
2071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002073 * Re-listen when widgets are reset.
2074 */
2075 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002076 if (mAppWidgetHost != null) {
2077 mAppWidgetHost.startListening();
2078 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002079 }
2080
2081 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 * Launches the intent referred by the clicked shortcut.
2083 *
2084 * @param v The view representing the clicked shortcut.
2085 */
2086 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002087 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2088 // view has detached (it's possible for this to happen if the view is removed mid touch).
2089 if (v.getWindowToken() == null) {
2090 return;
2091 }
2092
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002093 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002094 return;
2095 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002098 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002100 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2101 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002102
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002103 // Check for special shortcuts
2104 if (intent.getComponent() != null) {
2105 final String shortcutClass = intent.getComponent().getClassName();
2106
2107 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2108 showAllApps(true);
2109 return;
2110 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2111 MemoryDumpActivity.startDump(this);
2112 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002113 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2114 toggleShowWeightWatcher();
2115 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002116 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002117 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002118
2119 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002120 int[] pos = new int[2];
2121 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002122 intent.setSourceBounds(new Rect(pos[0], pos[1],
2123 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002124
2125 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002126
Daniel Sandlerff02d492013-08-05 02:12:05 -04002127 mStats.recordLaunch(intent, shortcut);
2128
Michael Jurkaddd62e92011-02-16 17:49:14 -08002129 if (success && v instanceof BubbleTextView) {
2130 mWaitingForResume = (BubbleTextView) v;
2131 mWaitingForResume.setStayPressed(true);
2132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002134 if (v instanceof FolderIcon) {
2135 FolderIcon fi = (FolderIcon) v;
2136 handleFolderClick(fi);
2137 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002138 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002139 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002140 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002141 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002142 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 }
2145 }
2146
Michael Jurka0e260592010-06-30 17:07:39 -07002147 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002148 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002149 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002150 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2151 showWorkspace(true);
2152 }
Michael Jurka0e260592010-06-30 17:07:39 -07002153 return false;
2154 }
2155
Michael Jurkaaf442092010-06-10 17:01:57 -07002156 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002157 * Event handler for the search button
2158 *
2159 * @param v The view that was clicked.
2160 */
2161 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002162 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2163
Amith Yamasania135ba82011-08-09 17:42:01 -07002164 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002165 }
2166
2167 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002168 * Event handler for the voice button
2169 *
2170 * @param v The view that was clicked.
2171 */
2172 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002173 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002174
Bjorn Bringertc459e522013-06-07 19:36:01 +01002175 startVoice();
2176 }
2177
2178 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002179 try {
2180 final SearchManager searchManager =
2181 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2182 ComponentName activityName = searchManager.getGlobalSearchActivity();
2183 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002184 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002185 if (activityName != null) {
2186 intent.setPackage(activityName.getPackageName());
2187 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002188 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002189 } catch (ActivityNotFoundException e) {
2190 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002191 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002192 startActivitySafely(null, intent, "onClickVoiceButton");
2193 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002194 }
2195
2196 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002197 * Event handler for the "grid" button that appears on the home screen, which
2198 * enters all apps mode.
2199 *
2200 * @param v The view that was clicked.
2201 */
2202 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002203 showAllApps(true);
2204 }
2205
2206 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002207 // Provide the same haptic feedback that the system offers for virtual keys.
2208 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002209 }
2210
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002211 public void onClickAppMarketButton(View v) {
2212 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002213 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002214 } else {
2215 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002216 }
2217 }
2218
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002219 void startApplicationDetailsActivity(ComponentName componentName) {
2220 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002221 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2222 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002223 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002224 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002225 }
2226
Michael Jurka1e2f4652013-07-08 18:03:46 -07002227 // returns true if the activity was started
2228 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2229 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002230 // System applications cannot be installed. For now, show a toast explaining that.
2231 // We may give them the option of disabling apps this way.
2232 int messageId = R.string.uninstall_system_app_text;
2233 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002234 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002235 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002236 String packageName = componentName.getPackageName();
2237 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002238 Intent intent = new Intent(
2239 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002240 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2241 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002242 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002243 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002244 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002245 }
2246
Michael Jurka86a720e2012-05-09 11:23:23 -07002247 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002251 // Only launch using the new animation if the shortcut has not opted out (this is a
2252 // private contract between launcher and may be ignored in the future).
2253 boolean useLaunchAnimation = (v != null) &&
2254 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2255 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002256 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2257 v.getMeasuredWidth(), v.getMeasuredHeight());
2258
2259 startActivity(intent, opts.toBundle());
2260 } else {
2261 startActivity(intent);
2262 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002263 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 } catch (SecurityException e) {
2265 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002266 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002268 "or use the exported attribute for this activity. "
2269 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002271 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002273
Michael Jurka86a720e2012-05-09 11:23:23 -07002274 boolean startActivitySafely(View v, Intent intent, Object tag) {
2275 boolean success = false;
2276 try {
2277 success = startActivity(v, intent, tag);
2278 } catch (ActivityNotFoundException e) {
2279 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2280 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2281 }
2282 return success;
2283 }
2284
Romain Guy8e633c52010-03-04 12:51:36 -08002285 void startActivityForResultSafely(Intent intent, int requestCode) {
2286 try {
2287 startActivityForResult(intent, requestCode);
2288 } catch (ActivityNotFoundException e) {
2289 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2290 } catch (SecurityException e) {
2291 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2292 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2293 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2294 "or use the exported attribute for this activity.", e);
2295 }
2296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297
Adam Cohena9cf38f2011-05-02 15:36:58 -07002298 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002299 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002300 Folder openFolder = mWorkspace.getFolderForTag(info);
2301
2302 // If the folder info reports that the associated folder is open, then verify that
2303 // it is actually opened. There have been a few instances where this gets out of sync.
2304 if (info.opened && openFolder == null) {
2305 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002306 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002307 info.opened = false;
2308 }
2309
Adam Cohenfb91f302012-06-11 15:45:18 -07002310 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 // Close any open folder
2312 closeFolder();
2313 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002314 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 } else {
2316 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 int folderScreen;
2318 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002319 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 // .. and close it
2321 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002322 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 // Close any folder open on the current screen
2324 closeFolder();
2325 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002326 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 }
2328 }
2329 }
2330 }
2331
Adam Cohen268c4752012-06-06 17:47:33 -07002332 /**
2333 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2334 * in the DragLayer in the exact absolute location of the original FolderIcon.
2335 */
2336 private void copyFolderIconToImage(FolderIcon fi) {
2337 final int width = fi.getMeasuredWidth();
2338 final int height = fi.getMeasuredHeight();
2339
2340 // Lazy load ImageView, Bitmap and Canvas
2341 if (mFolderIconImageView == null) {
2342 mFolderIconImageView = new ImageView(this);
2343 }
2344 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2345 mFolderIconBitmap.getHeight() != height) {
2346 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2347 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2348 }
2349
2350 DragLayer.LayoutParams lp;
2351 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2352 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2353 } else {
2354 lp = new DragLayer.LayoutParams(width, height);
2355 }
2356
Adam Cohen307fe232012-08-16 17:55:58 -07002357 // The layout from which the folder is being opened may be scaled, adjust the starting
2358 // view size by this scale factor.
2359 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002360 lp.customPosition = true;
2361 lp.x = mRectForFolderAnimation.left;
2362 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002363 lp.width = (int) (scale * width);
2364 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002365
2366 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2367 fi.draw(mFolderIconCanvas);
2368 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002369 if (fi.getFolder() != null) {
2370 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2371 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002372 }
Adam Cohen268c4752012-06-06 17:47:33 -07002373 // Just in case this image view is still in the drag layer from a previous animation,
2374 // we remove it and re-add it.
2375 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2376 mDragLayer.removeView(mFolderIconImageView);
2377 }
2378 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002379 if (fi.getFolder() != null) {
2380 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002381 }
Adam Cohen268c4752012-06-06 17:47:33 -07002382 }
2383
Adam Cohen2801caf2011-05-13 20:57:39 -07002384 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002385 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002386 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2387 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2388 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2389
Adam Cohenc51934b2011-07-26 21:07:43 -07002390 FolderInfo info = (FolderInfo) fi.getTag();
2391 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2392 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002393 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2394 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002395 }
2396
Adam Cohen268c4752012-06-06 17:47:33 -07002397 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2398 copyFolderIconToImage(fi);
2399 fi.setVisibility(View.INVISIBLE);
2400
Michael Jurka2ecf9952012-06-18 12:52:28 -07002401 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002402 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002403 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2404 oa.start();
2405 }
2406
Adam Cohen268c4752012-06-06 17:47:33 -07002407 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002408 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002409 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2410 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2411 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2412
Adam Cohen268c4752012-06-06 17:47:33 -07002413 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002414
Adam Cohen268c4752012-06-06 17:47:33 -07002415 // We remove and re-draw the FolderIcon in-case it has changed
2416 mDragLayer.removeView(mFolderIconImageView);
2417 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002418 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002419 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002420 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002421 oa.addListener(new AnimatorListenerAdapter() {
2422 @Override
2423 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002424 if (cl != null) {
2425 cl.clearFolderLeaveBehind();
2426 // Remove the ImageView copy of the FolderIcon and make the original visible.
2427 mDragLayer.removeView(mFolderIconImageView);
2428 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002429 }
2430 }
2431 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002432 oa.start();
2433 }
2434
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002436 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 * is animated relative to the specified View. If the View is null, no animation
2438 * is played.
2439 *
2440 * @param folderInfo The FolderInfo describing the folder to open.
2441 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002442 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002443 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002444 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445
Adam Cohena9cf38f2011-05-02 15:36:58 -07002446 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447
Adam Cohen4554ee12011-08-03 16:13:21 -07002448 // Just verify that the folder hasn't already been added to the DragLayer.
2449 // There was a one-off crash where the folder had a parent already.
2450 if (folder.getParent() == null) {
2451 mDragLayer.addView(folder);
2452 mDragController.addDropTarget((DropTarget) folder);
2453 } else {
2454 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2455 folder.getParent() + ").");
2456 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002457 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002458 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002459
2460 // Notify the accessibility manager that this folder "window" has appeared and occluded
2461 // the workspace items
2462 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2463 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002464 }
2465
2466 public void closeFolder() {
2467 Folder folder = mWorkspace.getOpenFolder();
2468 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002469 if (folder.isEditingName()) {
2470 folder.dismissEditingName();
2471 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002472 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002473
2474 // Dismiss the folder cling
2475 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002476 }
2477 }
2478
2479 void closeFolder(Folder folder) {
2480 folder.getInfo().opened = false;
2481
2482 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2483 if (parent != null) {
2484 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2485 shrinkAndFadeInFolderIcon(fi);
2486 }
2487 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002488
2489 // Notify the accessibility manager that this folder "window" has disappeard and no
2490 // longer occludeds the workspace items
2491 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 }
2493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002495 if (!isDraggingEnabled()) return false;
2496 if (isWorkspaceLocked()) return false;
2497 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498
2499 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002500 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 }
2502
Michael Jurka0280c3b2010-09-17 15:00:07 -07002503 resetAddInfo();
2504 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002506 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 return true;
2508 }
2509
Winson Chung3d503fb2011-07-13 17:25:49 -07002510 // The hotseat touch handling does not go through Workspace, and we always allow long press
2511 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002512 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002513 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2514 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002515 if (itemUnderLongClick == null) {
2516 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002517 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2518 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002519 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002520 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2521 mWorkspace.startReordering();
2522 } else {
2523 startWallpaper();
2524 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002526 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002528 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 }
2530 }
2531 }
2532 return true;
2533 }
2534
Winson Chung3d503fb2011-07-13 17:25:49 -07002535 boolean isHotseatLayout(View layout) {
2536 return mHotseat != null && layout != null &&
2537 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2538 }
2539 Hotseat getHotseat() {
2540 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002541 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002542 SearchDropTargetBar getSearchBar() {
2543 return mSearchDropTargetBar;
2544 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002545
Winson Chung3d503fb2011-07-13 17:25:49 -07002546 /**
2547 * Returns the CellLayout of the specified container at the specified screen.
2548 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002549 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002550 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2551 if (mHotseat != null) {
2552 return mHotseat.getLayout();
2553 } else {
2554 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002555 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002556 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002557 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002558 }
2559 }
2560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 Workspace getWorkspace() {
2562 return mWorkspace;
2563 }
2564
Daniel Sandler843e8602010-06-07 14:59:01 -04002565 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002566 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002567 }
2568
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002569 /**
2570 * Helper method for the cameraZoomIn/cameraZoomOut animations
2571 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002572 * @param scaleFactor The scale factor used for the zoom
2573 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002574 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002575 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002576 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002577 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002578
Winson Chung18f41f82012-05-09 13:28:10 -07002579 void disableWallpaperIfInAllApps() {
2580 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002581 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002582 if (mAppsCustomizeTabHost != null &&
2583 !mAppsCustomizeTabHost.isTransitioning()) {
2584 updateWallpaperVisibility(false);
2585 }
2586 }
2587 }
2588
Craig Mautner360310b2012-10-26 15:13:08 -07002589 private void setWorkspaceBackground(boolean workspace) {
2590 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002591 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002592 }
2593
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002594 void updateWallpaperVisibility(boolean visible) {
2595 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2596 int curflags = getWindow().getAttributes().flags
2597 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2598 if (wpflags != curflags) {
2599 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2600 }
Craig Mautner360310b2012-10-26 15:13:08 -07002601 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002602 }
2603
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002604 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2605 if (v instanceof LauncherTransitionable) {
2606 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2607 }
2608 }
2609
Michael Jurkabed61d22012-02-14 22:51:29 -08002610 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2611 if (v instanceof LauncherTransitionable) {
2612 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2613 }
Winson Chung70442722012-02-10 15:43:22 -08002614
2615 // Update the workspace transition step as well
2616 dispatchOnLauncherTransitionStep(v, 0f);
2617 }
2618
2619 private void dispatchOnLauncherTransitionStep(View v, float t) {
2620 if (v instanceof LauncherTransitionable) {
2621 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2622 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002623 }
2624
2625 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2626 if (v instanceof LauncherTransitionable) {
2627 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2628 }
Winson Chung70442722012-02-10 15:43:22 -08002629
2630 // Update the workspace transition step as well
2631 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002632 }
2633
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002634 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002635 * Things to test when changing the following seven functions.
2636 * - Home from workspace
2637 * - from center screen
2638 * - from other screens
2639 * - Home from all apps
2640 * - from center screen
2641 * - from other screens
2642 * - Back from all apps
2643 * - from center screen
2644 * - from other screens
2645 * - Launch app from workspace and quit
2646 * - with back
2647 * - with home
2648 * - Launch app from all apps and quit
2649 * - with back
2650 * - with home
2651 * - Go to a screen that's not the default, then all
2652 * apps, and launch and app, and go back
2653 * - with back
2654 * -with home
2655 * - On workspace, long press power and go back
2656 * - with back
2657 * - with home
2658 * - On all apps, long press power and go back
2659 * - with back
2660 * - with home
2661 * - On workspace, power off
2662 * - On all apps, power off
2663 * - Launch an app and turn off the screen while in that app
2664 * - Go back with home key
2665 * - Go back with back key TODO: make this not go to workspace
2666 * - From all apps
2667 * - From workspace
2668 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2669 * - From all apps
2670 * - From the center workspace
2671 * - From another workspace
2672 */
2673
2674 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002675 * Zoom the camera out from the workspace to reveal 'toView'.
2676 * Assumes that the view to show is anchored at either the very top or very bottom
2677 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002678 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002679 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002680 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002681 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002682 mStateAnimation.cancel();
2683 mStateAnimation = null;
2684 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002685 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002686
Winson Chungf0ea4d32011-06-06 14:27:16 -07002687 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2688 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2689 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002690 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002691 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002692 final int startDelay =
2693 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002694
Michael Jurkab3e22d92011-10-31 15:58:33 -07002695 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002696
Michael Jurkad74c9842011-07-10 12:44:21 -07002697 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002698 Animator workspaceAnim =
2699 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002700
2701 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002702 toView.setScaleX(scale);
2703 toView.setScaleY(scale);
2704 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2705 scaleAnim.
2706 scaleX(1f).scaleY(1f).
2707 setDuration(duration).
2708 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002709
Winson Chungf0ea4d32011-06-06 14:27:16 -07002710 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002711 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002712 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002713 .ofFloat(toView, "alpha", 0f, 1f)
2714 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002715 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002716 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2717 @Override
2718 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002719 if (animation == null) {
2720 throw new RuntimeException("animation is null");
2721 }
Winson Chung70442722012-02-10 15:43:22 -08002722 float t = (Float) animation.getAnimatedValue();
2723 dispatchOnLauncherTransitionStep(fromView, t);
2724 dispatchOnLauncherTransitionStep(toView, t);
2725 }
2726 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002727
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002728 // toView should appear right at the end of the workspace shrink
2729 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002730 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002731 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002732 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002733
2734 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002735 boolean animationCancelled = false;
2736
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002737 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002738 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002739 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002740 // Prepare the position
2741 toView.setTranslationX(0.0f);
2742 toView.setTranslationY(0.0f);
2743 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002744 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002745 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002746 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002747 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002748 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2749 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002750
Daniel Sandlere4f98912013-06-25 15:13:26 -04002751 if (mWorkspace != null
2752 && !springLoaded
2753 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002754 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002755 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002756 if (!animationCancelled) {
2757 updateWallpaperVisibility(false);
2758 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002759
2760 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002761 if (mSearchDropTargetBar != null) {
2762 mSearchDropTargetBar.hideSearchBar(false);
2763 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002764 }
2765
Adam Cohencff6af82011-09-13 14:51:53 -07002766 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002767 public void onAnimationCancel(Animator animation) {
2768 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002769 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002770 });
2771
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002772 if (workspaceAnim != null) {
2773 mStateAnimation.play(workspaceAnim);
2774 }
Michael Jurka1899a362011-11-03 13:50:45 -07002775
2776 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002777
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002778 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2779 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002780
2781 // If any of the objects being animated haven't been measured/laid out
2782 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002783 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002784 (mWorkspace.getMeasuredWidth() == 0) ||
2785 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002786 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002787 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002788
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002789 final AnimatorSet stateAnimation = mStateAnimation;
2790 final Runnable startAnimRunnable = new Runnable() {
2791 public void run() {
2792 // Check that mStateAnimation hasn't changed while
2793 // we waited for a layout/draw pass
2794 if (mStateAnimation != stateAnimation)
2795 return;
2796 setPivotsForZoom(toView, scale);
2797 dispatchOnLauncherTransitionStart(fromView, animated, false);
2798 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002799 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002800 }
2801 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002802 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002803 final ViewTreeObserver observer = toView.getViewTreeObserver();
2804 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2805 public void onGlobalLayout() {
2806 startAnimRunnable.run();
2807 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2808 }
2809 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002810 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002811 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002812 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002813 } else {
2814 toView.setTranslationX(0.0f);
2815 toView.setTranslationY(0.0f);
2816 toView.setScaleX(1.0f);
2817 toView.setScaleY(1.0f);
2818 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002819 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002820
Daniel Sandlere4f98912013-06-25 15:13:26 -04002821 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002822 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002823
2824 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002825 if (mSearchDropTargetBar != null) {
2826 mSearchDropTargetBar.hideSearchBar(false);
2827 }
Michael Jurkaabded662011-03-04 12:06:57 -08002828 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002829 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002830 dispatchOnLauncherTransitionStart(fromView, animated, false);
2831 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002832 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002833 dispatchOnLauncherTransitionStart(toView, animated, false);
2834 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002835 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002836 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002837 }
2838
2839 /**
2840 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002841 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002842 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002843 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002844 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2845 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002846
Michael Jurkab3e22d92011-10-31 15:58:33 -07002847 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002848 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002849 mStateAnimation.cancel();
2850 mStateAnimation = null;
2851 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002852 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002853
Winson Chungf0ea4d32011-06-06 14:27:16 -07002854 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002855 final int fadeOutDuration =
2856 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002857 final float scaleFactor = (float)
2858 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2859 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002860 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002861 Animator workspaceAnim = null;
2862
2863 if (toState == State.WORKSPACE) {
2864 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2865 workspaceAnim = mWorkspace.getChangeStateAnimation(
2866 Workspace.State.NORMAL, animated, stagger);
2867 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2868 workspaceAnim = mWorkspace.getChangeStateAnimation(
2869 Workspace.State.SPRING_LOADED, animated);
2870 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002871
Michael Jurkab3e22d92011-10-31 15:58:33 -07002872 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002873 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002874 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002875 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002876 final LauncherViewPropertyAnimator scaleAnim =
2877 new LauncherViewPropertyAnimator(fromView);
2878 scaleAnim.
2879 scaleX(scaleFactor).scaleY(scaleFactor).
2880 setDuration(duration).
2881 setInterpolator(new Workspace.ZoomInInterpolator());
2882
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002883 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002884 .ofFloat(fromView, "alpha", 1f, 0f)
2885 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002886 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002887 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2888 @Override
2889 public void onAnimationUpdate(ValueAnimator animation) {
2890 float t = 1f - (Float) animation.getAnimatedValue();
2891 dispatchOnLauncherTransitionStep(fromView, t);
2892 dispatchOnLauncherTransitionStep(toView, t);
2893 }
2894 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002895
Michael Jurka2ecf9952012-06-18 12:52:28 -07002896 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002897
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002898 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2899 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002900 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002901
2902 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002903 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002904 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002905 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002906 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002907 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2908 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002909 if (onCompleteRunnable != null) {
2910 onCompleteRunnable.run();
2911 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002912 mAppsCustomizeContent.updateCurrentPageScroll();
2913 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002914 }
2915 });
2916
Winson Chungf0ea4d32011-06-06 14:27:16 -07002917 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002918 if (workspaceAnim != null) {
2919 mStateAnimation.play(workspaceAnim);
2920 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002921 dispatchOnLauncherTransitionStart(fromView, animated, true);
2922 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002923 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002924 } else {
2925 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002926 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002927 dispatchOnLauncherTransitionStart(fromView, animated, true);
2928 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002929 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002930 dispatchOnLauncherTransitionStart(toView, animated, true);
2931 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002932 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002933 }
2934
Michael Jurkae326f182011-11-21 14:05:46 -08002935 @Override
2936 public void onTrimMemory(int level) {
2937 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002938 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002939 mAppsCustomizeTabHost.onTrimMemory();
2940 }
2941 }
2942
Winson Chung18f41f82012-05-09 13:28:10 -07002943 @Override
2944 public void onWindowFocusChanged(boolean hasFocus) {
2945 if (!hasFocus) {
2946 // When another window occludes launcher (like the notification shade, or recents),
2947 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2948 updateWallpaperVisibility(true);
2949 } else {
2950 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002951 mWorkspace.postDelayed(new Runnable() {
2952 @Override
2953 public void run() {
2954 disableWallpaperIfInAllApps();
2955 }
2956 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002957 }
2958 }
2959
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002960 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002961 showWorkspace(animated, null);
2962 }
2963
2964 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002965 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002966 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002967 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002968 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002969
Winson Chungc7d2b602012-05-16 17:02:20 -07002970 // Show the search bar (only animate if we were showing the drop target bar in spring
2971 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002972 if (mSearchDropTargetBar != null) {
2973 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2974 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002975
Michael Jurkab737ee62011-11-15 15:57:22 -08002976 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002977 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002978
2979 // Set focus to the AppsCustomize button
2980 if (mAllAppsButton != null) {
2981 mAllAppsButton.requestFocus();
2982 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002983 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002984
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002985 // Change the state *after* we've called all the transition code
2986 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002987
Winson Chung5fb63472011-02-02 17:03:37 -08002988 // Resume the auto-advance of widgets
2989 mUserPresent = true;
2990 updateRunning();
2991
alanv1d4fde62012-10-17 13:15:47 -07002992 // Send an accessibility event to announce the context change
2993 getWindow().getDecorView()
2994 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002995
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002996 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002997 }
2998
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002999 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003000 }
3001
Michael Jurkab3e22d92011-10-31 15:58:33 -07003002 void showAllApps(boolean animated) {
3003 if (mState != State.WORKSPACE) return;
3004
3005 showAppsCustomizeHelper(animated, false);
3006 mAppsCustomizeTabHost.requestFocus();
3007
Michael Jurkab3e22d92011-10-31 15:58:33 -07003008 // Change the state *after* we've called all the transition code
3009 mState = State.APPS_CUSTOMIZE;
3010
3011 // Pause the auto-advance of widgets until we are out of AllApps
3012 mUserPresent = false;
3013 updateRunning();
3014 closeFolder();
3015
3016 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003017 getWindow().getDecorView()
3018 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003019 }
3020
Adam Cohen7777d962011-08-18 18:58:38 -07003021 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003022 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003023 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08003024 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07003025 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003026 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003027 }
Adam Cohen7777d962011-08-18 18:58:38 -07003028
Adam Cohened66b2b2012-01-23 17:28:51 -08003029 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3030 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003031 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3032
Winson Chunge7a03942011-08-05 15:05:12 -07003033 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003034 @Override
3035 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003036 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003037 // Before we show workspace, hide all apps again because
3038 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3039 // clean up our state transition functions
3040 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003041 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003042 } else {
3043 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003044 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003045 }
3046 }, (extendedDelay ?
3047 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3048 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3049 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003050
Michael Jurkad3ef3062010-11-23 16:23:58 -08003051 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003052 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003053 final boolean animated = true;
3054 final boolean springLoaded = true;
3055 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003056 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003057 }
3058 // Otherwise, we are not in spring loaded mode, so don't do anything.
3059 }
3060
Michael Jurkab737ee62011-11-15 15:57:22 -08003061 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003062 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003063 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003064 }
3065 }
3066
3067 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003068 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003069 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003070 if (mDividerAnimator != null) {
3071 mDividerAnimator.cancel();
3072 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003073 mDividerAnimator = null;
3074 }
3075 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003076 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003077 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003078 int duration = 0;
3079 if (mSearchDropTargetBar != null) {
3080 duration = mSearchDropTargetBar.getTransitionInDuration();
3081 }
3082 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003083 mDividerAnimator.start();
3084 }
3085 }
3086 }
3087
Michael Jurkab3e22d92011-10-31 15:58:33 -07003088 void lockAllApps() {
3089 // TODO
3090 }
3091
3092 void unlockAllApps() {
3093 // TODO
3094 }
3095
Winson Chungf0ea4d32011-06-06 14:27:16 -07003096 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003097 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003098 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003099 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003100 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003101 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003102 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003103 int duration = 0;
3104 if (mSearchDropTargetBar != null) {
3105 duration = mSearchDropTargetBar.getTransitionInDuration();
3106 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003107 mHotseat.animate().alpha(1f).setDuration(duration);
3108 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003109 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003110 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003111 }
3112 }
3113 }
3114
3115 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003116 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003117 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003118 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003119 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003120 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003121 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003122 int duration = 0;
3123 if (mSearchDropTargetBar != null) {
3124 duration = mSearchDropTargetBar.getTransitionOutDuration();
3125 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003126 mHotseat.animate().alpha(0f).setDuration(duration);
3127 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003128 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003129 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003130 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003131 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003132 }
3133
Patrick Dubroy5f445422011-02-18 14:35:21 -08003134 /**
3135 * Add an item from all apps or customize onto the given workspace screen.
3136 * If layout is null, add to the current screen.
3137 */
3138 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003139 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003140 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003141 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003142 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003143
Winson Chungdff8ebb2011-09-08 17:25:31 -07003144 /** Maps the current orientation to an index for referencing orientation correct global icons */
3145 private int getCurrentOrientationIndexForGlobalIcons() {
3146 // default - 0, landscape - 1
3147 switch (getResources().getConfiguration().orientation) {
3148 case Configuration.ORIENTATION_LANDSCAPE:
3149 return 1;
3150 default:
3151 return 0;
3152 }
3153 }
3154
Michael Jurkaae65ee32012-04-30 11:45:54 -07003155 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003156 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003157 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003158 // Look for the toolbar icon specified in the activity meta-data
3159 Bundle metaData = packageManager.getActivityInfo(
3160 activityName, PackageManager.GET_META_DATA).metaData;
3161 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003162 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003163 if (iconResId != 0) {
3164 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003165 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003166 }
3167 }
3168 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003169 // This can happen if the activity defines an invalid drawable
3170 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3171 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003172 } catch (Resources.NotFoundException nfe) {
3173 // This can happen if the activity defines an invalid drawable
3174 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3175 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003176 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003177 return null;
3178 }
3179
3180 // if successful in getting icon, return it; otherwise, set button to use default drawable
3181 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003182 int buttonId, ComponentName activityName, int fallbackDrawableId,
3183 String toolbarResourceName) {
3184 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003185 Resources r = getResources();
3186 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3187 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003188
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003189 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003190 // If we were unable to find the icon via the meta-data, use a generic one
3191 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003192 toolbarIcon = r.getDrawable(fallbackDrawableId);
3193 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003194 if (button != null) {
3195 button.setCompoundDrawables(toolbarIcon, null, null, null);
3196 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003197 return null;
3198 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003199 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003200 if (button != null) {
3201 button.setCompoundDrawables(toolbarIcon, null, null, null);
3202 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003203 return toolbarIcon.getConstantState();
3204 }
3205 }
3206
3207 // if successful in getting icon, return it; otherwise, set button to use default drawable
3208 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003209 int buttonId, ComponentName activityName, int fallbackDrawableId,
3210 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003211 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003212 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003213
Michael Jurka19e0fc52011-07-22 18:00:21 -07003214 if (button != null) {
3215 // If we were unable to find the icon via the meta-data, use a
3216 // generic one
3217 if (toolbarIcon == null) {
3218 button.setImageResource(fallbackDrawableId);
3219 } else {
3220 button.setImageDrawable(toolbarIcon);
3221 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003222 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003223
3224 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3225
Michael Jurka0423dcf2010-10-05 14:56:18 -07003226 }
3227
Winson Chung1b884092012-06-08 17:13:02 -07003228 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003229 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003230 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003231 }
3232
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003233 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003234 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003235 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003236 }
3237
Winson Chungbb185bd2011-11-21 12:31:42 -08003238 private void invalidatePressedFocusedStates(View container, View button) {
3239 if (container instanceof HolographicLinearLayout) {
3240 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3241 layout.invalidatePressedFocusedStates();
3242 } else if (button instanceof HolographicImageView) {
3243 HolographicImageView view = (HolographicImageView) button;
3244 view.invalidatePressedFocusedStates();
3245 }
3246 }
3247
Winson Chungc51db6a2011-10-05 11:44:49 -07003248 private boolean updateGlobalSearchIcon() {
3249 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003250 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003251 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003252 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003253
Winson Chung1cad91e2011-05-25 17:41:01 -07003254 final SearchManager searchManager =
3255 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3256 ComponentName activityName = searchManager.getGlobalSearchActivity();
3257 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003258 int coi = getCurrentOrientationIndexForGlobalIcons();
3259 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003260 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3261 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3262 if (sGlobalSearchIcon[coi] == null) {
3263 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3264 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3265 TOOLBAR_ICON_METADATA_NAME);
3266 }
3267
Winson Chungc51db6a2011-10-05 11:44:49 -07003268 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3269 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003270 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003271 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003272 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003273 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003274 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3275 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3276 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003277 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003278 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003279 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003280 }
3281 }
3282
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003283 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003284 final View searchButtonContainer = findViewById(R.id.search_button_container);
3285 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003286 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003287 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003288 }
3289
Winson Chungc51db6a2011-10-05 11:44:49 -07003290 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003291 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003292 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003293
Winson Chungc51db6a2011-10-05 11:44:49 -07003294 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003295 final SearchManager searchManager =
3296 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3297 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3298
3299 ComponentName activityName = null;
3300 if (globalSearchActivity != null) {
3301 // Check if the global search activity handles voice search
3302 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3303 intent.setPackage(globalSearchActivity.getPackageName());
3304 activityName = intent.resolveActivity(getPackageManager());
3305 }
3306
3307 if (activityName == null) {
3308 // Fallback: check if an activity other than the global search activity
3309 // resolves this
3310 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3311 activityName = intent.resolveActivity(getPackageManager());
3312 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003313 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003314 int coi = getCurrentOrientationIndexForGlobalIcons();
3315 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003316 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3317 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3318 if (sVoiceSearchIcon[coi] == null) {
3319 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3320 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3321 TOOLBAR_ICON_METADATA_NAME);
3322 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003323 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003324 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003325 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003326 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003327 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003328 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003329 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003330 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003331 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003332 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003333 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003334 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003335
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003336 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003337 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3338 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003339 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003340 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003341 }
3342
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003343 public void setVoiceButtonProxyVisible(boolean visible) {
3344 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3345 if (voiceButtonProxy != null) {
3346 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3347 }
3348 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003349 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003350 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003351 */
3352 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003353 final View marketButton = findViewById(R.id.market_button);
3354 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3355 // Find the app market activity by resolving an intent.
3356 // (If multiple app markets are installed, it will return the ResolverActivity.)
3357 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003358 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003359 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003360 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003361 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003362 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3363 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003364 marketButton.setVisibility(View.VISIBLE);
3365 } else {
3366 // We should hide and disable the view so that we don't try and restore the visibility
3367 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3368 marketButton.setVisibility(View.GONE);
3369 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003370 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003371 }
3372
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003373 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003374 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3375 Resources r = getResources();
3376 Drawable marketIconDrawable = d.newDrawable(r);
3377 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3378 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3379 marketIconDrawable.setBounds(0, 0, w, h);
3380
3381 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003382 }
3383
Michael Jurkad7c28052012-04-27 15:43:36 -07003384 @Override
3385 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003386 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003387 final List<CharSequence> text = event.getText();
3388 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003389 // Populate event with a fake title based on the current state.
3390 if (mState == State.APPS_CUSTOMIZE) {
3391 text.add(getString(R.string.all_apps_button_label));
3392 } else {
3393 text.add(getString(R.string.all_apps_home_button_label));
3394 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003395 return result;
3396 }
3397
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003398 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003399 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003400 */
3401 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3402 @Override
3403 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003404 closeSystemDialogs();
3405 }
3406 }
3407
3408 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003409 * Receives notifications whenever the appwidgets are reset.
3410 */
3411 private class AppWidgetResetObserver extends ContentObserver {
3412 public AppWidgetResetObserver() {
3413 super(new Handler());
3414 }
3415
3416 @Override
3417 public void onChange(boolean selfChange) {
3418 onAppWidgetReset();
3419 }
3420 }
3421
3422 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003423 * If the activity is currently paused, signal that we need to run the passed Runnable
3424 * in onResume.
3425 *
3426 * This needs to be called from incoming places where resources might have been loaded
3427 * while we are paused. That is becaues the Configuration might be wrong
3428 * when we're not running, and if it comes back to what it was when we
3429 * were paused, we are not restarted.
3430 *
3431 * Implementation of the method from LauncherModel.Callbacks.
3432 *
3433 * @return true if we are currently paused. The caller might be able to
3434 * skip some work in that case since we will come back again.
3435 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003436 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003437 if (mPaused) {
3438 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003439 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003440 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003441 }
3442 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003443 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003444 return true;
3445 } else {
3446 return false;
3447 }
3448 }
3449
Michael Jurkac402cd92013-05-20 15:49:32 +02003450 private boolean waitUntilResume(Runnable run) {
3451 return waitUntilResume(run, false);
3452 }
3453
Michael Jurka1e2f4652013-07-08 18:03:46 -07003454 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003455 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003456 }
3457
3458 public void removeOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003459 mOnResumeCallbacks.remove(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003460 }
3461
Michael Jurka7607c2f2013-04-03 14:33:19 -07003462 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003463 * If the activity is currently paused, signal that we need to re-run the loader
3464 * in onResume.
3465 *
3466 * This needs to be called from incoming places where resources might have been loaded
3467 * while we are paused. That is becaues the Configuration might be wrong
3468 * when we're not running, and if it comes back to what it was when we
3469 * were paused, we are not restarted.
3470 *
3471 * Implementation of the method from LauncherModel.Callbacks.
3472 *
3473 * @return true if we are currently paused. The caller might be able to
3474 * skip some work in that case since we will come back again.
3475 */
3476 public boolean setLoadOnResume() {
3477 if (mPaused) {
3478 Log.i(TAG, "setLoadOnResume");
3479 mOnResumeNeedsLoad = true;
3480 return true;
3481 } else {
3482 return false;
3483 }
3484 }
3485
3486 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003488 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003490 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003491 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003492 } else {
3493 return SCREEN_COUNT / 2;
3494 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003495 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003496
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 /**
3498 * Refreshes the shortcuts shown on the workspace.
3499 *
3500 * Implementation of the method from LauncherModel.Callbacks.
3501 */
3502 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003503 // If we're starting binding all over again, clear any bind calls we'd postponed in
3504 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3505 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003506 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003507
Michael Jurka7607c2f2013-04-03 14:33:19 -07003508 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003509 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 int count = workspace.getChildCount();
3511 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003512 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003513 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3514 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003515 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003516 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003517 if (mHotseat != null) {
3518 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003519 }
3520 }
3521
Adam Cohendcd297f2013-06-18 13:13:40 -07003522 @Override
3523 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003524 bindAddScreens(orderedScreenIds);
3525 mWorkspace.addExtraEmptyScreen();
3526 }
3527
3528 @Override
3529 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003530 int count = orderedScreenIds.size();
3531 for (int i = 0; i < count; i++) {
Winson Chung8481e322013-08-09 16:06:38 -07003532 Log.d(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")");
Winson Chung64359a52013-07-08 17:17:08 -07003533 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003534 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003535 }
3536
Adam Cohen39a06042013-07-19 14:30:12 -07003537 private boolean shouldShowWeightWatcher() {
3538 String spKey = LauncherAppState.getSharedPreferencesKey();
3539 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3540 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3541
3542 return show;
3543 }
3544
3545 private void toggleShowWeightWatcher() {
3546 String spKey = LauncherAppState.getSharedPreferencesKey();
3547 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3548 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3549
3550 show = !show;
3551
3552 SharedPreferences.Editor editor = sp.edit();
3553 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3554 editor.commit();
3555
3556 if (mWeightWatcher != null) {
3557 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3558 }
3559 }
3560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003561 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 * Bind the items start-end from the list.
3563 *
3564 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003565 */
Winson Chung64359a52013-07-08 17:17:08 -07003566 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3567 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003568 if (waitUntilResume(new Runnable() {
3569 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003570 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003571 }
3572 })) {
3573 return;
3574 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003575
Winson Chungf0c6ae02012-03-21 16:10:31 -07003576 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003577 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3578 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003579 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003580 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003581 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003582 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003584
3585 // Short circuit if we are loading dock items for a configuration which has no dock
3586 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3587 mHotseat == null) {
3588 continue;
3589 }
3590
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 switch (item.itemType) {
3592 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3593 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003594 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003595 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003596
Winson Chung64359a52013-07-08 17:17:08 -07003597 /*
3598 * TODO: FIX collision case
3599 */
Winson Chungce376632013-07-11 16:12:41 -07003600 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3601 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3602 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3603 throw new RuntimeException("OCCUPIED");
3604 }
Winson Chung64359a52013-07-08 17:17:08 -07003605 }
3606
Adam Cohendcd297f2013-06-18 13:13:40 -07003607 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3608 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003609 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003610 // Animate all the applications up now
3611 shortcut.setAlpha(0f);
3612 shortcut.setScaleX(0f);
3613 shortcut.setScaleY(0f);
3614 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003615 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003616 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003618 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003619 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003620 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003621 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003622 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3623 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003624 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003625 default:
3626 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003628 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003629
Winson Chung997a9232013-07-24 15:33:46 -07003630 if (animateIcons) {
3631 // Animate to the correct page
3632 if (newShortcutsScreenId > -1) {
3633 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3634 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3635 if (newShortcutsScreenId != currentScreenId) {
3636 mWorkspace.snapToPage(newScreenIndex);
3637 }
3638 }
3639
Winson Chung64359a52013-07-08 17:17:08 -07003640 // We post the animation slightly delayed to prevent slowdowns when we are loading
3641 // right after we return to launcher.
3642 mWorkspace.postDelayed(new Runnable() {
3643 public void run() {
3644 anim.playTogether(bounceAnims);
3645 anim.start();
3646 }
3647 }, NEW_APPS_ANIMATION_DELAY);
3648 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003650 }
3651
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 /**
3653 * Implementation of the method from LauncherModel.Callbacks.
3654 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003655 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3656 if (waitUntilResume(new Runnable() {
3657 public void run() {
3658 bindFolders(folders);
3659 }
3660 })) {
3661 return;
3662 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003663 sFolders.clear();
3664 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003665 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003666
3667 /**
3668 * Add the views for a widget to the workspace.
3669 *
3670 * Implementation of the method from LauncherModel.Callbacks.
3671 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003672 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3673 if (waitUntilResume(new Runnable() {
3674 public void run() {
3675 bindAppWidget(item);
3676 }
3677 })) {
3678 return;
3679 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003680
Daniel Sandler843e8602010-06-07 14:59:01 -04003681 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3682 if (DEBUG_WIDGETS) {
3683 Log.d(TAG, "bindAppWidget: " + item);
3684 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003685 final Workspace workspace = mWorkspace;
3686
3687 final int appWidgetId = item.appWidgetId;
3688 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003689 if (DEBUG_WIDGETS) {
3690 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3691 }
3692
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3694
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003696 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697
Adam Cohendcd297f2013-06-18 13:13:40 -07003698 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003700 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3701
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 workspace.requestLayout();
3703
Daniel Sandler843e8602010-06-07 14:59:01 -04003704 if (DEBUG_WIDGETS) {
3705 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3706 + (SystemClock.uptimeMillis()-start) + "ms");
3707 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003708 }
3709
Adam Cohen1462de32012-07-24 22:34:36 -07003710 public void onPageBoundSynchronously(int page) {
3711 mSynchronouslyBoundPages.add(page);
3712 }
3713
Joe Onorato9c1289c2009-08-17 11:03:03 -04003714 /**
3715 * Callback saying that there aren't any more items to bind.
3716 *
3717 * Implementation of the method from LauncherModel.Callbacks.
3718 */
Adam Cohene25af792013-06-06 23:08:25 -07003719 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003720 if (waitUntilResume(new Runnable() {
3721 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003722 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003723 }
3724 })) {
3725 return;
3726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 if (mSavedState != null) {
3728 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003729 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 mSavedState = null;
3732 }
3733
Winson Chung98ca0f72013-07-29 12:58:51 -07003734 // Create the custom content page here before onLayout to prevent flashing
3735 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3736 mWorkspace.createCustomContentPage();
3737 }
3738
Adam Cohen1462de32012-07-24 22:34:36 -07003739 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003741 // If we received the result of any pending adds while the loader was running (e.g. the
3742 // widget configuration forced an orientation change), process them now.
3743 for (int i = 0; i < sPendingAddList.size(); i++) {
3744 completeAdd(sPendingAddList.get(i));
3745 }
3746 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003747
Winson Chungc51db6a2011-10-05 11:44:49 -07003748 // Update the market app icon as necessary (the other icons will be managed in response to
3749 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003750 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003751
Winson Chungf0c6ae02012-03-21 16:10:31 -07003752 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003753 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003754 mWorkspace.stripDuplicateApps();
3755 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003756 }
Adam Cohen99894d92013-06-14 11:22:59 -07003757
Adam Cohen66a01fd2013-06-11 12:48:00 -07003758 mWorkspace.post(new Runnable() {
3759 @Override
3760 public void run() {
3761 onFinishBindingItems();
3762 }
3763 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003764 }
3765
Winson Chunga2413752012-04-03 14:22:34 -07003766 private boolean canRunNewAppsAnimation() {
3767 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3768 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3769 }
3770
Winson Chungc9168342013-06-26 14:54:55 -07003771 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3772 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3773 PropertyValuesHolder.ofFloat("alpha", 1f),
3774 PropertyValuesHolder.ofFloat("scaleX", 1f),
3775 PropertyValuesHolder.ofFloat("scaleY", 1f));
3776 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3777 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3778 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3779 return bounceAnim;
3780 }
3781
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003782 @Override
3783 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003784 boolean searchVisible = updateGlobalSearchIcon();
3785 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003786 if (mSearchDropTargetBar != null) {
3787 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3788 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003789 }
3790
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003791 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 * Add the icons for all apps.
3793 *
3794 * Implementation of the method from LauncherModel.Callbacks.
3795 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003796 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003797 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3798 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003799 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003800 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003801 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003802 }
3803
3804 /**
3805 * A package was updated.
3806 *
3807 * Implementation of the method from LauncherModel.Callbacks.
3808 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003809 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3810 if (waitUntilResume(new Runnable() {
3811 public void run() {
3812 bindAppsUpdated(apps);
3813 }
3814 })) {
3815 return;
3816 }
3817
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003818 if (mWorkspace != null) {
3819 mWorkspace.updateShortcuts(apps);
3820 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003821 }
3822
3823 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003824 * A package was uninstalled. We take both the super set of packageNames
3825 * in addition to specific applications to remove, the reason being that
3826 * this can be called when a package is updated as well. In that scenario,
3827 * we only remove specific components from the workspace, where as
3828 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003829 *
3830 * Implementation of the method from LauncherModel.Callbacks.
3831 */
Winson Chung83892cc2013-05-01 16:53:33 -07003832 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3833 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003834 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003835 if (waitUntilResume(new Runnable() {
3836 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003837 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003838 }
3839 })) {
3840 return;
3841 }
3842
Winson Chung64359a52013-07-08 17:17:08 -07003843 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003844 mWorkspace.removeItemsByPackageName(packageNames);
3845 } else {
3846 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003847 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003848
Winson Chunga1820962011-10-03 16:31:06 -07003849 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003850 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 }
Joe Onoratobe386092009-11-17 17:32:16 -08003852
3853 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003854 * A number of packages were updated.
3855 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003856 private ArrayList<Object> mWidgetsAndShortcuts;
3857 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003858 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003859 bindPackagesUpdated(mWidgetsAndShortcuts);
3860 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003861 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003862 };
3863
3864 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3865 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3866 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003867 return;
3868 }
3869
Winson Chung64359a52013-07-08 17:17:08 -07003870 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003871 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003872 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003873 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003874 }
3875
Winson Chung400438b2011-01-16 17:53:48 -08003876 private int mapConfigurationOriActivityInfoOri(int configOri) {
3877 final Display d = getWindowManager().getDefaultDisplay();
3878 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3879 switch (d.getRotation()) {
3880 case Surface.ROTATION_0:
3881 case Surface.ROTATION_180:
3882 // We are currently in the same basic orientation as the natural orientation
3883 naturalOri = configOri;
3884 break;
3885 case Surface.ROTATION_90:
3886 case Surface.ROTATION_270:
3887 // We are currently in the other basic orientation to the natural orientation
3888 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3889 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3890 break;
3891 }
3892
3893 int[] oriMap = {
3894 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3895 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3896 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3897 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3898 };
3899 // Since the map starts at portrait, we need to offset if this device's natural orientation
3900 // is landscape.
3901 int indexOffset = 0;
3902 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3903 indexOffset = 1;
3904 }
3905 return oriMap[(d.getRotation() + indexOffset) % 4];
3906 }
Adam Cohen446e9402011-09-15 18:21:21 -07003907
Winson Chung4b919f82012-05-01 10:44:08 -07003908 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003909 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003910 getResources().getBoolean(R.bool.allow_rotation);
3911 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003912 }
Winson Chung4b919f82012-05-01 10:44:08 -07003913 public void lockScreenOrientation() {
3914 if (isRotationEnabled()) {
3915 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3916 .getConfiguration().orientation));
3917 }
3918 }
3919 public void unlockScreenOrientation(boolean immediate) {
3920 if (isRotationEnabled()) {
3921 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003922 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003923 } else {
3924 mHandler.postDelayed(new Runnable() {
3925 public void run() {
3926 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3927 }
3928 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003929 }
Winson Chung4b919f82012-05-01 10:44:08 -07003930 }
Winson Chung400438b2011-01-16 17:53:48 -08003931 }
3932
Winson Chung82f55532011-08-09 14:14:23 -07003933 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003934 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003935 if (DISABLE_CLINGS) {
3936 return false;
3937 }
3938
Brett Chabot2a9e2282011-08-23 15:03:13 -07003939 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003940 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003941
Michael Jurkae233a8b2013-03-19 13:49:20 +01003942 // Restricted secondary users (child mode) will potentially have very few apps
3943 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003944// boolean supportsLimitedUsers =
3945// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3946// Account[] accounts = AccountManager.get(this).getAccounts();
3947// if (supportsLimitedUsers && accounts.length == 0) {
3948// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3949// Bundle restrictions = um.getUserRestrictions();
3950// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3951// return false;
3952// }
3953// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003954 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003955 }
Michael Jurka22143132012-10-04 17:42:38 +02003956
Winson Chung7d7541e2011-09-16 20:14:36 -07003957 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003958 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003959 if (cling != null) {
3960 cling.init(this, positionData);
3961 cling.setVisibility(View.VISIBLE);
3962 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3963 if (animate) {
3964 cling.buildLayer();
3965 cling.setAlpha(0f);
3966 cling.animate()
3967 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003968 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003969 .setDuration(SHOW_CLING_DURATION)
3970 .setStartDelay(delay)
3971 .start();
3972 } else {
3973 cling.setAlpha(1f);
3974 }
Michael Jurka22143132012-10-04 17:42:38 +02003975 cling.setFocusableInTouchMode(true);
3976 cling.post(new Runnable() {
3977 public void run() {
3978 cling.setFocusable(true);
3979 cling.requestFocus();
3980 }
3981 });
3982 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3983 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003984 }
3985 return cling;
3986 }
Michael Jurka22143132012-10-04 17:42:38 +02003987
Winson Chung7d7541e2011-09-16 20:14:36 -07003988 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003989 // To catch cases where siblings of top-level views are made invisible, just check whether
3990 // the cling is directly set to GONE before dismissing it.
3991 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003992 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003993 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003994 anim.addListener(new AnimatorListenerAdapter() {
3995 public void onAnimationEnd(Animator animation) {
3996 cling.setVisibility(View.GONE);
3997 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003998 // We should update the shared preferences on a background thread
3999 new Thread("dismissClingThread") {
4000 public void run() {
4001 SharedPreferences.Editor editor = mSharedPrefs.edit();
4002 editor.putBoolean(flag, true);
4003 editor.commit();
4004 }
4005 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004006 };
4007 });
4008 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004009 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004010 }
4011 }
Michael Jurka22143132012-10-04 17:42:38 +02004012
Winson Chung9d9d74f2011-09-19 11:49:12 -07004013 private void removeCling(int id) {
4014 final View cling = findViewById(id);
4015 if (cling != null) {
4016 final ViewGroup parent = (ViewGroup) cling.getParent();
4017 parent.post(new Runnable() {
4018 @Override
4019 public void run() {
4020 parent.removeView(cling);
4021 }
4022 });
Michael Jurka22143132012-10-04 17:42:38 +02004023 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004024 }
4025 }
Michael Jurka974c3862012-05-22 22:00:31 -07004026
4027 private boolean skipCustomClingIfNoAccounts() {
4028 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4029 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4030 if (customCling) {
4031 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004032 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004033 Account[] accounts = am.getAccountsByType("com.google");
4034 return accounts.length == 0;
4035 }
4036 return false;
4037 }
4038
Winson Chung7d7541e2011-09-16 20:14:36 -07004039 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004040 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004041 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004042 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4043 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004044 // If we're not using the default workspace layout, replace workspace cling
4045 // with a custom workspace cling (usually specified in an overlay)
4046 // For now, only do this on tablets
4047 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004048 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004049 // Use a custom cling
4050 View cling = findViewById(R.id.workspace_cling);
4051 ViewGroup clingParent = (ViewGroup) cling.getParent();
4052 int clingIndex = clingParent.indexOfChild(cling);
4053 clingParent.removeViewAt(clingIndex);
4054 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4055 clingParent.addView(customCling, clingIndex);
4056 customCling.setId(R.id.workspace_cling);
4057 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004058 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004059 } else {
4060 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004061 }
4062 }
4063 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004064 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004065 if (isClingsEnabled() &&
4066 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004067 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004068 } else {
4069 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004070 }
4071 }
4072 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004073 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004074 if (isClingsEnabled() &&
4075 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4076 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004077 } else {
4078 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004079 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004080 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004081 }
4082 public boolean isFolderClingVisible() {
4083 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004084 if (cling != null) {
4085 return cling.getVisibility() == View.VISIBLE;
4086 }
4087 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004088 }
Winson Chung82f55532011-08-09 14:14:23 -07004089 public void dismissWorkspaceCling(View v) {
4090 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004091 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004092 }
4093 public void dismissAllAppsCling(View v) {
4094 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004095 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4096 }
4097 public void dismissFolderCling(View v) {
4098 Cling cling = (Cling) findViewById(R.id.folder_cling);
4099 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004100 }
4101
Winson Chung80baf5a2010-08-09 16:03:15 -07004102 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004103 * Prints out out state for debugging.
4104 */
4105 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004106 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004107 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004108 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4109 Log.d(TAG, "mRestoring=" + mRestoring);
4110 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4111 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004112 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004113 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004114
Winson Chung785d2eb2011-04-14 16:08:02 -07004115 if (mAppsCustomizeContent != null) {
4116 mAppsCustomizeContent.dumpState();
4117 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004118 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004119 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004120
4121 @Override
4122 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4123 super.dump(prefix, fd, writer, args);
4124 writer.println(" ");
4125 writer.println("Debug logs: ");
4126 for (int i = 0; i < sDumpLogs.size(); i++) {
4127 writer.println(" " + sDumpLogs.get(i));
4128 }
4129 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004130
4131 public static void dumpDebugLogsToConsole() {
4132 Log.d(TAG, "");
4133 Log.d(TAG, "*********************");
4134 Log.d(TAG, "Launcher debug logs: ");
4135 for (int i = 0; i < sDumpLogs.size(); i++) {
4136 Log.d(TAG, " " + sDumpLogs.get(i));
4137 }
4138 Log.d(TAG, "*********************");
4139 Log.d(TAG, "");
4140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004141}
Michael Jurka2763be32011-02-24 11:19:57 -08004142
Michael Jurkaabded662011-03-04 12:06:57 -08004143interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004144 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004145 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004146 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004147 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004148 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004149}